Skip to content

Instantly share code, notes, and snippets.

@beherca
Forked from jakzal/Doctrine.php
Created November 30, 2015 19:08
Show Gist options
  • Save beherca/0a1a58d62f110ecfc7ca to your computer and use it in GitHub Desktop.
Save beherca/0a1a58d62f110ecfc7ca to your computer and use it in GitHub Desktop.
Use Doctrine and Symfony Kernel in phpunit tests
<?php
namespace Zalas\Test;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\Tools\SchemaTool;
trait Doctrine
{
/**
* @var ManagerRegistry
*/
protected $doctrine;
/**
* @var ObjectManager
*/
protected $manager;
/**
* @before
*/
protected function setUpDoctrine()
{
$this->doctrine = $this->createDoctrineRegistry();
$this->manager = $this->doctrine->getManager();
}
/**
* @before
*/
protected function createSchema()
{
if ($metadata = $this->getMetadata()) {
$schemaTool = new SchemaTool($this->manager);
$schemaTool->dropSchema($metadata);
$schemaTool->createSchema($metadata);
}
}
/**
* 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()
{
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()
{
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__));
}
}
<?php
namespace Repository\Tests;
use Zalas\Test\Doctrine;
use Zalas\Test\SymfonyKernel;
class ProductsTest extends \PHPUnit_Framework_TestCase
{
use SymfonyKernel;
use Doctrine;
public function testIt()
{
$doctrine = $this->doctrine;
$manager = $this->manager;
$kernel = $this->kernel;
$container = $this->container;
}
}
<?php
namespace Zalas\Test;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Kernel;
/**
* The kernel class needs to be autoloaded in order for this to work.
* @see https://github.com/symfony/symfony-standard/commit/ab358a4458b052fc80a3e99a93b125c1ef133a38
*/
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();
}
/**
* @param array $options
*
* @return Kernel
*/
protected function createKernel()
{
$class = $this->getKernelClass();
$options = $this->getKernelOptions();
return new $class(
isset($options['environment']) ? $options['environment'] : 'test',
isset($options['debug']) ? $options['debug'] : true
);
}
/**
* @return string
*/
protected function getKernelClass()
{
return \AppKernel::class;
}
/**
* @return string
*/
protected function getKernelOptions()
{
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