DatabaseSoftDeletingTraitTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Database\Eloquent\SoftDeletes;
  4. use Illuminate\Support\Carbon;
  5. use Mockery as m;
  6. use PHPUnit\Framework\TestCase;
  7. use stdClass;
  8. class DatabaseSoftDeletingTraitTest extends TestCase
  9. {
  10. protected function tearDown(): void
  11. {
  12. m::close();
  13. }
  14. public function testDeleteSetsSoftDeletedColumn()
  15. {
  16. $model = m::mock(DatabaseSoftDeletingTraitStub::class);
  17. $model->makePartial();
  18. $model->shouldReceive('newModelQuery')->andReturn($query = m::mock(stdClass::class));
  19. $query->shouldReceive('where')->once()->with('id', '=', 1)->andReturn($query);
  20. $query->shouldReceive('update')->once()->with([
  21. 'deleted_at' => 'date-time',
  22. 'updated_at' => 'date-time',
  23. ]);
  24. $model->shouldReceive('syncOriginalAttributes')->once()->with([
  25. 'deleted_at',
  26. 'updated_at',
  27. ]);
  28. $model->delete();
  29. $this->assertInstanceOf(Carbon::class, $model->deleted_at);
  30. }
  31. public function testRestore()
  32. {
  33. $model = m::mock(DatabaseSoftDeletingTraitStub::class);
  34. $model->makePartial();
  35. $model->shouldReceive('fireModelEvent')->with('restoring')->andReturn(true);
  36. $model->shouldReceive('save')->once();
  37. $model->shouldReceive('fireModelEvent')->with('restored', false)->andReturn(true);
  38. $model->restore();
  39. $this->assertNull($model->deleted_at);
  40. }
  41. public function testRestoreCancel()
  42. {
  43. $model = m::mock(DatabaseSoftDeletingTraitStub::class);
  44. $model->makePartial();
  45. $model->shouldReceive('fireModelEvent')->with('restoring')->andReturn(false);
  46. $model->shouldReceive('save')->never();
  47. $this->assertFalse($model->restore());
  48. }
  49. }
  50. class DatabaseSoftDeletingTraitStub
  51. {
  52. use SoftDeletes;
  53. public $deleted_at;
  54. public $updated_at;
  55. public $timestamps = true;
  56. public function newQuery()
  57. {
  58. //
  59. }
  60. public function getKey()
  61. {
  62. return 1;
  63. }
  64. public function getKeyName()
  65. {
  66. return 'id';
  67. }
  68. public function save()
  69. {
  70. //
  71. }
  72. public function delete()
  73. {
  74. return $this->performDeleteOnModel();
  75. }
  76. public function fireModelEvent()
  77. {
  78. //
  79. }
  80. public function freshTimestamp()
  81. {
  82. return Carbon::now();
  83. }
  84. public function fromDateTime()
  85. {
  86. return 'date-time';
  87. }
  88. public function getUpdatedAtColumn()
  89. {
  90. return defined('static::UPDATED_AT') ? static::UPDATED_AT : 'updated_at';
  91. }
  92. public function setKeysForSaveQuery($query)
  93. {
  94. $query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery());
  95. return $query;
  96. }
  97. protected function getKeyForSaveQuery()
  98. {
  99. return 1;
  100. }
  101. }