-
-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[5.x] Support scopes as query methods (#5927)
Co-authored-by: Jason Varga <[email protected]> Co-authored-by: Duncan McClean <[email protected]>
- Loading branch information
1 parent
346acb7
commit 7de9f95
Showing
15 changed files
with
294 additions
and
2 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
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,21 @@ | ||
<?php | ||
|
||
namespace Statamic\Query\Scopes; | ||
|
||
use Statamic\Support\Str; | ||
|
||
trait AllowsScopes | ||
{ | ||
public function allowQueryScope(string $scope, ?string $method = null) | ||
{ | ||
/** @var \Illuminate\Support\Collection $scopes */ | ||
$scopes = app('statamic.query-scopes'); | ||
|
||
$method ??= Str::camel($scope::handle()); | ||
|
||
$scopes->put( | ||
$class = get_class($this->query()), | ||
$scopes->get($class, collect())->put($method, $scope) | ||
); | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
namespace Statamic\Query\Scopes; | ||
|
||
use Statamic\Facades\Scope; | ||
use Statamic\Tags\Parameters; | ||
|
||
trait AppliesScopes | ||
{ | ||
public function applyScope($scope, Parameters|array $context = []) | ||
{ | ||
if (! $class = $this->getScopeClassFor($scope)) { | ||
throw new \Exception("The [$scope] scope does not exist."); | ||
} | ||
|
||
Scope::find($class::handle())->apply($this, $context); | ||
} | ||
|
||
public function canApplyScope($scope): bool | ||
{ | ||
return (bool) $this->getScopeClassFor($scope); | ||
} | ||
|
||
private function getScopeClassFor(string $method): ?string | ||
{ | ||
return app('statamic.query-scopes') | ||
->get(get_class($this), collect()) | ||
->get($method); | ||
} | ||
} |
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,110 @@ | ||
<?php | ||
|
||
namespace Tests\Auth\Eloquent; | ||
|
||
use Illuminate\Filesystem\Filesystem; | ||
use Illuminate\Support\Carbon; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Statamic\Facades\User as Users; | ||
use Statamic\Query\Scopes\Scope; | ||
use Tests\TestCase; | ||
|
||
class EloquentUserQueryBuilderTest extends TestCase | ||
{ | ||
public static $migrationsGenerated = false; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Carbon::setTestNow(Carbon::create(2019, 11, 21, 23, 39, 29)); | ||
|
||
config([ | ||
'statamic.users.repository' => 'eloquent', | ||
'auth.providers.users' => [ | ||
'driver' => 'eloquent', | ||
'model' => User::class, | ||
], | ||
]); | ||
|
||
$this->loadMigrationsFrom(static::migrationsDir()); | ||
|
||
$tmpDir = static::migrationsDir().'/tmp'; | ||
|
||
if (! self::$migrationsGenerated) { | ||
$this->artisan('statamic:auth:migration', ['--path' => $tmpDir]); | ||
|
||
self::$migrationsGenerated = true; | ||
} | ||
|
||
$this->loadMigrationsFrom($tmpDir); | ||
} | ||
|
||
private static function migrationsDir() | ||
{ | ||
return __DIR__.'/__migrations__'; | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
Users::all()->each->delete(); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
public static function tearDownAfterClass(): void | ||
{ | ||
// Clean up the orphaned migration file. | ||
(new Filesystem)->deleteDirectory(static::migrationsDir().'/tmp'); | ||
|
||
parent::tearDownAfterClass(); | ||
} | ||
|
||
#[Test] | ||
public function it_queries_by_scope() | ||
{ | ||
NameScope::register(); | ||
Users::allowQueryScope(NameScope::class); | ||
Users::allowQueryScope(NameScope::class, 'byName'); | ||
|
||
User::create(['name' => 'Jack', 'email' => '[email protected]']); | ||
User::create(['name' => 'Jason', 'email' => '[email protected]']); | ||
User::create(['name' => 'Bob Down', 'email' => '[email protected]']); | ||
User::create(['name' => 'Bob Vance', 'email' => '[email protected]']); | ||
|
||
// Scope defined in model ... | ||
// Eloquent model scopes can pass arguments. | ||
$this->assertEquals([ | ||
'Jack', 'Jason', | ||
], Users::query()->domain('statamic.com')->get()->map->name->all()); | ||
|
||
// Statamic style scope ... | ||
// Statamic scopes require arrays. | ||
$this->assertEquals([ | ||
'Bob Down', 'Bob Vance', | ||
], Users::query()->name(['name' => 'bob'])->get()->map->name->all()); | ||
|
||
// Statamic style scope with method ... | ||
$this->assertEquals([ | ||
'Bob Down', 'Bob Vance', | ||
], Users::query()->byName(['name' => 'bob'])->get()->map->name->all()); | ||
|
||
// Otherwise calling a non-existent method should throw appropriate exception ... | ||
try { | ||
Users::query()->something()->get(); | ||
$this->fail('Undefined method exception was not thrown.'); | ||
} catch (\BadMethodCallException $e) { | ||
$this->assertEquals('Call to undefined method Illuminate\\Database\\Eloquent\\Builder::something()', $e->getMessage()); | ||
} | ||
} | ||
} | ||
|
||
class NameScope extends Scope | ||
{ | ||
protected static $handle = 'name'; | ||
|
||
public function apply($query, $values) | ||
{ | ||
$query->where('name', 'like', '%'.$values['name'].'%'); | ||
} | ||
} |
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
Oops, something went wrong.