Created
May 26, 2013 00:22
-
-
Save Stubbs/5651300 to your computer and use it in GitHub Desktop.
Isolate unit tests in Symfony2 http://php-and-symfony.matthiasnoback.nl/2011/10/symfony2-use-a-bootstrap-file-for-your-phpunit-tests-and-run-some-console-commands/ and then use this as your bootstrap to load migrations & fixtures.
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 | |
require_once __DIR__ . '/bootstrap.php.cache'; | |
require_once __DIR__ . '/AppKernel.php'; | |
use Symfony\Bundle\FrameworkBundle\Console\Application; | |
use Symfony\Component\Console\Output\ConsoleOutput; | |
use Symfony\Component\Console\Input\ArrayInput; | |
use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand; | |
use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand; | |
use Doctrine\Bundle\MigrationsBundle\Command\MigrationsMigrateDoctrineCommand; | |
use Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand; | |
$kernel = new AppKernel('test', true); // create a "test" kernel | |
$kernel->boot(); | |
$application = new Application($kernel); | |
// add the database:drop command to the application and run it | |
$command = new DropDatabaseDoctrineCommand(); | |
$application->add($command); | |
$input = new ArrayInput(array( | |
'command' => 'doctrine:database:drop', | |
'--force' => true, | |
)); | |
$command->run($input, new ConsoleOutput()); | |
// This stops a bug where Drop Database does not close the handle properly & causes subsequent | |
// "database not found" errors. | |
$connection = $application->getKernel()->getContainer()->get('doctrine')->getConnection(); | |
if ($connection->isConnected()) { | |
$connection->close(); | |
} | |
// add the database:create command to the application and run it | |
$command = new CreateDatabaseDoctrineCommand(); | |
$application->add($command); | |
$input = new ArrayInput(array( | |
'command' => 'doctrine:database:create', | |
)); | |
$command->run($input, new ConsoleOutput()); | |
// Run the database migrations, with --quiet because they are quite | |
// chatty on the console. | |
$command = new MigrationsMigrateDoctrineCommand(); | |
$application->add($command); | |
$input = new ArrayInput(array( | |
'command' => 'doctrine:migrations:migrate', | |
'--quiet' => true, | |
'--no-interaction' => true, | |
)); | |
$input->setInteractive(false); | |
$command->run($input, new ConsoleOutput(ConsoleOutput::VERBOSITY_QUIET)); | |
// and load the fixtures | |
$command = new LoadDataFixturesDoctrineCommand(); | |
$application->add($command); | |
$input = new ArrayInput(array( | |
'command' => 'doctrine:fixtures:load', | |
'--quiet', | |
)); | |
$input->setInteractive(false); | |
$command->run($input, new ConsoleOutput()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since you always recreate database you might as well use sqLite in memory. In this case all you need is this command before loading fixtures:
That makes bootstrapping faster as well.