Skip to content

Commit 7c0a5f6

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 07c14c5 commit 7c0a5f6

File tree

1 file changed

+78
-4
lines changed

1 file changed

+78
-4
lines changed

tests/Database/PruneCommandTest.php

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
use Illuminate\Container\Container;
66
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
77
use Illuminate\Database\Capsule\Manager as DB;
8-
use Illuminate\Database\ConnectionResolverInterface;
98
use Illuminate\Database\Console\PruneCommand;
109
use Illuminate\Database\Eloquent\MassPrunable;
1110
use Illuminate\Database\Eloquent\Model;
1211
use Illuminate\Database\Eloquent\Prunable;
12+
use Illuminate\Database\Eloquent\SoftDeletes;
1313
use Illuminate\Database\Events\ModelsPruned;
1414
use Illuminate\Events\Dispatcher;
15-
use Mockery as m;
1615
use PHPUnit\Framework\TestCase;
1716
use Symfony\Component\Console\Input\ArrayInput;
1817
use Symfony\Component\Console\Output\BufferedOutput;
@@ -53,6 +52,36 @@ public function testPrunableTestModelWithoutPrunableRecords()
5352
EOF, str_replace("\r", '', $output->fetch()));
5453
}
5554

55+
public function testPrunableSoftDeletedModelWithPrunableRecords()
56+
{
57+
$db = new DB;
58+
$db->addConnection([
59+
'driver' => 'sqlite',
60+
'database' => ':memory:',
61+
]);
62+
$db->bootEloquent();
63+
$db->setAsGlobal();
64+
DB::connection('default')->getSchemaBuilder()->create('prunables', function ($table) {
65+
$table->string('value')->nullable();
66+
$table->datetime('deleted_at')->nullable();
67+
});
68+
DB::connection('default')->table('prunables')->insert([
69+
['value' => 1, 'deleted_at' => null],
70+
['value' => 2, 'deleted_at' => '2021-12-01 00:00:00'],
71+
['value' => 3, 'deleted_at' => null],
72+
['value' => 4, 'deleted_at' => '2021-12-02 00:00:00'],
73+
]);
74+
75+
$output = $this->artisan(['--model' => PrunableTestSoftDeletedModelWithPrunableRecords::class]);
76+
77+
$this->assertEquals(<<<'EOF'
78+
2 [Illuminate\Tests\Database\PrunableTestSoftDeletedModelWithPrunableRecords] records have been pruned.
79+
80+
EOF, str_replace("\r", '', $output->fetch()));
81+
82+
$this->assertEquals(2, PrunableTestSoftDeletedModelWithPrunableRecords::withTrashed()->count());
83+
}
84+
5685
public function testNonPrunableTest()
5786
{
5887
$output = $this->artisan(['--model' => NonPrunableTestModel::class]);
@@ -70,6 +99,7 @@ public function testTheCommandMayBePretended()
7099
'driver' => 'sqlite',
71100
'database' => ':memory:',
72101
]);
102+
$db->bootEloquent();
73103
$db->setAsGlobal();
74104
DB::connection('default')->getSchemaBuilder()->create('prunables', function ($table) {
75105
$table->string('name')->nullable();
@@ -82,8 +112,6 @@ public function testTheCommandMayBePretended()
82112
['name' => 'stuart', 'value' => 4],
83113
['name' => 'bello', 'value' => 5],
84114
]);
85-
$resolver = m::mock(ConnectionResolverInterface::class, ['connection' => $db->getConnection('default')]);
86-
PrunableTestModelWithPrunableRecords::setConnectionResolver($resolver);
87115

88116
$output = $this->artisan([
89117
'--model' => PrunableTestModelWithPrunableRecords::class,
@@ -98,6 +126,39 @@ public function testTheCommandMayBePretended()
98126
$this->assertEquals(5, PrunableTestModelWithPrunableRecords::count());
99127
}
100128

129+
public function testTheCommandMayBePretendedOnSoftDeletedModel()
130+
{
131+
$db = new DB;
132+
$db->addConnection([
133+
'driver' => 'sqlite',
134+
'database' => ':memory:',
135+
]);
136+
$db->bootEloquent();
137+
$db->setAsGlobal();
138+
DB::connection('default')->getSchemaBuilder()->create('prunables', function ($table) {
139+
$table->string('value')->nullable();
140+
$table->datetime('deleted_at')->nullable();
141+
});
142+
DB::connection('default')->table('prunables')->insert([
143+
['value' => 1, 'deleted_at' => null],
144+
['value' => 2, 'deleted_at' => '2021-12-01 00:00:00'],
145+
['value' => 3, 'deleted_at' => null],
146+
['value' => 4, 'deleted_at' => '2021-12-02 00:00:00'],
147+
]);
148+
149+
$output = $this->artisan([
150+
'--model' => PrunableTestSoftDeletedModelWithPrunableRecords::class,
151+
'--pretend' => true,
152+
]);
153+
154+
$this->assertEquals(<<<'EOF'
155+
2 [Illuminate\Tests\Database\PrunableTestSoftDeletedModelWithPrunableRecords] records will be pruned.
156+
157+
EOF, str_replace("\r", '', $output->fetch()));
158+
159+
$this->assertEquals(4, PrunableTestSoftDeletedModelWithPrunableRecords::withTrashed()->count());
160+
}
161+
101162
protected function artisan($arguments)
102163
{
103164
$input = new ArrayInput($arguments);
@@ -139,6 +200,19 @@ public function prunable()
139200
}
140201
}
141202

203+
class PrunableTestSoftDeletedModelWithPrunableRecords extends Model
204+
{
205+
use MassPrunable, SoftDeletes;
206+
207+
protected $table = 'prunables';
208+
protected $connection = 'default';
209+
210+
public function prunable()
211+
{
212+
return static::where('value', '>=', 3);
213+
}
214+
}
215+
142216
class PrunableTestModelWithoutPrunableRecords extends Model
143217
{
144218
use Prunable;

0 commit comments

Comments
 (0)