Skip to content

Instantly share code, notes, and snippets.

@tinker1987
Last active September 1, 2017 14:00
Show Gist options
  • Save tinker1987/bdc62bbc4c4d57f4fba3eef5c31f805a to your computer and use it in GitHub Desktop.
Save tinker1987/bdc62bbc4c4d57f4fba3eef5c31f805a to your computer and use it in GitHub Desktop.
Allow to load fixtures for integration tests
<?php
declare(strict_types=1);
namespace Tests;
use Doctrine\Common\DataFixtures\{
Executor\ORMExecutor, FixtureInterface, Purger\ORMPurger, SharedFixtureInterface
};
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader;
/**
* Class FixtureAwareTestCaseTrait
* @package Tests
* @author Dmytro Paiareli <dmitriy.payareli@gmail.com>
*/
trait FixtureAwareTestCaseTrait
{
/**
* @var ORMExecutor
*/
private $fixtureExecutor;
/**
* @var ContainerAwareLoader
*/
private $fixtureLoader;
/**
* @param FixtureInterface $fixture
* @return self
*/
protected function addFixture(FixtureInterface $fixture)
{
$this->getFixtureLoader()->addFixture($fixture);
return $this;
}
/**
* Executes all the fixtures that have been loaded so far.
* @param bool $append
*/
protected function executeFixtures(bool $append = false)
{
$this->getFixtureExecutor()->execute($this->getFixtureLoader()->getFixtures(), $append);
}
/**
* Purges loaded fixtures from DB
*/
protected function purgeFixtures()
{
$this->getFixtureExecutor()->getPurger()->purge();
}
/**
* @return ORMExecutor
*/
private function getFixtureExecutor()
{
if (!$this->fixtureExecutor) {
/**
* @var \Doctrine\ORM\EntityManager $entityManager
*/
$entityManager = self::$kernel->getContainer()->get('doctrine')->getManager();
$this->fixtureExecutor = new class ($entityManager, new ORMPurger($entityManager)) extends ORMExecutor
{
/**
* @inheritDoc
*/
public function load(ObjectManager $manager, FixtureInterface $fixture)
{
if ($fixture instanceof SharedFixtureInterface) {
$fixture->setReferenceRepository($this->referenceRepository);
}
$fixture->load($manager);
}
};
}
return $this->fixtureExecutor;
}
/**
* @return ContainerAwareLoader
*/
private function getFixtureLoader()
{
if (!$this->fixtureLoader) {
$this->fixtureLoader = new ContainerAwareLoader(self::$kernel->getContainer());
}
return $this->fixtureLoader;
}
}
@tinker1987
Copy link
Author

tinker1987 commented Aug 24, 2017

Test case class should extends Symfony\Bundle\FrameworkBundle\Test\KernelTestCase to able get access to service container.

class FooTest extends Symfony\Bundle\FrameworkBundle\Test\KernelTestCase
{
    use Tests\FixtureAwareTestCaseTrait;

    public function setUp()
    {
        self::bootKernel();

        // Base fixture for all tests
        $this
                ->addFixture(new FirstFixture())
                ->addFixture(new SecondFixture())
                ->executeFixtures();

        // Fixtures are now loaded in a clean DB
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment