Created
September 14, 2015 10:44
-
-
Save piwi91/f2630647bba5bcfc6164 to your computer and use it in GitHub Desktop.
KernelAndDatabaseAwareTest Symfony2 Doctrine
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 | |
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(); | |
} | |
} |
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 | |
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