TestSoftDeleteTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Module\Game\Tests;
  3. use App\Module\Game\Models\Test;
  4. use Illuminate\Foundation\Testing\DatabaseTransactions;
  5. use Tests\TestCase;
  6. class TestSoftDeleteTest extends TestCase
  7. {
  8. use DatabaseTransactions;
  9. /**
  10. * 测试软删除功能
  11. */
  12. public function test_soft_delete(): void
  13. {
  14. // 创建测试数据
  15. $test = Test::factory()->create([
  16. 'name' => '测试数据1',
  17. 'description' => '这是一个测试数据'
  18. ]);
  19. // 验证数据存在
  20. $this->assertDatabaseHas('tests', [
  21. 'id' => $test->id,
  22. 'name' => '测试数据1',
  23. 'description' => '这是一个测试数据'
  24. ]);
  25. // 软删除数据
  26. $test->delete();
  27. // 验证数据在正常查询中不可见
  28. $this->assertDatabaseMissing('tests', [
  29. 'id' => $test->id,
  30. 'name' => '测试数据1',
  31. 'description' => '这是一个测试数据'
  32. ]);
  33. // 验证数据在软删除表中可见
  34. $this->assertSoftDeleted('tests', [
  35. 'id' => $test->id,
  36. 'name' => '测试数据1',
  37. 'description' => '这是一个测试数据'
  38. ]);
  39. // 恢复数据
  40. $test->restore();
  41. // 验证数据恢复后可见
  42. $this->assertDatabaseHas('tests', [
  43. 'id' => $test->id,
  44. 'name' => '测试数据1',
  45. 'description' => '这是一个测试数据'
  46. ]);
  47. }
  48. /**
  49. * 测试强制删除功能
  50. */
  51. public function test_force_delete(): void
  52. {
  53. // 创建测试数据
  54. $test = Test::factory()->create([
  55. 'name' => '测试数据2',
  56. 'description' => '这是另一个测试数据'
  57. ]);
  58. // 软删除数据
  59. $test->delete();
  60. // 强制删除数据
  61. $test->forceDelete();
  62. // 验证数据完全删除
  63. $this->assertDatabaseMissing('tests', [
  64. 'id' => $test->id,
  65. 'name' => '测试数据2',
  66. 'description' => '这是另一个测试数据'
  67. ]);
  68. }
  69. }