UPDATED RESPONSE
You can create a base class for your test cases, which simplifies instrument loading using some classes from the Doctrine Data Fixtures library . This class will look something like this:
<?php use Doctrine\Common\DataFixtures\Executor\ORMExecutor; use Doctrine\Common\DataFixtures\FixtureInterface; use Doctrine\Common\DataFixtures\Purger\ORMPurger; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; abstract class FixtureAwareTestCase extends KernelTestCase { private $fixtureExecutor; private $fixtureLoader; public function setUp() { self::bootKernel(); } protected function addFixture(FixtureInterface $fixture) { $this->getFixtureLoader()->addFixture($fixture); } protected function executeFixtures() { $this->getFixtureExecutor()->execute($this->getFixtureLoader()->getFixtures()); } private function getFixtureExecutor() { if (!$this->fixtureExecutor) { $entityManager = self::$kernel->getContainer()->get('doctrine')->getManager(); $this->fixtureExecutor = new ORMExecutor($entityManager, new ORMPurger($entityManager)); } return $this->fixtureExecutor; } private function getFixtureLoader() { if (!$this->fixtureLoader) { $this->fixtureLoader = new ContainerAwareLoader(self::$kernel->getContainer()); } return $this->fixtureLoader; } }
Then, in your test case, just add the class above and add all the necessary devices before the test and run them. This will automatically clear your database before loading fixtures. Example:
class MyTestCase extends FixtureAwareTestCase { public function setUp() { parent::setUp();
OLD ANSWER
(I decided to βtrickβ this answer because it explains how to clear the database without specifying how to load the snap-ins after that).
There is an even cleaner way to accomplish this without having to run commands. This basically consists of using a combination of SchemaTool and ORMPurger. You can create an abstract base class that performs such operations in order to avoid repeating them for each specialized test case. Here is a sample test class code that sets up a database for a common test case:
use Doctrine\Common\DataFixtures\Purger\ORMPurger; use Doctrine\ORM\Tools\SchemaTool; abstract class DatabaseAwareWebTestCase extends WebTestCase { public static function setUpBeforeClass() { parent::setUpBeforeClass(); $kernel = static::createKernel(); $kernel->boot(); $em = $kernel->getContainer()->get('doctrine')->getManager(); $schemaTool = new SchemaTool($em); $metadata = $em->getMetadataFactory()->getAllMetadata();
Thus, before starting each test case that inherits from the above class, the database schema will be restored from scratch and then cleared after each test run.
Hope this helps.
Andrea Sprega Mar 02 '14 at 19:53 2014-03-02 19:53
source share