From 2a5683122ea57cc01ed7b3387a5a6ed15dba61b7 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Thu, 20 Jul 2023 13:43:10 +0200 Subject: [PATCH] Add and test excluding models from discovery --- src/Database/Concerns/DealsWithModels.php | 4 +++- tests/PostgresRLSTest.php | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Database/Concerns/DealsWithModels.php b/src/Database/Concerns/DealsWithModels.php index 3839bd07..85c531cd 100644 --- a/src/Database/Concerns/DealsWithModels.php +++ b/src/Database/Concerns/DealsWithModels.php @@ -16,6 +16,8 @@ trait DealsWithModels */ public static Closure|null $modelDiscoveryOverride = null; + public static array $modelsExcludedFromDiscovery = []; + /** * Discover all models in the directories configured in 'tenancy.rls.model_directories'. */ @@ -41,7 +43,7 @@ trait DealsWithModels } return null; - }, iterator_to_array($modelFiles)), fn (object|null $class) => $class instanceof Model); + }, iterator_to_array($modelFiles)), fn (object|null $object) => $object instanceof Model && ! in_array($object::class, static::$modelsExcludedFromDiscovery)); } /** diff --git a/tests/PostgresRLSTest.php b/tests/PostgresRLSTest.php index ec5669cc..db689cef 100644 --- a/tests/PostgresRLSTest.php +++ b/tests/PostgresRLSTest.php @@ -249,9 +249,19 @@ test('model discovery gets the models correctly', function() { // Check that the Post and ScopedComment models are found in the directory $expectedModels = [Post::class, ScopedComment::class]; - $foundModels = array_filter(tenancy()->getModels(), fn (Model $model) => in_array($model::class, $expectedModels)); + $foundModels = fn () => collect(tenancy()->getModels())->filter(fn (Model $model) => in_array($model::class, $expectedModels)); - expect($foundModels)->toHaveCount(count($expectedModels)); + expect($foundModels()->map(fn (Model $model) => $model::class)->values()) + ->toHaveCount(count($expectedModels)) + ->toContain(...$expectedModels); + + $expectedModels = [Post::class]; + $excludedModels = tenancy()::$modelsExcludedFromDiscovery = [ScopedComment::class]; + + expect($foundModels()->map(fn (Model $model) => $model::class)->values()) + ->toHaveCount(count($expectedModels)) + ->toContain(...$expectedModels) + ->not()->toContain(...$excludedModels); }); trait UsesUuidAsPrimaryKey