Skip to content

Commit

Permalink
fix: PruneCommand finding its usage within other traits (#42350)
Browse files Browse the repository at this point in the history
This addresses an edge case where using the Prunable trait within another trait (due to several reasons like modifying behaviour, removing duplication, etc..) it was breaking the PruneCommand from behaving properly ince the trait was being detected as a class.

Signed-off-by: Bruno Gaspar <[email protected]>
  • Loading branch information
brunogaspar authored May 11, 2022
1 parent 0a85d0c commit 5cd540e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Illuminate/Database/Console/PruneCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ public function handle(Dispatcher $events)
protected function models()
{
if (! empty($models = $this->option('model'))) {
return collect($models);
return collect($models)->filter(function ($model) {
return class_exists($model);
})->values();
}

$except = $this->option('except');
Expand All @@ -111,6 +113,8 @@ protected function models()
});
})->filter(function ($model) {
return $this->isPrunable($model);
})->filter(function ($model) {
return class_exists($model);
})->values();
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Database/PruneCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ public function testNonPrunableTest()
$this->assertEquals(<<<'EOF'
No prunable [Illuminate\Tests\Database\NonPrunableTestModel] records found.

EOF, str_replace("\r", '', $output->fetch()));
}

public function testNonPrunableTestWithATrait()
{
$output = $this->artisan(['--model' => NonPrunableTrait::class]);

$this->assertEquals(<<<'EOF'
No prunable models found.

EOF, str_replace("\r", '', $output->fetch()));
}

Expand Down Expand Up @@ -227,3 +237,8 @@ class NonPrunableTestModel extends Model
{
// ..
}

trait NonPrunableTrait
{
use Prunable;
}

0 comments on commit 5cd540e

Please sign in to comment.