1

我正在尝试使用 Liip 测试包使用 FixturesTrait 中的 loadFixtures() 方法从代码中加载夹具但是,我需要排除我不想在此过程中删除的 RESOURCE 表。如果我理解正确,这应该很容易根据文档https://github.com/liip/LiipTestFixturesBundle/blob/master/doc/database.md使用 setExcludedDoctrineTables 方法

不幸的是,当我运行此代码时,表 RESOURCES 与所有其他表一起被删除。

任何人都可以看到为什么?

  • 不确定这是否相关,但我在单独的 docker 容器中使用 mysql db。

      <?php
    
      namespace App\Tests\Controller;
    
      use Symfony\Component\Panther\PantherTestCase;
      use Symfony\Component\Panther\Client;
      use Facebook\WebDriver\WebDriverBy as By;
      use Facebook\WebDriver\Exception\TimeoutException;
      use Liip\FunctionalTestBundle\Test\WebTestCase;
      use Symfony\Component\Panther\PantherTestCaseTrait;
      use Liip\TestFixturesBundle\Test\FixturesTrait;
    
      use App\Repository\UserRepository;
      use App\DataFixtures\UserFixtures;
      use App\DataFixtures\AccountFixtures;
    
      abstract class AbstractPantherTest extends WebTestCase{    
          // use trait so we can combine Liip and Panther features
          use PantherTestCaseTrait; // this is the magic. Panther is now available.
    
          // provide fixtures loading feature
          use FixturesTrait;
    
          // @var Symfony\Component\Panther\Client
          protected static $client;
    
          //Initialize the test case
          function setUp():void
          {
              static::bootKernel();
              if(self::$client === null){    
                  self::$client = self::createPantherClient(['browser' => PantherTestCase::FIREFOX]);
    
                  $this->setExcludedDoctrineTables(["RESOURCES"]);
                  $this->loadFixtures([
                      UserFixtures::class,
                  ]);
                  // retrieve the test user
                  $userRepository = static::$container->get(UserRepository::class);
    
                  // retrieve the test user
                  $testUser = $userRepository->findOneByUsername('Administrator');
    
                  // simulate $testUser being logged in
                  self::doLogin($testUser->getUsername(), 'xxx');
              }
          }
    

    }

4

1 回答 1

1

在我刚刚发现问题时回答我自己的问题。有时,您只需要一夜好眠 :)

所以这段代码实际上是有效的。表 RESOURCES 被删除,因为在实现测试的具体类中重新定义了 setUp 方法,并且重新定义包括对 loadFixtures 的另一个调用,但是对于不同的数据类并且没有排除:

<?php

namespace App\Tests\Controller;

use App\DataFixtures\DiscountFixtures;

class DiscountControllerWebTest extends AbstractPantherTest
{

    function setUp():void
    {
        parent::setUp();

        $this->loadFixtures([
            DiscountFixtures::class,
        ]);

    }
于 2020-12-19T08:59:34.553 回答