Phpunit没有运行Symfony测试

时间:2017-02-20 14:23:28

标签: php phpunit symfony

我正在尝试在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 Best Practices示例:

// 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。因为出现了这个异常,我必须要求phpunit / phpunit ^ 4.8和composer:

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>

1 个答案:

答案 0 :(得分:2)

这是一个版本问题,似乎:出于某种原因,在创建Symfony项目时,PHP版本设置为5.5,这限制了更新的PHPUnit版本。当我下载PHAR版本时,它是版本6.0(它不适用于我正在使用的项目版本 - PHP 5.6 - 但由于我的php-cli是7.0,因此可以运行)。 PHPUnit 4.8,通过作曲家,确实运行了ApplicationAvailability测试(至少,这是我的代码发生的奇怪事情,我稍后会发现),但仍然有尴尬的行为(可理解,因为它&#39;实际上已弃用)。当我使用PHP 5.6配置composer.json时,一切都很清楚:PHPUnit升级到5.7,与PHP 5.6完美配合。感谢@mickadoo指出测试运行正常。