Skip to content

Instantly share code, notes, and snippets.

@Stubbs Stubbs/bootstrap.php

Created May 26, 2013
Embed
What would you like to do?
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.
<?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());
@Starojitski

This comment has been minimized.

Copy link

Starojitski commented Jan 11, 2018

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:

 . . .

$application = new Application($kernel);

$command = new UpdateSchemaDoctrineCommand();
$application->add($command);
$input = new ArrayInput(array(
    '--force' => true
));

$command->run($input, new ConsoleOutput());

That makes bootstrapping faster as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.