5

我在客户端和服务器端 JavaScript 应用程序开发方面有一些经验。但是现在我在 php 上设计了我的第一个 Web 应用程序并寻找最好的开发工具堆栈。我使用 phinx 在测试、开发和生产环境之间共享我的数据库结构。我将使用 codeception 进行数据库操作测试。

问题是codeception期望我将创建表的sql命令放入tests/_data/dump.sql并删除我在phinx迁移文件中创建的所有表。我可以设置cleanup: falsecodeception.yml但在这种情况下,我必须在每次测试之前清理数据库表。我不知道怎么做。在每次代码接收测试之前,我发现没有手动清理数据库的能力。

我如何获得代码接收和菲尼克斯协调?

PS:我发现了关于在代码接收中使用迁移的讨论,似乎它的好处并不适合所有人。

4

1 回答 1

2

使用 Codeception ,你可以为任何你想要的东西创建一个助手,包括迁移加载。

这是在每次测试之前加载数据库迁移的助手。我没有机会测试这段代码,但这里的主要思想应该很清楚。

代码接收助手:

namespace Codeception\Module;

use Codeception\Module;
use Codeception\TestInterface;
use Phinx\Console\PhinxApplication;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\NullOutput;

class FixtureHelper extends Module
{
    /**
     * Run database migrations before each test if database population enabled.
     *
     * @param TestInterface $test
     */
    public function _before(TestInterface $test)
    {
        $populate = $this->getModule('Db')->_getConfig('populate');

        if ($populate) {
            $this->migrateDatabase();
        }
    }

    /**
     * Run the database migrations.
     */
    public function migrateDatabase()
    {
        // Run Phinx console application.
        $app = new PhinxApplication();
        $app->setAutoExit(false);

        $output = new NullOutput();
        //$output = new ConsoleOutput();

        // Run database migrations for test environment.
        $input = new StringInput('migrate -e test');
        $app->run($input, $output);

        // ... you also can load the fixtures here
        //$input = new StringInput('seed:run -s <my-seeds> -e test');
        //$app->run($input, $output);
    }
} 

Codeception 配置(用于功能测试):

actor: FunctionalTester
modules:
  enabled:
    - ... your modules
    - FunctionalHelper
    - FixtureHelper
  config:
    Db:
      dsn: '... dsn'
      user: '%DB_USER%'
      password: '%DB_PASSWORD%'
      dump: 'tests/_data/dump.sql'
      populate: true
      cleanup: true
    FixtureHelper:
      depends: Db

数据库转储(tests/_data/dump.sql):

-- Dump should not be empty because cleanup will not work. 
-- So at least any silly sql query.
SELECT 1+2 AS veryComplicatedCalculations;

Phinx config ( phinx.yml) 必须与 Codeception config () 位于同一目录中,codeception.yml否则您必须确保PhinxApplication加载您的配置。

希望这有帮助!

于 2017-10-13T18:42:18.610 回答