共享测试设置时找不到类



我正在用phpunit做一些功能测试,并在Symfony 3.2应用程序中大口大口地吃

我有多个测试需要在测试运行之前加载数据库并登录到应用程序。这是这样设置的:

<?php
namespace TestsLegacyFunctional;
use GuzzleHttpClient;
use GuzzleHttpCookieCookieJar;
use SymfonyBundleFrameworkBundleTestKernelTestCase;
class UsersTest extends KernelTestCase
{
    protected function setUp()
    {
        self::bootKernel();
        $dir = self::$kernel->getRootDir()."/../Tests/Legacy/Functional";
        exec('mysql < '.$dir.'/pagesTestData.sql');
    }
    protected function getCookieJar()
    {
        static $jar;
        if (!isset($jar)){
            $jar = new CookieJar;
        }
        return $jar;
    }
    protected function getClientAndLogin()
    {
        $client = new Client([
            'base_uri' => 'http://functionaltest.thesite.example.com',
            'cookies' => $this->getCookieJar()
        ]);
        $loginPageResponse = $client->get('/index.php');
        $csrf = [];
        preg_match('#<input type="hidden" name="X-XSRF-TOKEN" value="(.*?)" />#ism', $loginPageResponse->getBody(), $csrf);
        if ($csrf[1]){
            $loginData = [
                'email_address' => 'test@example.com',
                'password' => 'secure',
                'X-XSRF-TOKEN' => $csrf[1],
                'start' => 'Start!'
            ];
            if ($client->post('/index.php', [
                'form_params' => $loginData,
                'cookies' => $this->getCookieJar()
            ])){
                return $client;
            }else{
                throw new Exception('Test login failed');
            }
        }else{
            throw new Exception('Csrf not enabled');
        }
    }
    public function testUsers()
    {
        $client = $this->getClientAndLogin();
        //actual test and 
    }
}

我的 phpunit.xml 文件:

<?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="var/bootstrap.php.cache"
>
    <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>

当我将setup()和getClientAndLogin()解压缩到同一文件夹中的单独文件和类"FunctionalShared"中时,找不到:

<?php
/**
 * Created by PhpStorm.
 * User: jochen
 * Date: 6/01/17
 * Time: 11:19 AM
 */
namespace TestsLegacyFunctional;
use SymfonyBundleFrameworkBundleTestKernelTestCase;
class FunctionalShared extends KernelTestCase
{
    protected function setUp()
    {
        ...
    protected function getCookieJar()
    {
        ...
    protected function getClientAndLogin()
    {
        ...
class UsersTest extends FunctionalShared
{
/usr/bin/php /tmp/ide-phpunit.php --bootstrap /home/jochen/projects/zog2017/system/var/bootstrap.php.cache --configuration /home/jochen/projects/zog2017/system/phpunit.xml TestsLegacyFunctionalUsersTest /home/jochen/projects/zog2017/system/Tests/Legacy/Functional/UsersTest.php
Testing started at 11:24 AM ...
PHP Fatal error:  Class 'TestsLegacyFunctionalFunctionalShared' not found in /home/jochen/projects/zog2017/system/Tests/Legacy/Functional/UsersTest.php on line 17
PHP Stack trace:
PHP   1. {main}() /tmp/ide-phpunit.php:0
PHP   2. IDE_Base_PHPUnit_TextUI_Command::main() /tmp/ide-phpunit.php:587
PHP   3. PHPUnit_TextUI_Command->run() /tmp/ide-phpunit.php:299
PHP   4. PHPUnit_Runner_BaseTestRunner->getTest() /usr/share/php/PHPUnit/TextUI/Command.php:149
PHP   5. PHPUnit_Runner_BaseTestRunner->loadSuiteClass() /usr/share/php/PHPUnit/Runner/BaseTestRunner.php:102
PHP   6. PHPUnit_Runner_StandardTestSuiteLoader->load() /usr/share/php/PHPUnit/Runner/BaseTestRunner.php:168
PHP   7. PHPUnit_Util_Fileloader::checkAndLoad() /usr/share/php/PHPUnit/Runner/StandardTestSuiteLoader.php:77
PHP   8. PHPUnit_Util_Fileloader::load() /usr/share/php/PHPUnit/Util/Fileloader.php:76
PHP   9. include_once() /usr/share/php/PHPUnit/Util/Fileloader.php:92
Process finished with exit code 255

UsersTest文件中,您需要在一开始使用 FunctionalShared 类:

<?php
...
use TestsLegacyFunctionalFunctionalShared;
class UsersTest extends FunctionalShared  
{
...

如果您查看本测试设置教程中的 #5,您可以看到有关如何正确扩展类的示例。

最新更新