当我使用Laravel';通过phphphUnit运行数据库迁移时,Laravel指定一个迁移文件夹;s`RefreshDatabase`特性

当我使用Laravel';通过phphphUnit运行数据库迁移时,Laravel指定一个迁移文件夹;s`RefreshDatabase`特性,php,laravel,codeigniter,phpunit,database-migration,Php,Laravel,Codeigniter,Phpunit,Database Migration,我正在将我的应用程序从Codeigniter迁移到laravel,我们也正在进行集成和单元测试 该数据库由2个数据库组成: 旧的是从原始codeigniter中使用的 new用于codeingiter项目中不相关的其他功能 因此,我想为旧的数据库创建一个迁移脚本,但为了避免崩溃,我想为每个数据库的迁移脚本指定一个特定的文件夹 因此我找到了这个工具:通过这个帮助输出: Description: Generate a migration from an existing table stru

我正在将我的应用程序从Codeigniter迁移到laravel,我们也正在进行集成和单元测试

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

  • 旧的
    是从原始codeigniter中使用的
  • new
    用于codeingiter项目中不相关的其他功能
因此,我想为
旧的
数据库创建一个迁移脚本,但为了避免崩溃,我想为每个数据库的迁移脚本指定一个特定的文件夹

因此我找到了这个工具:通过这个帮助输出:

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

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

php artisan migrate -c old -p ./database/migration/old
因此,我可以使用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
    }
}

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

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

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

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

  • 修改my CreatesApplication trait以在创建应用程序后在子文件夹中运行迁移
  • 创建我自己的
    RefreshDatabase
    trait,该trait将借助包含的
    RefreshDatabase
    trait

  • 刷新数据库特征
    • 在tests/Traits目录下创建一个名为
      RecursiveRefreshDatabase.php的新文件(我必须创建这个文件)
      此文件包含以下代码:

    • 注意:在本例中,我覆盖了
      refreshInMemoryDatabase
      方法,但如果您不使用内存中的数据库进行测试,则可能需要覆盖另一个方法

    修改CreatesApplication.php
    • 修改文件
      tests/CreatesApplication.php
      ,将子文件夹迁移调用为createApplication(),如下所示



    这些改变对我起了作用,使我的测试再次起作用。让我知道它是否适合你

    我一整天都在寻找这个解决方案,谢谢!
    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
        }
    }
    
    <?php
    
    namespace Tests\Traits;
    
    use Illuminate\Contracts\Console\Kernel;
    use Illuminate\Foundation\Testing\RefreshDatabase;
    
    trait RecursiveRefreshDatabase {
        use RefreshDatabase;
    
        /**
         * Refresh the in-memory database.
         *
         * @return void
         */
        protected function refreshInMemoryDatabase()
        {
            $this->artisan('migrate');
            // 'database/migrations/sub-folder’ would probably be ‘database/migrations/old’ in the case of the OP
            $this->artisan('migrate', ['--path' => 'database/migrations/sub-folder’]);
    
            $this->app[Kernel::class]->setArtisan(null);
        }
    }
    
    use Illuminate\Foundation\Testing\RefreshDatabase;
    
    use Tests\Traits\RecursiveRefreshDatabase as RefreshDatabase;
    
    public function createApplication()
    {
        $app = require __DIR__ . ‘/../../bootstrap/app.php’;
    
        $app->make(Kernel::class)->bootstrap();
    
        $this->afterApplicationCreated(function () {
            // 'database/migrations/sub-folder’ would probably be ‘database/migrations/old’ in the case of the OP
            $this->artisan(‘migrate’, [‘—path’ => ‘database/migrations/sub-folder’]);
        });
    
        return $app;
    }