Search code examples
symfonydoctrine-ormliiptestfixturesbundle

excluding tables with setExcludedDoctrineTables when loading fixtures with liip TestFixturesBundle


I'm trying to use Liip test bundle to load fixtures from code using the loadFixtures() method from FixturesTrait However, I need to exclude the RESOURCE table that I don't want to be dropped in the process. If I understand correctly, this should be easy using the setExcludedDoctrineTables method according to the doc https://github.com/liip/LiipTestFixturesBundle/blob/master/doc/database.md

Unfortunately, when I run this code, the table RESOURCES gets dropped with all the others.

Can anybody see why ?

  • not sure if that's relevant but I'm using a mysql db in a separate docker container.

      <?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');
              }
          }
    

    }


Solution

  • Answering my own question as I just found out the issue. Sometimes a good night of sleep is all you need :)

    So this code is actually working. The table RESOURCES got dropped because the setUp method was redefined in the concrete class implementing the test and that redefinition included another call to loadFixtures, but for a different data class and without the exclusion :

    <?php
    
    namespace App\Tests\Controller;
    
    use App\DataFixtures\DiscountFixtures;
    
    class DiscountControllerWebTest extends AbstractPantherTest
    {
    
        function setUp():void
        {
            parent::setUp();
    
            $this->loadFixtures([
                DiscountFixtures::class,
            ]);
    
        }