【问题标题】:Symfony 4 phpunit class WebTestCase not found找不到 Symfony 4 phpunit 类 WebTestCase
【发布时间】:2019-04-17 08:10:19
【问题描述】:

我用 composer create project 开始了一个新的 symfony/skeleton 项目。

现在我想添加php单元,所以我这样做了

composer require --dev test

Flex 下载并安装 phpunit-bridge。

但是我在运行 php 单元时遇到了问题,因为它似乎无法正确自动加载。

我运行bin/phpunit,它显示:

#!/usr/bin/env php
PHP Fatal error:  Class 'Symfony\Bundle\FrameworkBundle\Test\WebTestCase' not found in /var/www/backend/tests/TestTest.php on line 6
PHP Stack trace:
PHP   1. {main}() /var/www/backend/bin/phpunit:0
PHP   2. require() /var/www/backend/bin/phpunit:20
PHP   3. include() /var/www/backend/vendor/symfony/phpunit-bridge/bin/simple-phpunit:261
PHP   4. PHPUnit\TextUI\Command::main() /var/www/backend/bin/.phpunit/phpunit-6.5/phpunit:17
PHP   5. Symfony\Bridge\PhpUnit\Legacy\CommandForV6->run() /var/www/backend/bin/.phpunit/phpunit-6.5/src/TextUI/Command.php:148
PHP   6. Symfony\Bridge\PhpUnit\Legacy\CommandForV6->handleArguments() /var/www/backend/bin/.phpunit/phpunit-6.5/src/TextUI/Command.php:159
PHP   7. PHPUnit\Util\Configuration->getTestSuiteConfiguration() /var/www/backend/bin/.phpunit/phpunit-6.5/src/TextUI/Command.php:837
PHP   8. PHPUnit\Util\Configuration->getTestSuite() /var/www/backend/bin/.phpunit/phpunit-6.5/src/Util/Configuration.php:918
PHP   9. PHPUnit\Framework\TestSuite->addTestFiles() /var/www/backend/bin/.phpunit/phpunit-6.5/src/Util/Configuration.php:1014
PHP  10. PHPUnit\Framework\TestSuite->addTestFile() /var/www/backend/bin/.phpunit/phpunit-6.5/src/Framework/TestSuite.php:403
PHP  11. PHPUnit\Util\Fileloader::checkAndLoad() /var/www/backend/bin/.phpunit/phpunit-6.5/src/Framework/TestSuite.php:325
PHP  12. PHPUnit\Util\Fileloader::load() /var/www/backend/bin/.phpunit/phpunit-6.5/src/Util/Fileloader.php:48
PHP  13. include_once() /var/www/backend/bin/.phpunit/phpunit-6.5/src/Util/Fileloader.php:64

我的phpunit.xml.dist是flex安装的:

<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="vendor/autoload.php"
>
    <php>
        <ini name="error_reporting" value="-1" />
    </php>

    <testsuites>
        <testsuite name="Project Test Suite">
            <directory>tests</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist>
            <directory>src</directory>
        </whitelist>
    </filter>

    <listeners>
        <listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
    </listeners>
</phpunit>

bin/phpunit.php 也是来自 flex 的:

#!/usr/bin/env php
<?php

if (!file_exists(dirname(__DIR__).'/vendor/symfony/phpunit-bridge/bin/simple-phpunit')) {
    echo "Unable to find the `simple-phpunit` script in `vendor/symfony/phpunit-bridge/bin/`.\n";
    exit(1);
}

$classLoader = require dirname(__DIR__).'/vendor/autoload.php';
StudentReport\Kernel::bootstrapEnv('test');
$classLoader->unregister();

if (false === getenv('SYMFONY_PHPUNIT_REMOVE')) {
    putenv('SYMFONY_PHPUNIT_REMOVE=');
}
if (false === getenv('SYMFONY_PHPUNIT_DIR')) {
    putenv('SYMFONY_PHPUNIT_DIR='.__DIR__.'/.phpunit');
}

require dirname(__DIR__).'/vendor/symfony/phpunit-bridge/bin/simple-phpunit';

这是我的测试类/tests/TestTest.php:

<?php namespace StudentReport\Tests;


use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class TestTest extends WebTestCase
{

    public function testHomepage()
    {
        $client = static::createClient();

        $client->request('GET', '/');

        $this->assertEquals(200, $client->getResponse()->getStatusCode());
    }
}

这是我的 composer.json:

{
    "type": "project",
    "license": "proprietary",
    "require": {
        "php": "^7.1.3",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "doctrine/doctrine-fixtures-bundle": "^3.0",
        "friendsofsymfony/rest-bundle": "^2.4",
        "friendsofsymfony/user-bundle": "^2.1",
        "jms/serializer-bundle": "^2.4",
        "league/tactician-bundle": "^1.1",
        "lexik/jwt-authentication-bundle": "2.5.4",
        "nelmio/api-doc-bundle": "^3.3",
        "nelmio/cors-bundle": "^1.5",
        "ramsey/uuid": "^3.8",
        "symfony/console": "^4.1",
        "symfony/flex": "^1.1",
        "symfony/framework-bundle": "^4.1",
        "symfony/monolog-bundle": "^3.3",
        "symfony/orm-pack": "^1.0",
        "symfony/swiftmailer-bundle": "^3.2",
        "symfony/templating": "*",
        "symfony/yaml": "*"
    },
    "require-dev": {
        "symfony/dotenv": "*",
        "symfony/maker-bundle": "^1.7",
        "symfony/test-pack": "^1.0"
    },
    "config": {
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "StudentReport\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "StudentReport\\Tests\\": "tests/"
        }
    },
    "replace": {
        "paragonie/random_compat": "2.*",
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php71": "*",
        "symfony/polyfill-php70": "*",
        "symfony/polyfill-php56": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": "true",
            "require": "4.1.*"
        }
    }
}

【问题讨论】:

标签: php symfony phpunit autoloader


【解决方案1】:

尝试像这样添加它:

composer 需要 --dev symfony/phpunit-bridge

问候

【讨论】:

    【解决方案2】:

    所以这可能对你有用。

    bin/phpunit 中删除这些行

    $classLoader = require dirname(__DIR__).'/vendor/autoload.php';
    App\Kernel::bootstrapEnv('test');
    $classLoader->unregister();
    

    来源 -> https://github.com/symfony/symfony/issues/29215#issuecomment-438719415

    【讨论】:

      【解决方案3】:

      我也遇到了这个问题,这个解决方案解决了这个问题

      首先在 symfony 4 上删除这个包

      composer remove symfony/phpunit-bridge
      

      然后运行

      composer require --dev phpunit/phpunit
      

      然后在根目录下编辑 phpunit.xml.dist

      <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
               backupGlobals="false"
               colors="true"
               bootstrap="vendor/autoload.php">
      

      完成后,您现在可以运行:

       ./vendor/bin/phpunit test
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-11
        • 1970-01-01
        • 2016-06-13
        • 2014-07-16
        • 2019-01-04
        • 1970-01-01
        • 2018-06-11
        • 2019-07-22
        相关资源
        最近更新 更多