gpt4 book ai didi

php - 在 Laravel 测试期间创建和删除表

转载 作者:行者123 更新时间:2023-12-02 00:33:27 27 4
gpt4 key购买 nike

我在我的 Laravel 5.6 应用程序中使用创建了一个特征。该特征称为 Projectable。 Eloquent 模型将使用此特性。为了测试特征,我想创建一个 ProjectableStub 模型用于测试。然而,由于这是一个 Eloquent 模型,它需要一个表格。

我只想简单地创建和删除一个表来进行测试。但是,当我这样做时,RefreshDatabase 功能似乎出现了问题。为了演示,我只是运行两个测试,这两个测试都尝试创建一个 id = 1Product 模型。由于正在使用 RefreshDatabase 特征,这应该可以正常工作。并且,在下面的示例中,它确实:

<?php

namespace Tests\Feature;

use App\Product;
use Tests\TestCase;
use App\Concerns\Projectable;
use Illuminate\Support\Facades\Schema;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Database\Eloquent\Model as Eloquent;

class ProjectableTest extends TestCase
{
use RefreshDatabase;

public function setUp()
{
parent::setUp();

//$this->createStubTable();
}

/**
* @test
*/
public function example_first_test()
{
factory(Product::class)->create(['id' => 1]);
}

/**
* @test
*/
public function example_second_test()
{
factory(Product::class)->create(['id' => 1]);
}

public function tearDown()
{
//$this->dropStubTable();

parent::tearDown();
}

private function createStubTable()
{
Schema::create('stubs', function ($table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}

private function dropStubTable()
{
Schema::dropIfExists('stubs');
}
}

class ProjectableStub extends Eloquent
{
use Projectable;

protected $table = 'stubs';

protected $guarded = [];
}

但是,一旦我取消注释这两行以便创建和删除 stubs 表,我就会收到一个 SQL 错误,指出正在使用重复的 ID:

<?php

namespace Tests\Feature;

use App\Product;
use Tests\TestCase;
use App\Concerns\Projectable;
use Illuminate\Support\Facades\Schema;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Database\Eloquent\Model as Eloquent;

class ProjectableTest extends TestCase
{
use RefreshDatabase;

public function setUp()
{
parent::setUp();

$this->createStubTable();
}

/**
* @test
*/
public function example_first_test()
{
factory(Product::class)->create(['id' => 1]);
}

/**
* @test
*/
public function example_second_test()
{
factory(Product::class)->create(['id' => 1]);
}

public function tearDown()
{
$this->dropStubTable();

parent::tearDown();
}

private function createStubTable()
{
Schema::create('stubs', function ($table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}

private function dropStubTable()
{
Schema::dropIfExists('stubs');
}
}

class ProjectableStub extends Eloquent
{
use Projectable;

protected $table = 'stubs';

protected $guarded = [];
}

1) Tests\Feature\ProjectableTest::example_second_test Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'

有谁知道为什么在测试中创建和删除表会导致此问题?有没有更好的方法来解决这个问题?也许可以通过某种方式在运行时为这个新表添加迁移?

最佳答案

我认为这可能是答案:

https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html

创建表会导致由 RefreshDatabase 创建的事件事务自动提交。

使 stub 表成为临时表就可以了。这也意味着我也不需要删除表格,因为它会自动发生:

Schema::create('stubs', function ($table) {
$table->temporary();
$table->increments('id');
$table->string('name');
$table->timestamps();
});

到目前为止似乎运行良好。

关于php - 在 Laravel 测试期间创建和删除表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50819257/

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