đ Corresponding Assertions â
toHaveCorresponding() â
Check the correspondence between a class/enum/interface/trait and a mask. To build the mask, you have access to the description of the current class. Correspondence rules can be used in many scenarios:
- If a model has a repository interface
- If a model has a policy with the same name
- If your controllers have associated queries or resources
For example, you can check whether each unit test class has a corresponding class in your project:
$this
->allClasses()
->fromDir('tests/Unit')
->should(
static fn(Expr $assert): Expr => $assert
->toHaveCorrespondingClass(
static fn (ClassDescription $classDescription): string => preg_replace(
'/^(.+?)\\\Tests\\\Unit\\\(.+?)(Test)$/',
'$1\\\$2',
$classDescription->namespace,
)
),
);toHaveCorrespondingClass() â
Similar to toHaveCorresponding(), but for matching with a class.
toHaveCorrespondingEnum() â
Similar to toHaveCorresponding(), but for matching with an enum.
toHaveCorrespondingFile() â
Check the correspondence between a class and a file on disk. The assertion passes if file_exists() returns true.
This is useful to ensure that each class has a corresponding configuration file (e.g., migration, or any other file artifact).
$this
->allClasses()
->fromDir('src/Enums')
->should(
static fn(Expr $assert): Expr => $assert
->toHaveCorrespondingFile(
static fn (ClassDescription $classDescription): string => sprintf(
'%s/lang/en/enums/%s.php',
dirname(__DIR__),
$classDescription->name,
),
),
);toNotHaveCorrespondingFile() â
Opposite of toHaveCorrespondingFile(). Check that no corresponding file exists on disk.
The assertion passes if file_exists() returns false.
$this
->allClasses()
->fromDir('src/Enums')
->should(
static fn(Expr $assert): Expr => $assert
->toNotHaveCorrespondingFile(
static fn (ClassDescription $classDescription): string => sprintf(
'%s/lang/en/enums/%s.php',
dirname(__DIR__),
$classDescription->name,
),
),
);Violation message
Resource name Foo must not have corresponding file /path/to/Foo.phptoHaveCorrespondingInterface() â
Similar to toHaveCorresponding(), but for matching with an interface.
toHaveCorrespondingTrait() â
Similar to toHaveCorresponding(), but for matching with a trait.