|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Eloquent; |
| 6 | + |
| 7 | +use Illuminate\Database\Console\PruneCommand; |
| 8 | +use Illuminate\Database\Eloquent\MassPrunable; |
| 9 | +use Illuminate\Database\Eloquent\Prunable; |
| 10 | +use MongoDB\Laravel\Tests\Models\Soft; |
| 11 | +use MongoDB\Laravel\Tests\Models\User; |
| 12 | +use MongoDB\Laravel\Tests\TestCase; |
| 13 | + |
| 14 | +class MassPrunableTest extends TestCase |
| 15 | +{ |
| 16 | + public function tearDown(): void |
| 17 | + { |
| 18 | + User::truncate(); |
| 19 | + Soft::truncate(); |
| 20 | + } |
| 21 | + |
| 22 | + public function testPruneWithQuery(): void |
| 23 | + { |
| 24 | + $this->assertTrue($this->isPrunable(User::class)); |
| 25 | + |
| 26 | + User::insert([ |
| 27 | + ['name' => 'John Doe', 'age' => 35], |
| 28 | + ['name' => 'Jane Doe', 'age' => 32], |
| 29 | + ['name' => 'Tomy Doe', 'age' => 11], |
| 30 | + ]); |
| 31 | + |
| 32 | + $model = new User(); |
| 33 | + $total = $model->pruneAll(); |
| 34 | + $this->assertEquals(2, $total); |
| 35 | + $this->assertEquals(1, User::count()); |
| 36 | + } |
| 37 | + |
| 38 | + public function testPruneSoftDelete(): void |
| 39 | + { |
| 40 | + $this->assertTrue($this->isPrunable(Soft::class)); |
| 41 | + |
| 42 | + Soft::insert([ |
| 43 | + ['name' => 'John Doe'], |
| 44 | + ['name' => 'Jane Doe'], |
| 45 | + ]); |
| 46 | + |
| 47 | + $model = new Soft(); |
| 48 | + $total = $model->pruneAll(); |
| 49 | + $this->assertEquals(2, $total); |
| 50 | + $this->assertEquals(0, Soft::count()); |
| 51 | + $this->assertEquals(0, Soft::withTrashed()->count()); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @see PruneCommand::isPrunable() |
| 56 | + */ |
| 57 | + protected function isPrunable($model) |
| 58 | + { |
| 59 | + $uses = class_uses_recursive($model); |
| 60 | + |
| 61 | + return in_array(Prunable::class, $uses) || in_array(MassPrunable::class, $uses); |
| 62 | + } |
| 63 | +} |
0 commit comments