为什么 PHPUnitTest WebTestCase 会考虑之前的测试?

Why PHPUnitTest WebTestCase takes into account previous test?

我有一个带有后端的 Symfony 3.2 项目。每个实体都有其 CRUD 控制器、视图等。我准备了一个 abstract class AbstractControllerTest extends WebTestCase 这是每个实体测试的基础。对于每个实体,我使用一个简单的测试来断言列出、显示、编辑和新建 returns HTTP 200.

所以当我运行 all test it test list, show etc for each Entity.问题是在列表控制器中,我使用默认顺序的 KNPPaginator。控制器工作正常,但是当我 运行 测试并且它到达第二个实体时,由于缺少实体字段,我收到 500 错误。事实证明,该测试从先前的测试中获取了寻呼机的列表查询。 因此,实体 A 默认使用位置字段进行排序。实体 B 没有位置字段并导致错误。因此,当 PHPUnit 去测试 A 实体时,它是 OK,然后它移动到测试 B 实体,然后出现错误。 我不知道发生了什么,因为排序没有保存在会话中,所以 PHPUnit 无法从先前实体的会话中获取查询。 有什么想法吗?

抽象控制器测试

abstract class AbstractControllerTest extends WebTestCase
{
    /** @var Client $client */
    public $client = null;

    protected $user = '';
    protected $prefix = '';
    protected $section = '';
    protected $entityId = '';

    public function setUp()
    {
        $this->client = $this->createAuthorizedClient();
    }

    /**
     * @return Client
     */
    protected function createAuthorizedClient()
    {
        $client = static::createClient();
        $client->setServerParameter('HTTP_HOST', $client->getContainer()->getParameter('test_info_domain'));
        $client->setServerParameter('HTTPS', true);
        $client->followRedirects();
        $container = $client->getContainer();

        $session = $container->get('session');
        /** @var $userManager \FOS\UserBundle\Doctrine\UserManager */
        $userManager = $container->get('fos_user.user_manager');
        /** @var $loginManager \FOS\UserBundle\Security\LoginManager */
        $loginManager = $container->get('fos_user.security.login_manager');
        $firewallName = $this->section;

        /** @var UserInterface $userObject */
        $userObject = $userManager->findUserBy(array('username' => $this->user));
        $loginManager->logInUser($firewallName, $userObject);

        // save the login token into the session and put it in a cookie
        $container->get('session')->set('_security_' . $firewallName,
            serialize($container->get('security.token_storage')->getToken()));
        $container->get('session')->save();
        $client->getCookieJar()->set(new Cookie($session->getName(), $session->getId()));

        return $client;
    }

    public function testIndex()
    {
        //CRUD index
        $this->client->request('GET', sprintf('/%s/%s',$this->section,$this->prefix));
        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
    }

    public function testShow()
    {
        //CRUD show
        $this->client->request('GET', sprintf('/%s/%s/%s/show',$this->section,$this->prefix, $this->entityId));
        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
    }

    public function testEdit()
    {
        //CRUD edit
        $this->client->request('GET', sprintf('/%s/%s/%s/edit',$this->section,$this->prefix, $this->entityId));
        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
    }

    public function testNew()
    {
        //CRUD new
        $this->client->request('GET', sprintf('/%s/%s/new',$this->section,$this->prefix));
        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
    }
}

以及一个实体的控制器class测试之一的示例

class AgendaCategoryControllerTest extends AbstractControllerTest
{
    protected $user = 'tom@test.com';
    protected $section = 'admin';
    protected $prefix = 'agenda-category';
    protected $entityId = '40';
}

如果我运行分开

php phpunit.phar src/Bundle/Tests/Controller/Admin/AControllerTest.php 

php phpunit.phar src/Bundle/Tests/Controller/Admin/BControllerTest.php 

没关系。 如果运行一起出现这个奇怪的bug

php phpunit.phar -c phpunit.xml.dist --testsuite=Admin

您可以通过在设置方法中执行以下操作在测试之间重置测试客户端:

public function setUp()
{
    $this->client = $this->createAuthorizedClient();

    $this->client->restart();
}

您可能必须将重新启动移动到您的 createAuthorizedClient 方法中,以确保它不会重置您的授权信息。