简体   繁体   中英

Phpunit not running Symfony tests

I'm trying to run a few functional tests in Symfony, but they won't work. Running phpunit targeting the whole AppBundle results in "No tests executed", and targeting it to a specific class gets me this error:

$ 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

Later I found out unit tests are not working as well, same error message.

The functional test I'm trying to run is Symfony Best Practices example:

// 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'),
            // ...
        );
    }
} 

I have tried targeting another class, the DefaultControllerTest, but the same exception showed up. I'm running phpunit version 6.0.7 (downloaded as Phar), Symfony 3.2 and PHP 7.0. I've had to require phpunit/phpunit ^4.8 with composer since this exception appeared:

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

I have tried using vendor/bin/phpunit instead of the .phar version, and at least it detects the tests, but all of them have weird results. It doesn't recognize expectException, for instance:

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

In functional tests, it doesn't seem to reach any path, even though they are perfectly accessible through the browser:

$ vendor/bin/phpunit -c phpunit.xml.dist
1) Tests\AppBundle\ApplicationAvailabilityFunctionalTest::testPageIsSuccessful with data set #0 ('/')
Failed asserting that false is true.

Is it a version issue?

composer.json file:

    {
"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"
    }
}
   }

Here's phpunit.xml.dist : (currently in root folder, unmodified since project creation)

<?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>

It was a version issue, it seems: for some reason when the Symfony project was created the PHP version was set to 5.5, which restrained more recent PHPUnit versions. When I downloaded the PHAR version, it was version 6.0 (which did not work with the version of the project I was working with - PHP 5.6 - but was runnable since my php-cli was 7.0). PHPUnit 4.8, which came through composer, did run the ApplicationAvailability tests ok (this, at least, is something weird happening with my code, I will find out about that later), but still had awkward behaviour (understandable, since it's practically deprecated). When I configured composer.json with PHP 5.6, everything was clear: PHPUnit upgraded to 5.7, which works perfectly with PHP 5.6. Thanks @mickadoo for pointing out that the tests did run ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM