phpsymfonydoctrine-ormcodeception

Codeception & Symfony - run Doctrine migrations before tests


I have a Symfony 4 application and Doctrine with Doctrine migrations. I'm introducing Codeception for running API tests, and need to run migrations before the tests run. Since I'm using the Doctrine2 module I don't really want to be also including the DB module as it's not needed for the tests and would require configuring the test database in two different locations.

I am using the Symfony module currently, and I noticed that the Laravel module has a run_database_migrations configuration option.

What is the best way to handle running the Doctrine migrations command in a Symfony app prior to the tests? (bin/console doctrine:migrations:migrate -n is the specific command).


Edit I've got a solution that, although it works, is nowhere near ideal. By using Codeception Customisation I've created the following extension that basically manually execs the underlying Symfony commands.

class DatabaseMigrationExtension extends Extension
{
    public static $events = [
        Events::SUITE_BEFORE => 'beforeSuite',
    ];

    public function beforeSuite(SuiteEvent $e)
    {
        echo(exec('bin/console doctrine:database:drop --force') . PHP_EOL);
        echo(exec('bin/console doctrine:database:create') . PHP_EOL);
        echo(exec('bin/console doctrine:migrations:migrate -n') . PHP_EOL);
    }
}

Edit 2 The goal of this is basically to replicate similar functionality to what the Codeception DB module does, which allows you to provide an SQL dump of a database that it automatically uses in the tests, but instead use Doctrine migrations to handle the DB. - https://codeception.com/docs/modules/Db#sql-data-dump


Solution

  • I spent a while trying a couple of different ways to achieve this. I initially used RunProcess however this seemed to cause sporadic issues with the DB being deleted and not recreated, despite using the sleep configuration. I ended up just updating the existing extension to use the CLI module instead, and it works as desired (without having to create scripts or run multiple commands) and without having to use exec.

    Final extension;

    class DatabaseMigrationExtension extends Extension
    {
        public static $events = [
            Events::SUITE_BEFORE => 'beforeSuite',
        ];
    
        public function beforeSuite()
        {
            try {
                /** @var \Codeception\Module\Cli $cli */
                $cli = $this->getModule('Cli');
    
                $this->writeln('Recreating the DB...');
                $cli->runShellCommand('bin/console doctrine:database:drop --if-exists --force');
                $cli->seeResultCodeIs(0);
                $cli->runShellCommand('bin/console doctrine:database:create');
                $cli->seeResultCodeIs(0);
    
                $this->writeln('Running Doctrine Migrations...');
                $cli->runShellCommand('bin/console doctrine:migrations:migrate --no-interaction');
                $cli->seeResultCodeIs(0);
    
                $this->writeln('Test database recreated');
            } catch (\Exception $e) {
                $this->writeln(
                    sprintf(
                        'An error occurred whilst rebuilding the test database: %s',
                        $e->getMessage()
                    )
                );
            }
        }
    }
    

    and registered;

    // codeception.yml
    extensions:
        enabled:
            - \DatabaseMigrationExtension
    

    Output (-vv or higher also displays the output of the DB & Migration commands);