当我利用 laravel 的“RefreshDatabase”特征通过 phphunit 运行数据库迁移时,Laravel 指定一个迁移文件夹

Dim*_*las 2 php phpunit codeigniter database-migration laravel

我正在将我的应用程序从 Codeigniter 迁移到 Laravel,我们也正在进行迭代和单元测试。

该数据库由2个数据库组成:

  • old这是最初的 codeigniter 中使用的。
  • new这用于与 codeingiter 项目不相关的其他功能。

因此,我想制作一个用于old数据库的迁移脚本,但为了避免故障,我想为每个数据库的迁移脚本指定一个特定的文件夹。

因此我找到了这个工具: https: //github.com/Xethron/migrations-generator并通过此帮助输出:

Description:
  Generate a migration from an existing table structure.

Usage:
  migrate:generate [options] [--] [<tables>]

Arguments:
  tables                              A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments

Options:
  -c, --connection[=CONNECTION]       The database connection to use. [default: "etable_api"]
  -t, --tables[=TABLES]               A list of Tables you wish to Generate Migrations for separated by a comma: users,posts,comments
  -i, --ignore[=IGNORE]               A list of Tables you wish to ignore, separated by a comma: users,posts,comments
  -p, --path[=PATH]                   Where should the file be created?
      --defaultIndexNames             Don't use db index names for migrations
      --defaultFKNames                Don't use db foreign key names for migrations
  -h, --help                          Display this help message
  -q, --quiet                         Do not output any message
  -V, --version                       Display this application version
      --ansi                          Force ANSI output
      --no-ansi                       Disable ANSI output
  -n, --no-interaction                Do not ask any interactive question
      --env[=ENV]                     The environment the command should run under
  -tp, --templatePath[=TEMPLATEPATH]  The location of the template for this generator
  -v|vv|vvv, --verbose                Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Run Code Online (Sandbox Code Playgroud)

我可以使用以下命令序列为迁移脚本创建专用文件夹:

mkdir -p ./database/migration/old
php artisan migrate:generate -c old -p ./database/migration/old
Run Code Online (Sandbox Code Playgroud)

通过 artisan 我可以通过以下方式运行迁移:

php artisan migrate -c old -p ./database/migration/old
Run Code Online (Sandbox Code Playgroud)

因此我可以使用 laravel 提供的解决方案:

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ExampleTest extends TestCase
{
    use RefreshDatabase;

    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
      // Do some fancy stuff here
    }
}
Run Code Online (Sandbox Code Playgroud)

但是在使用的时候如何为我想要参与测试的数据库的迁移脚本指定指定的文件夹呢Illuminate\Foundation\Testing\RefreshDatabase

One*_*eek 5

我发现这个问题有点老了,但如果其他人正在寻找这个问题的答案,这对我有用。您可以通过对应用程序进行一些调整来完成您想要做的事情。

\n

我在尝试对具有迁移子文件夹的应用程序运行测试时发现的两个问题是,当构建应用程序时以及当 RefreshDatabase 特征刷新数据库时,子文件夹中的迁移不会被执行。

\n

为了让它发挥作用,我必须:

\n
    \n
  1. 修改我的 CreatesApplication 特征以在创建应用程序后在子文件夹中运行迁移。
  2. \n
  3. 创建我自己的RefreshDatabase特征,该特征将搭载包含的RefreshDatabase特征。
  4. \n
\n
\n

刷新数据库特征

\n
    \n
  • 在tests/Traits目录下创建一个名为\n的新文件(我必须创建这个),该RecursiveRefreshDatabase.php文件包含以下代码:
  • \n
\n
<?php\n\nnamespace Tests\\Traits;\n\nuse Illuminate\\Contracts\\Console\\Kernel;\nuse Illuminate\\Foundation\\Testing\\RefreshDatabase;\n\ntrait RecursiveRefreshDatabase {\n    use RefreshDatabase;\n\n    /**\n     * Refresh the in-memory database.\n     *\n     * @return void\n     */\n    protected function refreshInMemoryDatabase()\n    {\n        $this->artisan('migrate');\n        // 'database/migrations/sub-folder\xe2\x80\x99 would probably be \xe2\x80\x98database/migrations/old\xe2\x80\x99 in the case of the OP\n        $this->artisan('migrate', ['--path' => 'database/migrations/sub-folder\xe2\x80\x99]);\n\n        $this->app[Kernel::class]->setArtisan(null);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  • 在你的测试中替换
  • \n
\n
use Illuminate\\Foundation\\Testing\\RefreshDatabase;\n
Run Code Online (Sandbox Code Playgroud)\n

\n
use Tests\\Traits\\RecursiveRefreshDatabase as RefreshDatabase;\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  • 注意:我将重写refreshInMemoryDatabase本示例中的方法,但如果您不使用内存数据库进行测试,则可能需要重写不同的方法。
  • \n
\n
\n

修改CreatesApplication.php

\n
    \n
  • 修改文件tests/CreatesApplication.php 以调用子文件夹迁移 createApplication() ,如下所示
  • \n
\n
public function createApplication()\n{\n    $app = require __DIR__ . \xe2\x80\x98/../../bootstrap/app.php\xe2\x80\x99;\n\n    $app->make(Kernel::class)->bootstrap();\n\n    $this->afterApplicationCreated(function () {\n        // 'database/migrations/sub-folder\xe2\x80\x99 would probably be \xe2\x80\x98database/migrations/old\xe2\x80\x99 in the case of the OP\n        $this->artisan(\xe2\x80\x98migrate\xe2\x80\x99, [\xe2\x80\x98\xe2\x80\x94path\xe2\x80\x99 => \xe2\x80\x98database/migrations/sub-folder\xe2\x80\x99]);\n    });\n\n    return $app;\n}\n
Run Code Online (Sandbox Code Playgroud)\n



\n这些更改对我有用,并使我的测试再次正常工作。请让我知道这对你有没有用!

\n