-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Database): Fix PHPStan with scopes parameter
- Loading branch information
Showing
7 changed files
with
127 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Database\Models\Scopes; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
use LaraStrict\Database\Scopes\AbstractScope; | ||
use Tests\LaraStrict\Feature\Database\Models\Test; | ||
|
||
class TestScope extends AbstractScope | ||
{ | ||
public function __construct( | ||
private readonly string $value = '', | ||
) { | ||
} | ||
|
||
public function apply(Builder $builder, Model $model): void | ||
{ | ||
$builder->where(Test::AttributeTest, $this->value); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
tests/Feature/Database/Queries/AbstractEloquentQueryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Database\Queries; | ||
|
||
use Closure; | ||
use Illuminate\Database\Eloquent\Scope; | ||
use Tests\LaraStrict\Feature\Database\Models\Scopes\TestScope; | ||
use Tests\LaraStrict\Feature\TestCase; | ||
|
||
class AbstractEloquentQueryTest extends TestCase | ||
{ | ||
/** | ||
* @return array<string|int, array{0: Closure(static):void}> | ||
*/ | ||
public function dataScopes(): array | ||
{ | ||
return [ | ||
'empty' => [ | ||
static fn (self $self) => $self->assertScopes( | ||
'select * from "tests" where "tests"."deleted_at" is null', | ||
[] | ||
), | ||
], | ||
'null' => [ | ||
static fn (self $self) => $self->assertScopes( | ||
'select * from "tests" where "tests"."deleted_at" is null', | ||
[null] | ||
), | ||
], | ||
'null and test scope' => [ | ||
static fn (self $self) => $self->assertScopes( | ||
'select * from "tests" where "test" = ? and "tests"."deleted_at" is null', | ||
[new TestScope(), null] | ||
), | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @param Closure(static):void $assert | ||
* | ||
* @dataProvider dataScopes | ||
*/ | ||
public function testScopes(Closure $assert): void | ||
{ | ||
$assert($this); | ||
} | ||
|
||
/** | ||
* @param array<int, Scope|null> $scopes | ||
*/ | ||
public function assertScopes(string $expectedSql, array $scopes): void | ||
{ | ||
$query = app(TestSqlQuery::class); | ||
assert($query instanceof TestSqlQuery); | ||
|
||
$this->assertEquals(expected: $expectedSql, actual: $query->execute($scopes)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Database\Queries; | ||
|
||
use LaraStrict\Database\Queries\AbstractEloquentQuery; | ||
use Tests\LaraStrict\Feature\Database\Models\Test; | ||
|
||
/** | ||
* This is not a real example of a query (scopes should not be a parameter in execute). | ||
* | ||
* @extends AbstractEloquentQuery<Test> | ||
*/ | ||
abstract class AbstractTestQuery extends AbstractEloquentQuery | ||
{ | ||
protected function getModelClass(): string | ||
{ | ||
return Test::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Database\Queries; | ||
|
||
use Illuminate\Database\Eloquent\Scope; | ||
|
||
class TestSqlQuery extends AbstractTestQuery | ||
{ | ||
/** | ||
* @param array<int, Scope|null> $scopes | ||
*/ | ||
public function execute(array $scopes): string | ||
{ | ||
return $this->getQuery($scopes) | ||
->toSql(); | ||
} | ||
} |