共享测试设置时找不到类

时间:2017-01-05 22:25:29

标签: symfony phpunit functional-testing

我正在使用phpunit进行一些功能测试并在Symfony 3.2应用程序中使用。

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

<?php

namespace Tests\Legacy\Functional;

use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

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()提取到一个单独的文件和类&#34; FunctionalShared&#34;在同一个文件夹中,找不到:

<?php
/**
 * Created by PhpStorm.
 * User: jochen
 * Date: 6/01/17
 * Time: 11:19 AM
 */

namespace Tests\Legacy\Functional;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

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 Tests\Legacy\Functional\UsersTest /home/jochen/projects/zog2017/system/Tests/Legacy/Functional/UsersTest.php
Testing started at 11:24 AM ...
PHP Fatal error:  Class 'Tests\Legacy\Functional\FunctionalShared' 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

1 个答案:

答案 0 :(得分:0)

UsersTest文件中,您需要在开头使用FunctionalShared课程:

<?php
...
use Tests\Legacy\Functional\FunctionalShared;

class UsersTest extends FunctionalShared  
{
...

如果您查看此Testing Setup Tutorial中的#5,您可以看到有关如何正确扩展课程的示例。