Skip to content

Instantly share code, notes, and snippets.

@gtrias
Forked from jakzal/KernelAwareTest.php
Last active August 29, 2015 14:12
Show Gist options
  • Save gtrias/bfb6c27d73be56f4bb5e to your computer and use it in GitHub Desktop.
Save gtrias/bfb6c27d73be56f4bb5e to your computer and use it in GitHub Desktop.
<?php
require_once dirname(__DIR__).'/../../../../app/AppKernel.php';
/**
* Test case class helpful with Entity tests requiring the database interaction.
* For regular entity tests it's better to extend standard \PHPUnit_Framework_TestCase instead.
*/
abstract class KernelAwareTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Symfony\Component\HttpKernel\AppKernel
*/
protected $kernel;
/**
* @var Doctrine\ORM\EntityManager
*/
protected $entityManager;
/**
* @var Symfony\Component\DependencyInjection\Container
*/
protected $container;
/**
* @return null
*/
public function setUp()
{
$this->kernel = new \AppKernel('test', true);
$this->kernel->boot();
$this->container = $this->kernel->getContainer();
$this->entityManager = $this->container->get('doctrine')->getManager();
$this->generateSchema();
parent::setUp();
}
/**
* @return null
*/
public function tearDown()
{
$this->kernel->shutdown();
parent::tearDown();
}
/**
* @return null
*/
protected function generateSchema()
{
$metadatas = $this->getMetadatas();
if (!empty($metadatas)) {
$tool = new \Doctrine\ORM\Tools\SchemaTool($this->entityManager);
$tool->dropSchema($metadatas);
$tool->createSchema($metadatas);
}
}
/**
* @return array
*/
protected function getMetadatas()
{
return $this->entityManager->getMetadataFactory()->getAllMetadata();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment