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.

Revisions

  1. tinker1987 revised this gist Sep 1, 2017. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions FixtureAwareTestCaseTrait.php
    Original file line number Diff line number Diff line change
    @@ -3,10 +3,9 @@

    namespace Tests;

    use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
    use Doctrine\Common\DataFixtures\FixtureInterface;
    use Doctrine\Common\DataFixtures\Purger\ORMPurger;
    use Doctrine\Common\DataFixtures\SharedFixtureInterface;
    use Doctrine\Common\DataFixtures\{
    Executor\ORMExecutor, FixtureInterface, Purger\ORMPurger, SharedFixtureInterface
    };
    use Doctrine\Common\Persistence\ObjectManager;
    use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader;

    @@ -28,7 +27,6 @@ trait FixtureAwareTestCaseTrait
    private $fixtureLoader;

    /**
    * Adds a new fixture to be loaded.
    * @param FixtureInterface $fixture
    * @return self
    */
    @@ -40,10 +38,11 @@ protected function addFixture(FixtureInterface $fixture)

    /**
    * Executes all the fixtures that have been loaded so far.
    * @param bool $append
    */
    protected function executeFixtures()
    protected function executeFixtures(bool $append = false)
    {
    $this->getFixtureExecutor()->execute($this->getFixtureLoader()->getFixtures());
    $this->getFixtureExecutor()->execute($this->getFixtureLoader()->getFixtures(), $append);
    }

    /**
  2. tinker1987 revised this gist Sep 1, 2017. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions FixtureAwareTestCaseTrait.php
    Original file line number Diff line number Diff line change
    @@ -46,6 +46,14 @@ protected function executeFixtures()
    $this->getFixtureExecutor()->execute($this->getFixtureLoader()->getFixtures());
    }

    /**
    * Purges loaded fixtures from DB
    */
    protected function purgeFixtures()
    {
    $this->getFixtureExecutor()->getPurger()->purge();
    }

    /**
    * @return ORMExecutor
    */
  3. tinker1987 revised this gist Aug 30, 2017. 1 changed file with 17 additions and 1 deletion.
    18 changes: 17 additions & 1 deletion FixtureAwareTestCaseTrait.php
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,8 @@
    use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
    use Doctrine\Common\DataFixtures\FixtureInterface;
    use Doctrine\Common\DataFixtures\Purger\ORMPurger;
    use Doctrine\Common\DataFixtures\SharedFixtureInterface;
    use Doctrine\Common\Persistence\ObjectManager;
    use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader;

    /**
    @@ -54,7 +56,21 @@ private function getFixtureExecutor()
    * @var \Doctrine\ORM\EntityManager $entityManager
    */
    $entityManager = self::$kernel->getContainer()->get('doctrine')->getManager();
    $this->fixtureExecutor = new ORMExecutor($entityManager, new ORMPurger($entityManager));
    $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;
    }
  4. tinker1987 revised this gist Aug 25, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion FixtureAwareTestCaseTrait.php
    Original file line number Diff line number Diff line change
    @@ -27,12 +27,13 @@ trait FixtureAwareTestCaseTrait

    /**
    * Adds a new fixture to be loaded.
    *
    * @param FixtureInterface $fixture
    * @return self
    */
    protected function addFixture(FixtureInterface $fixture)
    {
    $this->getFixtureLoader()->addFixture($fixture);
    return $this;
    }

    /**
  5. tinker1987 created this gist Aug 24, 2017.
    71 changes: 71 additions & 0 deletions FixtureAwareTestCaseTrait.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    <?php
    declare(strict_types=1);

    namespace Tests;

    use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
    use Doctrine\Common\DataFixtures\FixtureInterface;
    use Doctrine\Common\DataFixtures\Purger\ORMPurger;
    use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader;

    /**
    * Class FixtureAwareTestCaseTrait
    * @package Tests
    * @author Dmytro Paiareli <[email protected]>
    */
    trait FixtureAwareTestCaseTrait
    {
    /**
    * @var ORMExecutor
    */
    private $fixtureExecutor;

    /**
    * @var ContainerAwareLoader
    */
    private $fixtureLoader;

    /**
    * Adds a new fixture to be loaded.
    *
    * @param FixtureInterface $fixture
    */
    protected function addFixture(FixtureInterface $fixture)
    {
    $this->getFixtureLoader()->addFixture($fixture);
    }

    /**
    * Executes all the fixtures that have been loaded so far.
    */
    protected function executeFixtures()
    {
    $this->getFixtureExecutor()->execute($this->getFixtureLoader()->getFixtures());
    }

    /**
    * @return ORMExecutor
    */
    private function getFixtureExecutor()
    {
    if (!$this->fixtureExecutor) {
    /**
    * @var \Doctrine\ORM\EntityManager $entityManager
    */
    $entityManager = self::$kernel->getContainer()->get('doctrine')->getManager();
    $this->fixtureExecutor = new ORMExecutor($entityManager, new ORMPurger($entityManager));
    }
    return $this->fixtureExecutor;
    }

    /**
    * @return ContainerAwareLoader
    */
    private function getFixtureLoader()
    {
    if (!$this->fixtureLoader) {
    $this->fixtureLoader = new ContainerAwareLoader(self::$kernel->getContainer());
    }
    return $this->fixtureLoader;
    }
    }