Last active
June 27, 2024 16:20
-
-
Save jakzal/1319290 to your computer and use it in GitHub Desktop.
KernelAwareTest
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 | |
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
First call parent __construct.