Laravel phpunit 如何使用 diffent migrations 进行测试

我正在尝试在Laravel中设置测试,但我想运行与通常运行的迁移不同的迁移。


我为启动数据库而运行的迁移从生产环境中导入数据。

对于测试,我想使用一个名为“test”的不同数据库,并且我想用测试数据填充这个测试数据库,而不是生产数据。


我添加了一个“测试”连接,该连接使用“test”数据库:config/database.php


'connections' => [


        'mysql' => [

            'database' => env('DB_DATABASE', 'forge'),

            ...

        ],


        'testing' => [

            'database' => 'test',

            ...

        ],

],

我设置了这个“测试”连接:phpunit.xml


<?xml version="1.0" encoding="UTF-8"?>

<phpunit ...>

    ...

    <php>

        <env name="DB_CONNECTION" value="testing"/>

        ...

    </php>

</phpunit>

现在,我想使用测试数据初始化此“测试”数据库,使用与默认文件夹不同的文件夹进行迁移。


我可以像这样使用正常的迁移:


<?php


namespace Tests;


use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

use Illuminate\Foundation\Testing\DatabaseMigrations;


abstract class TestCase extends BaseTestCase

{

    use DatabaseMigrations;


    public function setUp(): void

    {

        parent::setUp();

        $this->seed();

    }

}

但这使用默认文件夹 。我想把测试迁移放在文件夹.database/migrationstests/database/migrations


有没有办法让使用来自另一个文件夹的迁移?use DatabaseMigrations;


收到一只叮咚
浏览 105回答 3
3回答

牛魔王的故事

在迁移运行之前,你可能需要覆盖 trait 中的方法,并从那里设置应用的数据库路径。runDatabaseMigrationsDatabaseMigrations该方法可能最终如下所示:runDatabaseMigrationsuse DatabaseMigrations { runDatabaseMigrations as runMigration; }&nbsp; &nbsp; public function runDatabaseMigrations()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->app->useDatabasePath(base_path('tests/database')); //example path//&nbsp; &nbsp; &nbsp; &nbsp; dump($this->app->databasePath());&nbsp; &nbsp; &nbsp; &nbsp; $this->runMigration();&nbsp; &nbsp; }或者,您可以在应用服务提供商的方法中设置:boot&nbsp; &nbsp; if (config('app.env') === 'testing') { //Laravel automatically set env to 'testing' when running test&nbsp; &nbsp; &nbsp; &nbsp; $this->app->useDatabasePath(base_path('tests/database'));&nbsp; &nbsp; }迁移将查找名为“迁移”的“测试/数据库”子文件夹。PS:如果您在默认文件夹中有其他代码或文件夹,则会有副作用。例如,对于此测试类,将找不到您的工厂类。database

大话西游666

工匠迁移有一个路径选项,你必须做出自己的特质才能具有类似的功能。我在想这样的事情。trait PathDatabaseMigrations {&nbsp; &nbsp; public function runDatabaseMigrations()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // optimal&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $path = 'tests/database/migrations';&nbsp; &nbsp; &nbsp; &nbsp; $this->artisan('migrate:fresh', ['--path' => $path,]);&nbsp; &nbsp; &nbsp; &nbsp; $this->app[Kernel::class]->setArtisan(null);&nbsp; &nbsp; &nbsp; &nbsp; $this->beforeApplicationDestroyed(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->artisan('migrate:rollback', ['--path' => $path,]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RefreshDatabaseState::$migrated = false;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}

HUH函数

该 trait 具有一个调用的方法,该方法将重载它在运行迁移时使用的参数。我更改为单个测试的特定测试迁移文件,方法是将其作为迁移参数传入。RefreshDatabasemigrateUsing--pathnamespace Tests\Unit;use Tests\TestCase;use App\Models\Traits\HasUuid;use Illuminate\Database\Eloquent\Model;use Illuminate\Foundation\Testing\RefreshDatabase;class HasUuidTest extends TestCase{&nbsp; &nbsp; use RefreshDatabase;&nbsp; &nbsp; protected function migrateUsing()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '--path' => 'tests/migrations/2022_03_15_220516_create_test_uuid_table.php'&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; }&nbsp; &nbsp; public function test_has_uuid()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $model = new TestUuid;&nbsp; &nbsp; &nbsp; &nbsp; $this->assertEmpty($model->id);&nbsp; &nbsp; &nbsp; &nbsp; $model->save();&nbsp; &nbsp; &nbsp; &nbsp; $this->assertNotEmpty($model->id);&nbsp; &nbsp; }}class TestUuid extends Model{&nbsp; &nbsp; use HasUuid;}
打开App,查看更多内容
随时随地看视频慕课网APP