Phpunit未运行Symfony测试

问题描述:

我试图在Symfony中运行一些功能测试,但它们不起作用。在“不执行的各测试”运行PHPUnit针对整个的appbundle结果,并针对一个特定的类让我这个错误:Phpunit未运行Symfony测试

$ 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。我不得不要求的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> 
+0

你能发布(至少是'composer.json'文件的自动加载器部分)吗? – Matteo

+0

youve not told phpunit where autoloader is。您需要创建一个phpunit.xml文件。 – DevDonkey

+0

@DevDonkey没有phpunit.xml.dist照顾这个? – LuteceTheCrab

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