繁体   English   中英

Codeception & Symfony - 在测试前运行 Doctrine 迁移

[英]Codeception & Symfony - run Doctrine migrations before tests

我有一个 Symfony 4 应用程序和带有 Doctrine 迁移的 Doctrine。 我正在引入用于运行 API 测试的 Codeception,并且需要在测试运行之前运行迁移。 由于我使用的是Doctrine2 模块,因此我真的不想还包括DB 模块,因为测试不需要它,并且需要在两个不同的位置配置测试数据库。

我目前正在使用Symfony 模块,我注意到Laravel 模块有一个run_database_migrations配置选项。

在测试之前处理在 Symfony 应用程序中运行 Doctrine 迁移命令的最佳方法是什么? bin/console doctrine:migrations:migrate -n是特定命令)。


编辑我有一个解决方案,虽然它有效,但远非理想。 通过使用Codeception Customization,我创建了以下扩展,它基本上手动exec底层 Symfony 命令。

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);
    }
}

编辑 2其目标基本上是复制与 Codeception DB 模块所做的类似的功能,这允许您提供它在测试中自动使用的数据库的 SQL 转储,而是使用 Doctrine 迁移来处理数据库。 - https://codeception.com/docs/modules/Db#sql-data-dump

我花了一段时间尝试了几种不同的方法来实现这一目标。 我最初使用RunProcess但是这似乎会导致数据库被删除而不是重新创建的偶发问题,尽管使用了睡眠配置。 我最终只是更新了现有的扩展以使用 CLI 模块,它可以按需要工作(无需创建脚本或运行多个命令),也无需使用exec

最终延期;

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()
                )
            );
        }
    }
}

并注册;

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

输出( -vv或更高版本还显示 DB & Migration 命令的输出);

我总是创建一个 bash 脚本来执行此操作,或者创建一个 Makefile。

bash 命令

我的./runtests.sh脚本包含

#!/bin/bash
./bin/console command:for:migrations
./bin/phpunit

生成文件

与 Makefile 相同

.FOO: testsuite
testsuite:
    ./runtests.sh

或者

.FOO: testsuite
testsuite:
    ./bin/console command:for:migrations
    ./bin/phpunit

为什么我更喜欢 Makefile

最近我在我的 .bash_profile 中添加了这个脚本,它允许我从 bash 自动完成在 makefile 中创建的所有目标(非常简单,因为您不再需要记住所有命令,而只需maketab )。

完整 -W "`grep -oE '^[a-zA-Z0-9_.-]+:([^=]|$)' Makefile | sed 's/[^a-zA-Z0-9_.-] *$//'`" 制作

因此,.. 您可以创建如下目标:

  • 运行测试
  • runtests_with_fixtures
  • 迁移
  • runtests_with_migrations
  • ...

等等

我的建议是创建自定义且简单的方法来运行命令。


这是一种运行所有或仅一个命令的方法 usgin make

.FOO: dropforce
dropforce:
    bin/console doctrine:database:drop --force

.FOO: dbcreate
dbcreate:
    bin/console doctrine:database:create

.FOO: migrate
migrate:
    bin/console doctrine:migrations:migrate

.FOO: suite
suite: dropforce dbcreate migrate

使用 Codeception 4,您可以在没有 Cli 模块的情况下完成:

$symfony = $this->getModule('Symfony');

$symfony->runSymfonyConsoleCommand('doctrine:database:drop',['--if-exists'=>true, '--force'=>true]);
$symfony->runSymfonyConsoleCommand('doctrine:database:create');
$symfony->runSymfonyConsoleCommand('doctrine:migrations:migrate', ['--no-interaction'=>true]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM