Skip to content

Commit

Permalink
Test the model:prune command pruning soft deleted models
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
derekmd committed Dec 14, 2021
1 parent 07c14c5 commit 6fcdc97
Showing 1 changed file with 78 additions and 3 deletions.
81 changes: 78 additions & 3 deletions tests/Database/PruneCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand All @@ -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();
Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6fcdc97

Please sign in to comment.