Skip to content

Instantly share code, notes, and snippets.

@devhelp
Last active December 18, 2015 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devhelp/5809205 to your computer and use it in GitHub Desktop.
Save devhelp/5809205 to your computer and use it in GitHub Desktop.
Test case for testing database queries, based on WebTestCase, doctrine tasks and using LoaderService.php (browse https://gist.github.com/devhelp for it)
<?php
namespace Devhelp\Bundle\TestBundle\Test;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\Tools\SchemaTool;
/**
* @package devhelp/test-bundle
* @author Paweł Barański <pawel.baranski1@gmail.com>
*/
class DbTestCase extends WebTestCase
{
protected function setUp()
{
static::$kernel = static::createKernel();
static::$kernel->boot();
$this->dropDatabase();
$this->createDatabase();
//this may be overkill, but without shutdown/boot it does not work
//simpliest solution, as said - may not be most optimal
static::$kernel->shutdown();
static::$kernel->boot();
$this->updateSchema();
}
protected function getDoctrineConnection($name = null)
{
return $this->getContainer()->get('doctrine')->getConnection($name);
}
protected function getEntityManager()
{
return $this->getContainer()->get('doctrine')->getEntityManager();
}
protected function dropDatabase()
{
$connection = $this->getDoctrineConnection();
$params = $connection->getParams();
$name = isset($params['path']) ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false);
if (!isset($params['path'])) {
$name = $connection->getDatabasePlatform()->quoteSingleIdentifier($name);
}
if (!$name) {
throw new \InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped.");
}
try {
$connection->getSchemaManager()->dropDatabase($name);
} catch(\Exception $e) {}
}
protected function createDatabase()
{
$connection = $this->getDoctrineConnection();
$params = $connection->getParams();
$name = isset($params['path']) ? $params['path'] : $params['dbname'];
unset($params['dbname']);
$tmpConnection = DriverManager::getConnection($params);
// Only quote if we don't have a path
if (!isset($params['path'])) {
$name = $tmpConnection->getDatabasePlatform()->quoteSingleIdentifier($name);
}
$tmpConnection->getSchemaManager()->createDatabase($name);
$tmpConnection->close();
}
protected function updateSchema()
{
$em = $this->getEntityManager();
$metadatas = $em->getMetadataFactory()->getAllMetadata();
if (!empty($metadatas)) {
$schemaTool = new SchemaTool($em);
$schemaTool->updateSchema($metadatas, true);
}
}
/**
* @return \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected function getContainer()
{
return static::$kernel->getContainer();
}
/**
* @return Devhelp\Bundle\DataFixturesBundle\Service\LoaderService
*/
protected function getFixturesLoader()
{
return $this->getContainer()->get('devhelp.data_fixtures.loader');
}
}
<?php
namespace Devhelp\Bundle\DemoBundle\Tests;
use Devhelp\Bundle\TestBundle\Test\DbTestCase;
/**
* @package devhelp/demo-bundle
* @author Paweł Barański <pawel.baranski1@gmail.com>
*/
class DemoEntityRepositoryTest extends DbTestCase
{
public function testSomeExampleRepositoryMethod()
{
$this->getFixturesLoader()->load(__DIR__.'/../DataFixtures/ORM/');
$demoEntityRepository = $this->getEntityManager()->getRepository('DevhelpDemoBundle:DemoEntity');
$expectedDemoEntity = $demoEntityRepository->findOneBySomeUniqueColumnName('some-unique-column-value');
$actualDemoEntity = $demoEntityRepository->someExampleRepositoryMethod();
$this->assertEquals(
$expectedDemoEntity->getId(),
$actualDemoEntity->getId()
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment