mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
WIP
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('team_user', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('team_id')->constrained();
|
||||
$table->foreignId('user_id')->constrained();
|
||||
$table->string('role')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('team_user');
|
||||
}
|
||||
};
|
||||
@@ -9,6 +9,7 @@ use Filament\Tests\Database\Factories\UserFactory;
|
||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
@@ -36,6 +37,11 @@ class User extends Authenticatable implements FilamentUser, HasTenants, MustVeri
|
||||
return $this->hasMany(Post::class, 'author_id');
|
||||
}
|
||||
|
||||
public function teams(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Team::class);
|
||||
}
|
||||
|
||||
protected static function newFactory()
|
||||
{
|
||||
return UserFactory::new();
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
use Filament\Support\Services\RelationshipJoiner;
|
||||
use Filament\Tests\Models\Team;
|
||||
use Filament\Tests\Models\User;
|
||||
use Filament\Tests\TestCase;
|
||||
use Illuminate\Database\Query\Expression;
|
||||
|
||||
uses(TestCase::class);
|
||||
|
||||
it('can prepare query for no constraints', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
expect($user->teams()->toBase())
|
||||
->distinct->toBeFalse()
|
||||
->getColumns()->toBe([])
|
||||
->orders->toBeNull();
|
||||
|
||||
$query = app(RelationshipJoiner::class)->prepareQueryForNoConstraints($user->teams());
|
||||
|
||||
expect($query->toBase())
|
||||
->distinct->toBeTrue()
|
||||
->getColumns()->toBe(['teams.*'])
|
||||
->orders->toBeNull();
|
||||
|
||||
$query = app(RelationshipJoiner::class)->prepareQueryForNoConstraints(
|
||||
$user
|
||||
->teams()
|
||||
->orderBy('id')
|
||||
->orderBy((new Team)->qualifyColumn('name'))
|
||||
->orderBy('team_user.role')
|
||||
);
|
||||
|
||||
expect($query->toBase())
|
||||
->distinct->toBeTrue()
|
||||
->getColumns()->toBe([
|
||||
(new Team)->qualifyColumn('*'), // Default select...
|
||||
'id', // Select without a qualified table also included just to be sure...
|
||||
// Select for `team.name` not included as that is already included in the `team.*`...
|
||||
'team_user.role', // Select for a qualitified other table included...
|
||||
])
|
||||
->orders->toBe([
|
||||
[
|
||||
'column' => 'id',
|
||||
'direction' => 'asc',
|
||||
],
|
||||
[
|
||||
'column' => 'teams.name',
|
||||
'direction' => 'asc',
|
||||
],
|
||||
[
|
||||
'column' => 'team_user.role',
|
||||
'direction' => 'asc',
|
||||
],
|
||||
]);
|
||||
|
||||
$query = app(RelationshipJoiner::class)->prepareQueryForNoConstraints(
|
||||
$user->teams()->orderByRaw("CASE WHEN role = 'admin' THEN 1 ELSE 2 END")
|
||||
);
|
||||
|
||||
expect($query->toBase())
|
||||
->distinct->toBeTrue()
|
||||
->getColumns()->toBe([
|
||||
(new Team)->qualifyColumn('*'),
|
||||
"CASE WHEN role = 'admin' THEN 1 ELSE 2 END", // Select added from `orderByRaw`...
|
||||
])
|
||||
->orders->toBe([
|
||||
[
|
||||
'type' => 'Raw',
|
||||
'sql' => "CASE WHEN role = 'admin' THEN 1 ELSE 2 END",
|
||||
],
|
||||
]);
|
||||
|
||||
$query = app(RelationshipJoiner::class)->prepareQueryForNoConstraints(
|
||||
$user->teams()->orderBy(new Expression("CASE WHEN role = 'some_other_role' THEN 1 ELSE 2 END"))
|
||||
);
|
||||
|
||||
expect($query->toBase())
|
||||
->distinct->toBeTrue()
|
||||
->getColumns()->toBe([
|
||||
(new Team)->qualifyColumn('*'),
|
||||
"CASE WHEN role = 'user' THEN 1 ELSE 2 END", // Select added from `orderByRaw`...
|
||||
])
|
||||
->orders->toHaveCount(1)
|
||||
->and($query->toBase()->orders[0])
|
||||
->column->getValue($user->teams()->getGrammar())->toBe("CASE WHEN role = 'some_other_role' THEN 1 ELSE 2 END")
|
||||
->direction->toBe('asc');
|
||||
});
|
||||
Reference in New Issue
Block a user