Phpunit无法运行Symfony测试

3

我正在尝试在Symfony中运行一些功能测试,但它们无法工作。 运行针对整个AppBundle的phpunit会导致“未执行任何测试”,而将其定位到特定类会导致出现以下错误:

$ phpunit tests/AppBundle/ApplicationAvailabilityFunctionalTest.php
PHP Fatal error:  Uncaught PHPUnit\Runner\Exception: Class 'tests/AppBundle/ApplicationAvailabilityFunctionalTest' could not be found in '/home/.../tests/AppBundle/ApplicationAvailabilityFunctionalTest.php'. in phar:///usr/local/bin/phpunit/phpunit/Runner/StandardTestSuiteLoader.php:107

后来我发现单元测试也没能正常工作,报错信息一样。
我尝试运行的功能测试是Symfony最佳实践示例:
// tests/AppBundle/ApplicationAvailabilityFunctionalTest.php
namespace Tests\AppBundle;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ApplicationAvailabilityFunctionalTest extends WebTestCase
{
    /**
     * @dataProvider urlProvider
     */
    public function testPageIsSuccessful($url)
    {
        $client = self::createClient();
        $client->request('GET', $url);

        $this->assertTrue($client->getResponse()->isSuccessful());
    }

    public function urlProvider()
    {
        return array(
            array('/'),
            array('/team'),
            // ...
        );
    }
} 

我尝试将目标对准另一个类,即DefaultControllerTest,但是仍然出现了相同的异常。 我正在运行phpunit版本6.0.7(以Phar方式下载),Symfony 3.2和PHP 7.0。由于出现这个异常,我必须使用composer要求phpunit / phpunit ^4.8。

PHP Fatal error:  Class 'PHPUnit_Framework_TestCase' not found in /home/.../vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php on line 23

我尝试使用vendor/bin/phpunit而不是.phar版本,至少它可以检测到测试,但所有测试结果都很奇怪。例如,它无法识别expectException

$ vendor/bin/phpunit -c phpunit.xml.dist
Error: Call to undefined method Tests\AppBundle\Util\Chart\ChartDataTest::expectException()

在功能测试中,似乎无法到达任何路径,即使这些路径在浏览器中完全可访问:
$ vendor/bin/phpunit -c phpunit.xml.dist
1) Tests\AppBundle\ApplicationAvailabilityFunctionalTest::testPageIsSuccessful with data set #0 ('/')
Failed asserting that false is true.

这是一个版本问题吗? composer.json 文件:
    {
"name": "symfony/framework-standard-edition",
"license": "MIT",
"type": "project",
"description": "The \"Symfony Standard Edition\" distribution",
"autoload": {
    "psr-4": { "": "src/" },
    "classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},
"autoload-dev": {
    "psr-4": { "Tests\\": "tests/" }
},
"require": {
    "php": ">=5.5.9",
    "symfony/symfony": "3.2.*",
    "doctrine/orm": "^2.5",
    "doctrine/doctrine-bundle": "^1.6",
    "doctrine/doctrine-cache-bundle": "^1.2",
    "symfony/swiftmailer-bundle": "^2.3",
    "symfony/monolog-bundle": "^3.0",
    "symfony/polyfill-apcu": "^1.0",
    "sensio/distribution-bundle": "^5.0",
    "sensio/framework-extra-bundle": "^3.0.2",
    "incenteev/composer-parameter-handler": "^2.0",
    "phpunit/phpunit": "^4.8"
},
"require-dev": {
    "sensio/generator-bundle": "^3.0",
    "symfony/phpunit-bridge": "^3.0",
    "doctrine/doctrine-fixtures-bundle": "^2.3"
},
"scripts": {
    "symfony-scripts": [
        "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
    ],
    "post-install-cmd": [
        "@symfony-scripts"
    ],
    "post-update-cmd": [
        "@symfony-scripts"
    ]
},
"config": {
    "platform": {
        "php": "5.5.9"
    }
},
"extra": {
    "symfony-app-dir": "app",
    "symfony-bin-dir": "bin",
    "symfony-var-dir": "var",
    "symfony-web-dir": "web",
    "symfony-tests-dir": "tests",
    "symfony-assets-install": "relative",
    "incenteev-parameters": {
        "file": "app/config/parameters.yml"
    },
    "branch-alias": {
        "dev-master": "3.2-dev"
    }
}
   }

这里是phpunit.xml.dist文件: (目前位于根目录,自创建以来未被修改)
<?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/4.8/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="app/autoload.php"
>
    <php>
        <ini name="error_reporting" value="-1" />
        <server name="KERNEL_DIR" value="app/" />
    </php>

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

    <filter>
        <whitelist>
            <directory>src</directory>
            <exclude>
                <directory>src/*Bundle/Resources</directory>
                <directory>src/*/*Bundle/Resources</directory>
                <directory>src/*/Bundle/*Bundle/Resources</directory>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

你能发布(至少是自动加载器部分的)composer.json文件吗? - Matteo
@DevDonkey,phpunit.xml.dist 不会处理这个问题吗? - Caio
只有在你告诉它使用时,它才会使用它。 - DevDonkey
@DevDonkey 使用-c phpunit.xml.dist,结果是相同的。 - Caio
@mickdev 我的测试位于 tests/ 文件夹中,正如文档所建议的那样。phpunit.xml.dist 位于根目录下(我不知道它是否放置在正确的位置,但这是在使用 composer 创建项目时生成的位置)。 - Caio
显示剩余7条评论
1个回答

3
似乎是一个版本问题:当Symfony项目创建时,PHP版本被设置为5.5,这限制了较新版本的PHPUnit。当我下载了PHAR版本时,它是6.0版本(不适用于我正在使用的项目版本 - PHP 5.6 - 但可以运行,因为我的php-cli是7.0)。通过composer安装的PHPUnit 4.8可以正常运行ApplicationAvailability测试(至少在我的代码中发生了一些奇怪的事情,稍后会查找),但仍然存在一些尴尬的行为(可以理解,因为它几乎已经过时了)。当我将composer.json配置为PHP 5.6时,一切都清晰明了:PHPUnit升级到了5.7,可以完美地与PHP 5.6配合使用。感谢 @mickadoo 指出测试可以正常运行。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接