From 6fcdc97dba0d444ac2987f7f186b79b8588f5195 Mon Sep 17 00:00:00 2001 From: Derek Date: Mon, 13 Dec 2021 16:38:44 -0500 Subject: [PATCH] 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. --- tests/Database/PruneCommandTest.php | 81 +++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 3 deletions(-) diff --git a/tests/Database/PruneCommandTest.php b/tests/Database/PruneCommandTest.php index de5c42f5a228..d95e0fa93a41 100644 --- a/tests/Database/PruneCommandTest.php +++ b/tests/Database/PruneCommandTest.php @@ -5,11 +5,11 @@ use Illuminate\Container\Container; use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; use Illuminate\Database\Capsule\Manager as DB; -use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Database\Console\PruneCommand; use Illuminate\Database\Eloquent\MassPrunable; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Prunable; +use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Events\ModelsPruned; use Illuminate\Events\Dispatcher; use Mockery as m; @@ -53,6 +53,36 @@ public function testPrunableTestModelWithoutPrunableRecords() EOF, str_replace("\r", '', $output->fetch())); } + public function testPrunableSoftDeletedModelWithPrunableRecords() + { + $db = new DB; + $db->addConnection([ + 'driver' => 'sqlite', + 'database' => ':memory:', + ]); + $db->bootEloquent(); + $db->setAsGlobal(); + DB::connection('default')->getSchemaBuilder()->create('prunables', function ($table) { + $table->string('value')->nullable(); + $table->datetime('deleted_at')->nullable(); + }); + DB::connection('default')->table('prunables')->insert([ + ['value' => 1, 'deleted_at' => null], + ['value' => 2, 'deleted_at' => '2021-12-01 00:00:00'], + ['value' => 3, 'deleted_at' => null], + ['value' => 4, 'deleted_at' => '2021-12-02 00:00:00'], + ]); + + $output = $this->artisan(['--model' => PrunableTestSoftDeletedModelWithPrunableRecords::class]); + + $this->assertEquals(<<<'EOF' +2 [Illuminate\Tests\Database\PrunableTestSoftDeletedModelWithPrunableRecords] records have been pruned. + +EOF, str_replace("\r", '', $output->fetch())); + + $this->assertEquals(2, PrunableTestSoftDeletedModelWithPrunableRecords::withTrashed()->count()); + } + public function testNonPrunableTest() { $output = $this->artisan(['--model' => NonPrunableTestModel::class]); @@ -70,6 +100,7 @@ public function testTheCommandMayBePretended() 'driver' => 'sqlite', 'database' => ':memory:', ]); + $db->bootEloquent(); $db->setAsGlobal(); DB::connection('default')->getSchemaBuilder()->create('prunables', function ($table) { $table->string('name')->nullable(); @@ -82,8 +113,6 @@ public function testTheCommandMayBePretended() ['name' => 'stuart', 'value' => 4], ['name' => 'bello', 'value' => 5], ]); - $resolver = m::mock(ConnectionResolverInterface::class, ['connection' => $db->getConnection('default')]); - PrunableTestModelWithPrunableRecords::setConnectionResolver($resolver); $output = $this->artisan([ '--model' => PrunableTestModelWithPrunableRecords::class, @@ -98,6 +127,39 @@ public function testTheCommandMayBePretended() $this->assertEquals(5, PrunableTestModelWithPrunableRecords::count()); } + public function testTheCommandMayBePretendedOnSoftDeletedModel() + { + $db = new DB; + $db->addConnection([ + 'driver' => 'sqlite', + 'database' => ':memory:', + ]); + $db->bootEloquent(); + $db->setAsGlobal(); + DB::connection('default')->getSchemaBuilder()->create('prunables', function ($table) { + $table->string('value')->nullable(); + $table->datetime('deleted_at')->nullable(); + }); + DB::connection('default')->table('prunables')->insert([ + ['value' => 1, 'deleted_at' => null], + ['value' => 2, 'deleted_at' => '2021-12-01 00:00:00'], + ['value' => 3, 'deleted_at' => null], + ['value' => 4, 'deleted_at' => '2021-12-02 00:00:00'], + ]); + + $output = $this->artisan([ + '--model' => PrunableTestSoftDeletedModelWithPrunableRecords::class, + '--pretend' => true, + ]); + + $this->assertEquals(<<<'EOF' +2 [Illuminate\Tests\Database\PrunableTestSoftDeletedModelWithPrunableRecords] records will be pruned. + +EOF, str_replace("\r", '', $output->fetch())); + + $this->assertEquals(4, PrunableTestSoftDeletedModelWithPrunableRecords::withTrashed()->count()); + } + protected function artisan($arguments) { $input = new ArrayInput($arguments); @@ -139,6 +201,19 @@ public function prunable() } } +class PrunableTestSoftDeletedModelWithPrunableRecords extends Model +{ + use MassPrunable, SoftDeletes; + + protected $table = 'prunables'; + protected $connection = 'default'; + + public function prunable() + { + return static::where('value', '>=', 3); + } +} + class PrunableTestModelWithoutPrunableRecords extends Model { use Prunable;