Skip to content

Commit 4e8b3e7

Browse files
committed
Test the model:prune command pruning soft deleted models
Adding the --pretend option introduced a bug on soft-deleted models because there wasn't any test coverage. Ensure yesterday's bug fix isn't mistakenly changed in the future.
1 parent 00a3c25 commit 4e8b3e7

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

tests/Database/PruneCommandTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Database\Eloquent\MassPrunable;
1111
use Illuminate\Database\Eloquent\Model;
1212
use Illuminate\Database\Eloquent\Prunable;
13+
use Illuminate\Database\Eloquent\SoftDeletes;
1314
use Illuminate\Database\Events\ModelsPruned;
1415
use Illuminate\Events\Dispatcher;
1516
use Mockery as m;
@@ -53,6 +54,37 @@ public function testPrunableTestModelWithoutPrunableRecords()
5354
EOF, str_replace("\r", '', $output->fetch()));
5455
}
5556

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+
5688
public function testNonPrunableTest()
5789
{
5890
$output = $this->artisan(['--model' => NonPrunableTestModel::class]);
@@ -98,6 +130,40 @@ public function testTheCommandMayBePretended()
98130
$this->assertEquals(5, PrunableTestModelWithPrunableRecords::count());
99131
}
100132

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+
101167
protected function artisan($arguments)
102168
{
103169
$input = new ArrayInput($arguments);
@@ -139,6 +205,19 @@ public function prunable()
139205
}
140206
}
141207

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+
142221
class PrunableTestModelWithoutPrunableRecords extends Model
143222
{
144223
use Prunable;

0 commit comments

Comments
 (0)