diff --git a/src/Database/Queries/AbstractEloquentQuery.php b/src/Database/Queries/AbstractEloquentQuery.php index 01d6720b..09f64ddd 100644 --- a/src/Database/Queries/AbstractEloquentQuery.php +++ b/src/Database/Queries/AbstractEloquentQuery.php @@ -94,7 +94,7 @@ protected function chunkWrite(array $items, int $columnsCount = 0): void /** * @param Scope[] $scopes * - * @return Collection + * @return Collection */ protected function getAll(array $scopes = []): Collection { @@ -209,31 +209,8 @@ protected function forceDelete(array $scopes = []): int protected function getQuery(array $scopes = []): Builder { $class = $this->getModelClass(); - - return $this->getScopedQuery($class::query(), $scopes); - } - - /** - * @param Builder $builder - * @param Scope[]|null[]|null $scopes - * - * @return Builder - */ - protected function getScopedQuery(Builder $builder, ?array $scopes): Builder - { - if ($scopes === null) { - return $builder; - } - - foreach ($scopes as $scope) { - if ($scope === null) { - continue; - } - - // We are unable to reuse applyScope logic due protected methods (our scopes can contain withoutGlobalScope) - $scope->apply($builder, $builder->getModel()); - } - - return $builder; + /** @var Builder $query */ + $query = $class::query(); + return $this->getScopedQuery($query, $scopes); } } diff --git a/src/Database/Queries/AbstractQuery.php b/src/Database/Queries/AbstractQuery.php index d59b1d2d..c70b2d4b 100644 --- a/src/Database/Queries/AbstractQuery.php +++ b/src/Database/Queries/AbstractQuery.php @@ -4,6 +4,29 @@ namespace LaraStrict\Database\Queries; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Scope; + abstract class AbstractQuery { + /** + * @template TBuilderModel of \Illuminate\Database\Eloquent\Model + * @param Builder $builder + * @param Scope[]|null[] $scopes + * + * @return Builder + */ + protected function getScopedQuery(Builder $builder, array $scopes): Builder + { + foreach ($scopes as $scope) { + if ($scope === null) { + continue; + } + + // We are unable to reuse applyScope logic due protected methods (our scopes can contain withoutGlobalScope) + $scope->apply($builder, $builder->getModel()); + } + + return $builder; + } } diff --git a/tests/Feature/Database/Queries/TestScopeQuery.php b/tests/Feature/Database/Queries/TestScopeQuery.php index 9eb858ee..07072032 100644 --- a/tests/Feature/Database/Queries/TestScopeQuery.php +++ b/tests/Feature/Database/Queries/TestScopeQuery.php @@ -22,8 +22,7 @@ class TestScopeQuery extends AbstractEloquentQuery */ public function execute(array $scopes): Collection { - return $this->getQuery($scopes) - ->get(); + return $this->getAll($scopes); } protected function getModelClass(): string diff --git a/tests/Feature/Database/Scopes/TestSoftDeleteScopeTest.php b/tests/Feature/Database/Scopes/TestSoftDeleteScopeTest.php index e44b3e47..93b9111f 100644 --- a/tests/Feature/Database/Scopes/TestSoftDeleteScopeTest.php +++ b/tests/Feature/Database/Scopes/TestSoftDeleteScopeTest.php @@ -4,6 +4,7 @@ namespace Tests\LaraStrict\Feature\Database\Scopes; +use Tests\LaraStrict\Feature\Database\Models\Test; use Tests\LaraStrict\Feature\Database\Queries\TestScopeQuery; use Tests\LaraStrict\Feature\DatabaseTestCase; @@ -20,6 +21,7 @@ public function testSoftDeletingGlobalScope(): void $this->assertCount(5, $results); foreach ($results as $result) { + $this->assertInstanceOf(Test::class, $result); $this->assertNull($result->deleted_at); } }