无法扩展控制器测试 class
Cannot extend controller test class
我有一个 Base class 要通过控制器测试扩展:
//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
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 正在检测 DefaultController.php
中的 Base
class,因此它似乎不是拼写错误。
这是我的 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>
知道我遗漏了什么吗?
正如@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
是否应该修改,但现在似乎可以按预期工作。
我相信你实际上可以直接告诉 phpunit 使用 composer 自动加载器:
bootstrap="app/autoload.php"
而不是"web/app_test.php"
这是针对 Symfony >= 2.8,对于以前的版本我认为是 "vendor/autoload.php"
我有一个 Base class 要通过控制器测试扩展:
//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 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 正在检测 DefaultController.php
中的 Base
class,因此它似乎不是拼写错误。
这是我的 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>
知道我遗漏了什么吗?
正如@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
是否应该修改,但现在似乎可以按预期工作。
我相信你实际上可以直接告诉 phpunit 使用 composer 自动加载器:
bootstrap="app/autoload.php"
而不是"web/app_test.php"
这是针对 Symfony >= 2.8,对于以前的版本我认为是 "vendor/autoload.php"