无法扩展控制器测试类

时间:2016-01-28 17:51:52

标签: symfony phpunit

我有一个由控制器测试扩展的Base类:

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

abstract class Base extends \PHPUnit_Framework_TestCase
{
    ...
}

但是当我试图扩展它时:

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

use AppBundle\Tests\System\Base;

class DefaultControllerTest extends Base
{
    ...
}

我收到此错误:

  

/ usr / bin / php /tmp/ide-phpunit.php --configuration /server/project/phpunit.xml / server / project / src / AppBundle / Tests   测试于18:36开始......   PHP致命错误:第7行的/server/project/src/AppBundle/Tests/Controller/DefaultControllerTest.php中找不到类'AppBundle \ Tests \ System \ Base'

     

处理完成,退出代码为255

PhpStorm正在检测Base中的DefaultController.php类,所以它似乎不是一个错字。

这是我的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>

知道我缺少什么吗?

2 个答案:

答案 0 :(得分:2)

正如@malcolm和@Ilya Yarkovets所说,我需要包含测试自动加载器。所以我使用此配置在TypeError: 'list' object not callable目录中创建了一个app_test.php文件:

web

并在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); 中更改了这一行:

phpunit.xml

我还不确定是否应该修改bootstrap = "web/app_test.php" > ,但现在似乎按预期工作了。

答案 1 :(得分:1)

我相信你实际上可以直接告诉phpunit使用composer autoloader:

bootstrap="app/autoload.php"

而不是"web/app_test.php"

这是针对Symfony&gt; = 2.8,对于以前的版本,我认为它是"vendor/autoload.php"