Cannot extend controller test class

懵懂的女人 提交于 2019-12-25 10:01:15

问题


I have a Base class to be extended by the controller tests:

//Base.php
namespace AppBundle\Tests\System;

abstract class Base extends \PHPUnit_Framework_TestCase
{
    ...
}

But when I try to extend it:

//DefaultControllerTest.php
namespace AppBundle\Tests\Controller;

use AppBundle\Tests\System\Base;

class DefaultControllerTest extends Base
{
    ...
}

I get this error:

/usr/bin/php /tmp/ide-phpunit.php --configuration /server/project/phpunit.xml /server/project/src/AppBundle/Tests Testing started at 18:36 ... PHP Fatal error: Class 'AppBundle\Tests\System\Base' not found in /server/project/src/AppBundle/Tests/Controller/DefaultControllerTest.php on line 7

Process finished with exit code 255

PhpStorm is detecting the Base class in DefaultController.php, so it does not seem a typo.

This is my phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
        backupGlobals               = "false"
        backupStaticAttributes      = "false"
        colors                      = "true"
        convertErrorsToExceptions   = "true"
        convertNoticesToExceptions  = "true"
        convertWarningsToExceptions = "true"
        processIsolation            = "false"
        stopOnFailure               = "false"
        syntaxCheck                 = "false"
        bootstrap                   = "app/bootstrap.php.cache" >

    <testsuites>
        <testsuite name="Tests">
            <directory>src/AppBundle/Tests</directory>
        </testsuite>
    </testsuites>

    <php>
        <server name="KERNEL_DIR" value="app" />
    </php>

    <groups>
        <exclude>
            <group>slow</group>
        </exclude>
    </groups>

    <!-- This is for code coverage -->
    <filter>
        <whitelist>
            <directory>app</directory>
            <directory>src</directory>

            <exclude>
                <directory>app/cache/*</directory>

                <file>app/check.php</file>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

Any idea of what I'm missing?


回答1:


I believe you can actually tell phpunit directly to use the composer autoloader:

bootstrap="app/autoload.php"

Instead of "web/app_test.php"

This is for Symfony >= 2.8, for previous versions I think it would be "vendor/autoload.php"




回答2:


As @malcolm and @Ilya Yarkovets suggest, I need to include the testing autoloader. So I've created an app_test.php file inside web directory with this configuration:

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;

/**
 * @var Composer\Autoload\ClassLoader $loader
 */
$loader = require __DIR__.'/../app/autoload.php';
Debug::enable();

$kernel = new AppKernel('test', true);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

and changed this line in phpunit.xml:

bootstrap                   = "web/app_test.php" >

I'm not sure yet if app_test.php should be modified, but now seems to work as expected.



来源:https://stackoverflow.com/questions/35068516/cannot-extend-controller-test-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!