Skip to content

Instantly share code, notes, and snippets.

@jakzal
Last active June 27, 2024 16:20
Show Gist options
  • Save jakzal/1319290 to your computer and use it in GitHub Desktop.
Save jakzal/1319290 to your computer and use it in GitHub Desktop.
KernelAwareTest
<?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();
}
}
@draev
Copy link

draev commented Feb 25, 2014

Would be great if not hardcoded em dependency :)
Not all the world use Doctrine :P

@krzysiekpiasecki
Copy link

First call parent __construct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment