Last active
September 14, 2020 13:19
-
-
Save jakzal/a24467c2e57d835dcb65 to your computer and use it in GitHub Desktop.
Use Doctrine and Symfony Kernel in phpunit tests
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 | |
declare(strict_types=1); | |
namespace SymfonyLive\Mastermind\Adapters\Database; | |
use Doctrine\Common\Persistence\ManagerRegistry; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use Doctrine\ORM\Tools\SchemaTool; | |
use RuntimeException; | |
trait Doctrine | |
{ | |
/** | |
* @var ManagerRegistry | |
*/ | |
protected $doctrine; | |
/** | |
* @var ObjectManager | |
*/ | |
protected $manager; | |
/** | |
* @before | |
*/ | |
protected function createSchema() | |
{ | |
if ($metadata = $this->getMetadata()) { | |
$schemaTool = new SchemaTool($this->manager); | |
$schemaTool->dropSchema($metadata); | |
$schemaTool->createSchema($metadata); | |
} | |
} | |
/** | |
* @before | |
*/ | |
protected function setUpDoctrine() | |
{ | |
$this->doctrine = $this->createDoctrineRegistry(); | |
$this->manager = $this->doctrine->getManager(); | |
} | |
/** | |
* Returns all metadata by default. | |
* | |
* Override to only build selected metadata. | |
* Return an empty array to prevent building the schema. | |
* | |
* @return array | |
*/ | |
protected function getMetadata(): array | |
{ | |
return $this->manager->getMetadataFactory()->getAllMetadata(); | |
} | |
/** | |
* Override to build doctrine registry yourself. | |
* | |
* By default a Symfony container is used to create it. It requires the SymfonyKernel trait. | |
* | |
* @return ManagerRegistry | |
*/ | |
protected function createDoctrineRegistry(): ManagerRegistry | |
{ | |
if (isset($this->kernel)) { | |
return $this->kernel->getContainer()->get('doctrine'); | |
} | |
throw new RuntimeException(sprintf('Override %s to create a ManagerRegistry or use the SymfonyKernel trait.', __METHOD__)); | |
} | |
} |
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 | |
declare(strict_types=1); | |
namespace SymfonyLive\Mastermind\Adapters\Database; | |
use SymfonyLive\Mastermind\Game\DecodingBoards; | |
use SymfonyLive\Mastermind\Game\DecodingBoardsTestCase; | |
/** | |
* @group integration | |
*/ | |
class DoctrineDecodingBoardsTest extends DecodingBoardsTestCase | |
{ | |
use Doctrine; | |
use SymfonyKernel; | |
protected function createDecodingBoards(): DecodingBoards | |
{ | |
return new DoctrineDecodingBoards($this->manager); | |
} | |
} |
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 | |
declare(strict_types=1); | |
namespace SymfonyLive\Mastermind\Adapters\Database; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Symfony\Component\HttpKernel\Kernel; | |
use SymfonyLive\Mastermind\Adapters\Web\Kernel as AppKernel; | |
trait SymfonyKernel | |
{ | |
/** | |
* @var Kernel | |
*/ | |
protected $kernel; | |
/** | |
* @var ContainerInterface | |
*/ | |
protected $container; | |
/** | |
* @before | |
*/ | |
protected function setUpSymfonyKernel() | |
{ | |
$this->kernel = $this->createKernel(); | |
$this->kernel->boot(); | |
$this->container = $this->kernel->getContainer(); | |
} | |
protected function createKernel(): Kernel | |
{ | |
$class = $this->getKernelClass(); | |
$options = $this->getKernelOptions(); | |
return new $class( | |
isset($options['environment']) ? $options['environment'] : 'test', | |
isset($options['debug']) ? $options['debug'] : true | |
); | |
} | |
protected function getKernelClass(): string | |
{ | |
return AppKernel::class; | |
} | |
protected function getKernelOptions(): array | |
{ | |
return ['environment' => 'test', 'debug' => true]; | |
} | |
/** | |
* @after | |
*/ | |
protected function tearDownSymfonyKernel() | |
{ | |
if (null !== $this->kernel) { | |
$this->kernel->shutdown(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment