Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Test the model:prune command pruning soft deleted models #40023

Merged
merged 1 commit into from
Dec 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 78 additions & 4 deletions tests/Database/PruneCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand All @@ -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();
Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down