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

时间:2017-04-13 09:00:12

标签: symfony phpunit knppaginator

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

因此,当我运行所有测试时,测试列表,显示每个实体等。问题是在列表控制器中我使用默认顺序的KNPPaginator。控制器工作正常,但是当我运行测试并且它到达第二个实体时,由于缺少实体字段,我得到500错误。事实证明,测试从先前的测试中获取了一个查询寻呼机的列表。 因此,实体A默认使用位置字段进行排序。实体B没有位置字段并导致错误。因此,当PHPUnit去测试一个实体时它没关系,然后它转移到测试B实体,然后出现错误。 我不知道发生了什么,因为订单没有保存在会话中,所以PHPUnit无法从之前的Entity的会话中获取查询。 有什么想法发生了什么?

AbstractControllerTest

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());
    }
}

一个实体的Controller测试类之一的示例

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 

没关系。 如果一起运行就有这个奇怪的错误

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

1 个答案:

答案 0 :(得分:1)

您可以在setUp-method中执行以下操作,在测试之间重置测试客户端:

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

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

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