Skip to content

Instantly share code, notes, and snippets.

@piwi91
Created September 14, 2015 10:44
Show Gist options
  • Save piwi91/f2630647bba5bcfc6164 to your computer and use it in GitHub Desktop.
Save piwi91/f2630647bba5bcfc6164 to your computer and use it in GitHub Desktop.
KernelAndDatabaseAwareTest Symfony2 Doctrine
<?php
namespace Company\Core\Tests;
use Doctrine\ORM\Tools\SchemaTool;
/**
* 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 KernelAndDatabaseAwareTest extends KernelAwareTest
{
/**
* @return null
*/
public function setUp()
{
parent::setUp();
$this->entityManager = $this->container->get('doctrine')->getManager();
$this->generateSchema();
}
/**
* @return null
*/
protected function generateSchema()
{
$metadatas = $this->getMetadatas();
if (!empty($metadatas)) {
$tool = new SchemaTool($this->entityManager);
$tool->dropSchema($metadatas);
$tool->createSchema($metadatas);
}
}
/**
* @return array
*/
protected function getMetadatas()
{
return $this->entityManager->getMetadataFactory()->getAllMetadata();
}
}
<?php
namespace Company\Core\Tests;
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 \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();
parent::setUp();
}
/**
* @return null
*/
public function tearDown()
{
$this->kernel->shutdown();
parent::tearDown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment