Skip to content

Instantly share code, notes, and snippets.

@adri
Last active November 8, 2018 14:36
Show Gist options
  • Save adri/e217ac182a1bfedf71eb00732237c5da to your computer and use it in GitHub Desktop.
Save adri/e217ac182a1bfedf71eb00732237c5da to your computer and use it in GitHub Desktop.
Symfony TestCase using fixtures and database transactions
<?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