1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 12:24:04 +00:00

Add and test excluding models from discovery

This commit is contained in:
lukinovec 2023-07-20 13:43:10 +02:00
parent a664860d63
commit 2a5683122e
2 changed files with 15 additions and 3 deletions

View file

@ -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));
}
/**

View file

@ -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