Last active
December 18, 2015 16:09
-
-
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)
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 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'); | |
} | |
} |
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 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