30

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

Ousmane
  • 2,673
  • 3
  • 30
  • 37
  • did my answer resolve your question? either load the fixtures in the setUp() method as in the question i hinted to or use the LiipFunctionalTestBundle. – Nicolai Fröhlich Jun 13 '13 at 16:43
  • @nifr Thanks it fit my needs, I used the solution in the question you hinted... I will consider the benefits of the LiipFunctionalTestBundle... – Ousmane Jun 14 '13 at 09:12

4 Answers4

27

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);
}
Amine Jallouli
  • 3,919
  • 8
  • 36
  • 73
firstprophet
  • 271
  • 3
  • 3
  • 2
    This misses all ReferenceRepository stuff - addRef/getRef – vp_arth Nov 22 '17 at 10:07
  • Do not forget to add reference repository before the load call. `$fixture->setReferenceRepository(new ReferenceRepository($entityManager));` – LugiHaue Nov 11 '18 at 17:51
18

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.

Community
  • 1
  • 1
Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130
13

I just wanted to offer a slightly neater approach if you want to first purge your table of previous test data, e.g. if you are running 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 fixtures to be loaded, (you can push more into the add fixture method), and purge the tables before they are loaded. Also note MongoDB has the same option using MongoDBPurger, and MongoDBExecutor. Hope it helps someone

Peter
  • 371
  • 3
  • 7
  • 1
    Hi, I'm trying to use your approach but `$executor` intent to delete the whole database and not just the related tables, why is that behavior? Any example in how to purge the generated data during fixtures load? – ReynierPM Mar 25 '14 at 14:14
2

As it was already mentioned it's recommended to use the LiipFunctionalTestBundle. Then you want extend your WebTestCase from the Liip\FunctionalTestBundle\Test\WebTestCase. This will allow to call $this->loadFixtures() which takes an array of fixtures as an argument.

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

For more details I wrote a short blogpost.

cezar
  • 11,616
  • 6
  • 48
  • 84
Marc Juchli
  • 2,240
  • 24
  • 20