mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
feat: adds toUseTraits expectation
This commit is contained in:
@ -611,6 +611,41 @@ final class Expectation
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the given expectation target to use the given trait.
|
||||
*
|
||||
* @param string $trait
|
||||
*/
|
||||
public function toUseTrait(string $trait): ArchExpectation
|
||||
{
|
||||
return $this->toUseTraits($trait);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the given expectation target to use the given traits.
|
||||
*
|
||||
* @param array<int, string>|string $traits
|
||||
*/
|
||||
public function toUseTraits(array|string $traits): ArchExpectation
|
||||
{
|
||||
$traits = is_array($traits) ? $traits : [$traits];
|
||||
|
||||
return Targeted::make(
|
||||
$this,
|
||||
function (ObjectDescription $object) use ($traits): bool {
|
||||
foreach ($traits as $trait) {
|
||||
if (! in_array($trait, $object->reflectionClass->getTraitNames(), true)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
"to use traits '".implode("', '", $traits)."'",
|
||||
FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the given expectation target to not implement any interfaces.
|
||||
*/
|
||||
@ -668,6 +703,8 @@ final class Expectation
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Asserts that the given expectation target to implement the given interfaces.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user