Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I load fixtures from functional test in Symfony 2

My DoctrineFixturesBundle is installed and I can load fixture trough the command-line but , how can I load fixtures from my functional test ?

like image 495
Ousmane Avatar asked Jun 13 '13 16:06

Ousmane


2 Answers

If you use symfony's WebTestCase, there's actually a very easy way to load your fixtures. Your fixture has to implement the FixtureInterface; thus, you can call it's load() method directly in your test's setUp() method. You just have to pass an EntityManager to the load() method, which can be aquired 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); } 
like image 154
firstprophet Avatar answered Sep 28 '22 17:09

firstprophet


You can load the fixtures in your test's setUp() method as you can see in this question.

You can use the code in the question but need to append --appendto the doctrine:fixtures:load command in order to avoid the confirmation by the fixtures-bundle.

The better solution is to have a look at the LiipFunctionalTestBundle which makes using data-fixtures easier.

like image 31
Nicolai Fröhlich Avatar answered Sep 28 '22 19:09

Nicolai Fröhlich