How can I load fixtures from a functional test in Symfony 2

My DoctrineFixturesBundle is installed, and I can load the device through the command line, but how can I load devices from my functional test?

+18
symfony doctrine functional-testing fixture
Jun 13 '13 at 16:02
source share
4 answers

You can load fixtures into your test setUp() method, as you can see in this question .

You can use the code in the question, but you need to add --append to the doctrine:fixtures:load command to avoid confirmation using the instrument package.

The best solution is to take a look at the LiipFunctionalTestBundle , which simplifies the use of data transfer tools.

+14
Jun 13 '13 at 16:05
source share

If you use symfony WebTestCase , there is actually a very simple way to load your lights. Your device must implement FixtureInterface ; thus, the load() method can be called directly in your setUp() test. You just need to pass the EntityManager to the load() method, which can be obtained from the symfony container:

 public function setUp() { $client = static::createClient(); $container = $client->getContainer(); $doctrine = $container->get('doctrine'); $entityManager = $doctrine->getManager(); $fixture = new YourFixture(); $fixture->load($entityManager); } 
+15
Dec 05 '13 at 12:55
source share

I just wanted to offer a slightly tidier approach if you want to clear the table of previous test data first, for example. if you run your tests in phpunit.

 use Doctrine\Common\DataFixtures\Purger\ORMPurger; use Doctrine\Common\DataFixtures\Executor\ORMExecutor; use Doctrine\Common\DataFixtures\Loader; use Namespace\FakeBundle\DataFixtures\ORM\YourFixtures; public function setUp() { static::$kernel = static::createKernel(); static::$kernel->boot(); $this->em = static::$kernel->getContainer() ->get('doctrine') ->getManager() ; $loader = new Loader(); $loader->addFixture(new YourFixtures); $purger = new ORMPurger($this->em); $executor = new ORMExecutor($this->em, $purger); $executor->execute($loader->getFixtures()); parent::setUp(); } 

This allows you to load fixtures (you can click on the method of adding accessories more) and clear the tables before loading them. Also note that MongoDB has the same option as MongoDBPurger and MongoDBExecutor. Hope this helps someone.

+3
Feb 17 '14 at 2:59
source share

As already mentioned, it is recommended to use LiipFunctionalTestBundle. Then you want to extend your WebTestCase from Liip \ FunctionalTestBundle \ Test \ WebTestCase. This will allow $this->loadFixtures() be called, which takes an array of arguments as an argument.

 $fixtures = array('Acme\MemeberBundle\DataFixtures\ORM\LoadMemberData'); $this->loadFixtures($fixtures); 

For more details, I wrote a short blog page: http://marcjuch.li/blog/2014/04/06/symfony2-rest-functional-testing-with-fixtures/

+1
Apr 07 '14 at 21:18
source share



All Articles