【问题标题】:Cannot extend controller test class无法扩展控制器测试类
【发布时间】:2016-05-06 05:50:39
【问题描述】:

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

//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 正在检测 DefaultController.php 中的 Base 类,因此它似乎不是错字。

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

知道我错过了什么吗?

【问题讨论】:

  • 我认为phpunit不要使用composer autoloader。
  • PHPStorm 检测到Base 类的东西仅与 PHPStorm 相关:)。当您运行测试时,您的 DefaultControllerTest 课程对您的 Base 课程一无所知。你需要某种自动加载器/内核来为你加载它。
  • 感谢@malcolm 提供线索。
  • 还有你,@Ilya Yarkovets

标签: symfony phpunit


【解决方案1】:

正如@malcolm 和@Ilya Yarkovets 所建议的,我需要包含测试自动加载器。所以我用这个配置在web 目录中创建了一个app_test.php 文件:

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

我还不确定是否应该修改 app_test.php,但现在似乎可以按预期工作。

【讨论】:

    【解决方案2】:

    我相信你实际上可以直接告诉 phpunit 使用 composer 自动加载器:

    bootstrap="app/autoload.php"
    

    而不是"web/app_test.php"

    这适用于 Symfony >= 2.8,对于以前的版本,我认为应该是 "vendor/autoload.php"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多