Last active
November 8, 2018 14:36
-
-
Save adri/e217ac182a1bfedf71eb00732237c5da to your computer and use it in GitHub Desktop.
Symfony TestCase using fixtures and database transactions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Doctrine\Bundle\DoctrineBundle\Registry; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use Doctrine\DBAL\Connection; | |
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | |
class FixtureTestCase extends KernelTestCase | |
{ | |
/** | |
* @var Registry | |
*/ | |
protected $doctrine; | |
/** | |
* @var ObjectManager | |
*/ | |
protected $manager; | |
/** | |
* @var Connection | |
*/ | |
protected $db; | |
public function setUp() : void | |
{ | |
static $kernel; | |
if (null === $kernel || null === $kernel->getContainer()) { | |
$kernel = self::bootKernel(); | |
} | |
$this->doctrine = $kernel->getContainer()->get('doctrine'); | |
$this->manager = $this->doctrine->getManager(); | |
$this->db = $kernel->getContainer()->get('database_connection'); | |
$this->db->beginTransaction(); | |
} | |
/** | |
* @throws \Doctrine\DBAL\ConnectionException | |
*/ | |
public function tearDown() : void | |
{ | |
$this->db->rollBack(); | |
} | |
protected function factory() : Factory | |
{ | |
return Factory::create($this->manager); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment