gpt4 book ai didi

php - 使用现有的迁移表进行 Laravel 单元测试

转载 作者:搜寻专家 更新时间:2023-10-31 21:56:16 24 4
gpt4 key购买 nike

我想使用一组现有的迁移来为我创建表来填充种子数据。我正在使用 Orchestra Testbench用于检测。 Examples like this显示动态创建表格:
DB::schema()->create('oauth_identities', function($table) {...
但我想使用现有的迁移。

测试用例.php:

use Orchestra\Testbench\TestCase as OrchestraTestCase;

abstract class TestCase extends OrchestraTestCase
{
protected function getBasePath()
{
// reset base path to point to our package's src directory
return __DIR__ . '/../vendor/orchestra/testbench/fixture';
}
}

DBTestCase.php:

类 DBTestCase 扩展了 TestCase
{
protected $artisan;
/*
* Bootstrap the application
*/
public function setUp()
{
parent::setUp();

$this->artisan = $this->app->make('Illuminate\Contracts\Console\Kernel');

$this->artisan('migrate', [
'--database' => 'testbench',
'--realpath' => realpath(__DIR__.'/../database/migrations'),
]
);

}

protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$app['config']->set('database.default', 'testbench');
$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => ''
]
);
}

UserTest.php:
class UserTest extends DBTestCase
{
use DatabaseMigrations;

private function seedUser()
{

return [
'email' => 'myemail',
...
];
...
}

...
public function testCreateUser()
{
$user = User::create($this->seedUser());

die(print_r($user));
...

错误:

In \App\Tests\UserTest::testCreateUser General error: 1 no such table: users



但是在我的迁移中,我肯定是在创建表:
Schema::create('users', function (Blueprint $table) {...
那么如何使用现有的迁移表并为迁移生成种子数据呢?

编辑:现在运行时 phpunit在 DBTestCase 中设置我的数据库驱动程序后, 'driver' => 'sqlite', ,它抛出错误:

Class 'Doctrine\DBAL\Driver\PDOSqlite\Driver' not found in .../Database/SQLiteConnection.php



导航至 SQLiteConnection.php , PDOSqliteuse Doctrine\DBAL\Driver\PDOSqlite\Driver as DoctrineDriver;以红色突出显示,表示这是一个未定义的命名空间。

我在我的 database.php 中有它作为驱动程序可用:

数据库.php:
'default' => env('DB_CONNECTION', 'mysql'),

'connections' => [

'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path('database.sqlite'),
'prefix' => '',
],

最佳答案

假设您已经设置了 ServiceProvider正确地在您的包中,并使用 loadMigrationsFrom 指向您的迁移文件内boot方法,那么你只需要像这样改变你的测试类:

class TestCase extends \Orchestra\Testbench\TestCase
{
public function setUp(): void
{
parent::setUp();

$this->artisan('migrate', ['--database' => 'testbench'])->run();
}

/**
* add the package provider
*
* @param $app
* @return array
*/
protected function getPackageProviders($app)
{
return [\Vendor\Your-Packagename::class];
}


/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{
// Setup default database to use sqlite :memory:
$app['config']->set('database.default', 'testbench');
$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}

关于php - 使用现有的迁移表进行 Laravel 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33375952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com