Symfony 共享测试设置时未找到类

Symfony 共享测试设置时未找到类,symfony,phpunit,functional-testing,Symfony,Phpunit,Functional Testing,我正在symfony3.2应用程序中使用phpunit和guzzle进行一些功能测试 我有多个测试需要在运行测试之前加载数据库并登录到应用程序。设置如下: <?php namespace Tests\Legacy\Functional; use GuzzleHttp\Client; use GuzzleHttp\Cookie\CookieJar; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; class UsersTe

我正在symfony3.2应用程序中使用phpunit和guzzle进行一些功能测试

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

<?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 
    }
}

UsersTest
文件中,您需要在一开始就使用
功能共享
类:

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

class UsersTest extends FunctionalShared  
{
...

我把它放在那里了,很抱歉没有把它正确地复制到我的问题上。但是,我仍然无法在测试文件夹中运行它。但是,当我将整个设置复制到AppBundle/src/Tests时,它会工作哦,好的。那么问题可能与Symfony如何组织其bundle有关(有点晚了)如何管理加载类?您的测试区域类是否有某种类型的自动加载器?是的,只有Symfony 3.2自动加载器。就我个人而言,我从未使用过这些自动加载器(尽管我只是查阅了一些源代码——除了一件小事,那里没有什么特别的),此外,从您的代码示例中不清楚自动加载器在何时何地运行以及如何配置。但很明显,它不知道
Tests\Legacy\Functional\Functional在应该的时候共享了
。不过,如果您声称
Tests\Legacy\Functional\UsersTest
确实有效(未扩展),这意味着autoloader知道
Tests\Legacy\Functional
名称空间,我猜名称空间声明或文件名/路径中都缺少拼写或smth
<?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
<?php
...
use Tests\Legacy\Functional\FunctionalShared;

class UsersTest extends FunctionalShared  
{
...