Merge pull request #934 from hungthai1401/to_have_attribute_expectation

[2.x] Add `toHaveAttribute` expectation
This commit is contained in:
Nuno Maduro
2023-09-03 23:18:47 +01:00
committed by GitHub
6 changed files with 83 additions and 0 deletions

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Pest;
use Attribute;
use BadMethodCallException;
use Closure;
use InvalidArgumentException;
@ -846,4 +847,19 @@ final class Expectation
return $this;
}
/**
* Asserts that the given expectation target to have the given attribute.
*
* @param class-string<Attribute> $attribute
*/
public function toHaveAttribute(string $attribute): ArchExpectation
{
return Targeted::make(
$this,
fn (ObjectDescription $object): bool => $object->reflectionClass->getAttributes($attribute) !== [],
"to have attribute '{$attribute}'",
FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')),
);
}
}