【问题标题】:Why PHPUnitTest WebTestCase takes into account previous test?为什么 PHPUnitTest WebTestCase 会考虑之前的测试?
【发布时间】:2017-09-09 07:31:15
【问题描述】:

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

因此,当我运行所有测试时,它会为每个实体测试列表、显示等。问题是在控制器列表中我使用默认顺序的 KNPPaginator。控制器工作正常,但是当我运行测试并到达第二个实体时,由于缺少实体字段,我收到 500 错误。事实证明,该测试从先前的测试中获取了一个 List Query for Pager。 因此,默认情况下,实体 A 使用位置字段进行排序。实体 B 没有位置字段,这会导致错误。因此,当 PHPUnit 去测试 A 实体时它是好的,然后它移动到测试 B 实体然后出现错误。 我不知道发生了什么,因为排序未保存在会话中,因此 PHPUnit 无法从先前实体的会话中获取查询。 有什么想法吗?

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

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

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

【问题讨论】:

  • 不得链接测试。如果是这样,它们必须在同一个测试用例中。
  • 什么意思?一个控制器测试对 index show edit new 有单独的测试。对于每个实体,我使用不同的 ControllerTest 类
  • 如果没有测试,很难看出你想做什么。
  • 添加代码和更多细节

标签: symfony phpunit knppaginator


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2016-07-19
    • 2011-04-26
    • 2013-10-13
    • 2017-05-17
    • 2021-09-05
    • 2016-05-31
    • 1970-01-01
    • 2016-10-13
    • 2018-05-14
    相关资源
    最近更新 更多