đšī¸ Other Assertions â
toHaveAnonymousClass() â
Assert that a class-like (class, enum, interface, trait) contains at least one anonymous class.
Important
- With
allClasses(): the anonymous class MUST be encapsulated inside the analyzed class-like. - With
allScripts(): detects any anonymous class present in the script, regardless of context.
Example with allClasses() â
$this
->allClasses()
->fromRaw('<?php class Foo { public function bar() { return new class {}; } }')
->should(fn(Expr $expr) => $expr->toHaveAnonymousClass());Example with allScripts() â
$this
->allScripts()
->fromRaw('<?php $obj = new class {};')
->should(fn(ExprScript $expr) => $expr->toHaveAnonymousClass());toNotHaveAnonymousClass() â
Assert that a class-like does not contain any anonymous class.
Example with allClasses() â
$this
->allClasses()
->fromRaw('<?php class Foo { public function bar() {} }')
->should(fn(Expr $expr) => $expr->toNotHaveAnonymousClass());Example with allScripts() â
$this
->allScripts()
->fromRaw('<?php function foo() {}')
->should(fn(ExprScript $expr) => $expr->toNotHaveAnonymousClass());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.
toHaveCorrespondingInterface() â
Similar to toHaveCorresponding(), but for matching with an interface.
toHaveCorrespondingTrait() â
Similar to toHaveCorresponding(), but for matching with a trait.
toUseStrictTypes() â
$this
->allClasses()
->fromRaw('<?php declare(strict_types=1); class Foo {}')
->should(fn(Expr $expr) => $expr->toUseStrictTypes());toUseDeclare() â
$this
->allClasses()
->fromRaw("<?php declare(encoding='ISO-8859-1'); class Foo {}")
->should(fn(Expr $expr) => $expr->toUseDeclare('encoding', 'ISO-8859-1'));toBeInOneOfTheNamespaces() â
Allows you to specifically target classes contained in a namespace.
INFO
Anonymous classes cannot have namespaces.
You can use regexes to select namespaces.
$this
->allClasses()
->fromDir('tests')
->that(
fn(Expr $expr) => $expr
->toBeInOneOfTheNamespaces('Tests\Unit.+')
)
->should(fn(Expr $expr) => $expr /* our rules */);notToBeInOneOfTheNamespaces() â
Allows you to specifically target classes not contained in a namespace.
INFO
Anonymous classes cannot have namespaces.
You can use regexes to select namespaces.
$this
->allClasses()
->fromDir('tests')
->that(
fn(Expr $expr) => $expr
->notToBeInOneOfTheNamespaces('Tests\Unit.+')
)
->should(fn(Expr $expr) => $expr /* our rules */);toUseInclude() â
Allows an inclusion (include*/require*) in a script or class.
use StructuraPhp\Structura\Enums\IncludeType;
$this
->allScripts()
->fromRaw('<?php require "foo.php";')
->should(fn(ExprScript $expr) => $expr->toUseInclude(IncludeType::Require));toNotUseInclude() â
$this
->allScripts()
->fromRaw('<?php require "foo.php";')
->should(fn(ExprScript $expr) => $expr->toNotUseInclude());toHaveFilePermission() â
Assert that a script or class file has specific Unix file permissions.
$this
->allScripts()
->fromDir('src')
->should(fn(ExprScript $expr) => $expr->toHaveFilePermission('0644'));toReturnArray() â
Assert that a PHP script returns an array at the root level using a return statement.
INFO
- The script must have a
returnstatement at the root level (not nested inside functions, classes, matches or switch). - The returned value must be an array literal using the
[]syntax orarray()construct (must not be in a variable or a function returning an array).
$this
->allScripts()
->fromRaw('<?php return [];')
->should(fn(ExprScript $expr) => $expr->toReturnArray());