diff --git a/tests/Database/PruneCommandTest.php b/tests/Database/PruneCommandTest.php index de5c42f5a228..0e7d286cd728 100644 --- a/tests/Database/PruneCommandTest.php +++ b/tests/Database/PruneCommandTest.php @@ -5,14 +5,13 @@ 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; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\BufferedOutput; @@ -53,6 +52,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 +99,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 +112,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 +126,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 +200,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;