You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I can make expectations on a namespace, and the built-in expectations like ->toBeClasses() will correctly be checked for all of the items inside the namespace, but there's no way that I can find to then make other expectations on the classes in the namespace.
X/Y Problem
What I'm trying to achieve is an expectation that all of the Model classes in namespace exist in a constant that is used for morph maps:
class DatabaseServiceProvider extends ServiceProvider
{
publicconstarrayOPTION_MORPHS = [
'foo' => \App\Models\Options\Foo::class,
'bar' => \App\Models\Options\Bar::class,
'baz' => \App\Models\Options\Baz::class,
];
publicfunctionboot(): void
{
Relation::morphMap(self::OPTION_MORPHS);
}
}
// Then the testarch('option models are registered in database morph map')
->expect('App\Models\Options')
->toBeIn(\App\Providers\DatabaseServiceProvider::OPTION_MORPHS);
That doesn't work because 'App\Models\Options' isn't in the array, so I went "Ok, makes sense" so I first tried to add ->each to iterate over the classes, and when that didn't work I added ->classes() to target the classes, but still no.
In the short term I've added this:
expect()->extend('toBeMapped', function (array$map): \Pest\Expectation {
Targeted::make(
$this,
fn (ObjectDescription$object): bool => in_array($object->name, $map),
'to be in map',
FileLineFinder::where(fn (string$line): bool => str_contains($line, 'class')),
);
return$this;
});
Which works, but it's using internal classes (Targeted and FileLineFinder), and doesn't format the error correctly - it says the filename doesn't exist in the map, instead of the class, which is fine for my purposes.
So what are the options here? Adding each class manually to the test won't work because the test is meant to catch when a new class is added without being added to the morph map.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I can make expectations on a namespace, and the built-in expectations like
->toBeClasses()
will correctly be checked for all of the items inside the namespace, but there's no way that I can find to then make other expectations on the classes in the namespace.X/Y Problem
What I'm trying to achieve is an expectation that all of the Model classes in namespace exist in a constant that is used for morph maps:
That doesn't work because
'App\Models\Options'
isn't in the array, so I went "Ok, makes sense" so I first tried to add->each
to iterate over the classes, and when that didn't work I added->classes()
to target the classes, but still no.In the short term I've added this:
Which works, but it's using internal classes (
Targeted
andFileLineFinder
), and doesn't format the error correctly - it says the filename doesn't exist in the map, instead of the class, which is fine for my purposes.So what are the options here? Adding each class manually to the test won't work because the test is meant to catch when a new class is added without being added to the morph map.
Beta Was this translation helpful? Give feedback.
All reactions