| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace App\Module\Game\Tests;
- use App\Module\Game\Models\Test;
- use Illuminate\Foundation\Testing\DatabaseTransactions;
- use Tests\TestCase;
- class TestSoftDeleteTest extends TestCase
- {
- use DatabaseTransactions;
- /**
- * 测试软删除功能
- */
- public function test_soft_delete(): void
- {
- // 创建测试数据
- $test = Test::factory()->create([
- 'name' => '测试数据1',
- 'description' => '这是一个测试数据'
- ]);
- // 验证数据存在
- $this->assertDatabaseHas('tests', [
- 'id' => $test->id,
- 'name' => '测试数据1',
- 'description' => '这是一个测试数据'
- ]);
- // 软删除数据
- $test->delete();
- // 验证数据在正常查询中不可见
- $this->assertDatabaseMissing('tests', [
- 'id' => $test->id,
- 'name' => '测试数据1',
- 'description' => '这是一个测试数据'
- ]);
- // 验证数据在软删除表中可见
- $this->assertSoftDeleted('tests', [
- 'id' => $test->id,
- 'name' => '测试数据1',
- 'description' => '这是一个测试数据'
- ]);
- // 恢复数据
- $test->restore();
- // 验证数据恢复后可见
- $this->assertDatabaseHas('tests', [
- 'id' => $test->id,
- 'name' => '测试数据1',
- 'description' => '这是一个测试数据'
- ]);
- }
- /**
- * 测试强制删除功能
- */
- public function test_force_delete(): void
- {
- // 创建测试数据
- $test = Test::factory()->create([
- 'name' => '测试数据2',
- 'description' => '这是另一个测试数据'
- ]);
- // 软删除数据
- $test->delete();
- // 强制删除数据
- $test->forceDelete();
- // 验证数据完全删除
- $this->assertDatabaseMissing('tests', [
- 'id' => $test->id,
- 'name' => '测试数据2',
- 'description' => '这是另一个测试数据'
- ]);
- }
- }
|