PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 16, 2022

[FIXED] excluding tables with setExcludedDoctrineTables when loading fixtures with liip TestFixturesBundle

 February 16, 2022     doctrine-orm, liiptestfixturesbundle, symfony     No comments   

Issue

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,
        ]);

    }


Answered By - Marc Salvetti
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing