|
10 | 10 | use Illuminate\Database\Eloquent\MassPrunable; |
11 | 11 | use Illuminate\Database\Eloquent\Model; |
12 | 12 | use Illuminate\Database\Eloquent\Prunable; |
| 13 | +use Illuminate\Database\Eloquent\SoftDeletes; |
13 | 14 | use Illuminate\Database\Events\ModelsPruned; |
14 | 15 | use Illuminate\Events\Dispatcher; |
15 | 16 | use Mockery as m; |
@@ -53,6 +54,37 @@ public function testPrunableTestModelWithoutPrunableRecords() |
53 | 54 | EOF, str_replace("\r", '', $output->fetch())); |
54 | 55 | } |
55 | 56 |
|
| 57 | + public function testPrunableSoftDeletedModelWithPrunableRecords() |
| 58 | + { |
| 59 | + $db = new DB; |
| 60 | + $db->addConnection([ |
| 61 | + 'driver' => 'sqlite', |
| 62 | + 'database' => ':memory:', |
| 63 | + ]); |
| 64 | + $db->setAsGlobal(); |
| 65 | + DB::connection('default')->getSchemaBuilder()->create('prunables', function ($table) { |
| 66 | + $table->string('value')->nullable(); |
| 67 | + $table->datetime('deleted_at')->nullable(); |
| 68 | + }); |
| 69 | + DB::connection('default')->table('prunables')->insert([ |
| 70 | + ['value' => 1, 'deleted_at' => null], |
| 71 | + ['value' => 2, 'deleted_at' => '2021-12-01 00:00:00'], |
| 72 | + ['value' => 3, 'deleted_at' => null], |
| 73 | + ['value' => 4, 'deleted_at' => '2021-12-02 00:00:00'], |
| 74 | + ]); |
| 75 | + $resolver = m::mock(ConnectionResolverInterface::class, ['connection' => $db->getConnection('default')]); |
| 76 | + PrunableTestSoftDeletedModelWithPrunableRecords::setConnectionResolver($resolver); |
| 77 | + |
| 78 | + $output = $this->artisan(['--model' => PrunableTestSoftDeletedModelWithPrunableRecords::class]); |
| 79 | + |
| 80 | + $this->assertEquals(<<<'EOF' |
| 81 | +2 [Illuminate\Tests\Database\PrunableTestSoftDeletedModelWithPrunableRecords] records have been pruned. |
| 82 | + |
| 83 | +EOF, str_replace("\r", '', $output->fetch())); |
| 84 | + |
| 85 | + $this->assertEquals(2, PrunableTestSoftDeletedModelWithPrunableRecords::withTrashed()->count()); |
| 86 | + } |
| 87 | + |
56 | 88 | public function testNonPrunableTest() |
57 | 89 | { |
58 | 90 | $output = $this->artisan(['--model' => NonPrunableTestModel::class]); |
@@ -98,6 +130,40 @@ public function testTheCommandMayBePretended() |
98 | 130 | $this->assertEquals(5, PrunableTestModelWithPrunableRecords::count()); |
99 | 131 | } |
100 | 132 |
|
| 133 | + public function testTheCommandMayBePretendedOnSoftDeletedModel() |
| 134 | + { |
| 135 | + $db = new DB; |
| 136 | + $db->addConnection([ |
| 137 | + 'driver' => 'sqlite', |
| 138 | + 'database' => ':memory:', |
| 139 | + ]); |
| 140 | + $db->setAsGlobal(); |
| 141 | + DB::connection('default')->getSchemaBuilder()->create('prunables', function ($table) { |
| 142 | + $table->string('value')->nullable(); |
| 143 | + $table->datetime('deleted_at')->nullable(); |
| 144 | + }); |
| 145 | + DB::connection('default')->table('prunables')->insert([ |
| 146 | + ['value' => 1, 'deleted_at' => null], |
| 147 | + ['value' => 2, 'deleted_at' => '2021-12-01 00:00:00'], |
| 148 | + ['value' => 3, 'deleted_at' => null], |
| 149 | + ['value' => 4, 'deleted_at' => '2021-12-02 00:00:00'], |
| 150 | + ]); |
| 151 | + $resolver = m::mock(ConnectionResolverInterface::class, ['connection' => $db->getConnection('default')]); |
| 152 | + PrunableTestSoftDeletedModelWithPrunableRecords::setConnectionResolver($resolver); |
| 153 | + |
| 154 | + $output = $this->artisan([ |
| 155 | + '--model' => PrunableTestSoftDeletedModelWithPrunableRecords::class, |
| 156 | + '--pretend' => true, |
| 157 | + ]); |
| 158 | + |
| 159 | + $this->assertEquals(<<<'EOF' |
| 160 | +2 [Illuminate\Tests\Database\PrunableTestSoftDeletedModelWithPrunableRecords] records will be pruned. |
| 161 | + |
| 162 | +EOF, str_replace("\r", '', $output->fetch())); |
| 163 | + |
| 164 | + $this->assertEquals(4, PrunableTestSoftDeletedModelWithPrunableRecords::withTrashed()->count()); |
| 165 | + } |
| 166 | + |
101 | 167 | protected function artisan($arguments) |
102 | 168 | { |
103 | 169 | $input = new ArrayInput($arguments); |
@@ -139,6 +205,19 @@ public function prunable() |
139 | 205 | } |
140 | 206 | } |
141 | 207 |
|
| 208 | +class PrunableTestSoftDeletedModelWithPrunableRecords extends Model |
| 209 | +{ |
| 210 | + use MassPrunable, SoftDeletes; |
| 211 | + |
| 212 | + protected $table = 'prunables'; |
| 213 | + protected $connection = 'default'; |
| 214 | + |
| 215 | + public function prunable() |
| 216 | + { |
| 217 | + return static::where('value', '>=', 3); |
| 218 | + } |
| 219 | +} |
| 220 | + |
142 | 221 | class PrunableTestModelWithoutPrunableRecords extends Model |
143 | 222 | { |
144 | 223 | use Prunable; |
|
0 commit comments