Skip to content

Instantly share code, notes, and snippets.

@lytithwyn
Created April 24, 2012 13:41
Show Gist options
  • Save lytithwyn/2479753 to your computer and use it in GitHub Desktop.
Save lytithwyn/2479753 to your computer and use it in GitHub Desktop.
Fixtures inside functional tests blog post
<?php
# make sure there were no problems loading the fixtures
self::assertNotContains('Exception', $cliCapturedOutput);
?>
<?php
# create a cli application
$application = new Application($client->getKernel());
$application->setAutoExit(false);
?>
<?php
# get the container
$client = static::createClient();
self::$container = $client->getContainer();
$doctrine = self::$container->get('doctrine');
$em = $doctrine->getEntityManager();
?>
<?php
namespace TestFrameWork;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Bundle\FrameworkBundle\Console\Application;
class FreshFixtureWebTestCase extends WebTestCase {
protected static $container;
protected static $schemaTool;
public static function setUpBeforeClass() {
# get the container
$client = static::createClient();
self::$container = $client->getContainer();
$doctrine = self::$container->get('doctrine');
$em = $doctrine->getEntityManager();
# get the list of all our classes' metadata
$mdf = $em->getMetadataFactory();
$classes = $mdf->getAllMetadata();
# drop the schema and create a fresh one using our metadata
self::$schemaTool = new SchemaTool($em);
self::$schemaTool->dropDatabase();
self::$schemaTool->createSchema($classes);
# create a cli application
$application = new Application($client->getKernel());
$application->setAutoExit(false);
# use the cli to load the fixtures
$cliOutputFP = tmpfile();
$cliOutput = new StreamOutput($cliOutputFP);
$cliInput = new StringInput('doctrine:fixtures:load');
$application->run($cliInput, $cliOutput);
# collect the output from the load fixtures command
fseek($cliOutputFP, 0);
$cliCapturedOutput = '';
while(!feof($cliOutputFP)) {
$cliCapturedOutput = fread($cliOutputFP, 4096);
}
fclose($cliOutputFP);
# make sure there were no problems loading the fixtures
self::assertNotContains('Exception', $cliCapturedOutput);
}
public static function tearDownAfterClass() {
# clear all our changes
self::$schemaTool->dropDatabase();
}
}
?>
<?php
namespace TestFrameWork;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class FreshFixtureWebTestCase extends WebTestCase {
public static function setUpBeforeClass() {
}
public static function tearDownAfterClass() {
}
}
?>
<?php
# get the list of all our classes' metadata
$mdf = $em->getMetadataFactory();
$classes = $mdf->getAllMetadata();
?>
<?php
# use the cli to load the fixtures
$cliOutputFP = tmpfile();
$cliOutput = new StreamOutput($cliOutputFP);
$cliInput = new StringInput('doctrine:fixtures:load');
$application->run($cliInput, $cliOutput);
# collect the output from the load fixtures command
fseek($cliOutputFP, 0);
$cliCapturedOutput = '';
while(!feof($cliOutputFP)) {
$cliCapturedOutput = fread($cliOutputFP, 4096);
}
fclose($cliOutputFP);
?>
<?php
# drop the schema and create a fresh one using our metadata
self::$schemaTool = new SchemaTool($em);
self::$schemaTool->dropDatabase();
self::$schemaTool->createSchema($classes);
?>
<?php
public static function tearDownAfterClass() {
# clear all our changes
self::$schemaTool->dropDatabase();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment