mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
872 lines
20 KiB
PHP
872 lines
20 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pest\Mixins;
|
|
|
|
use BadMethodCallException;
|
|
use Closure;
|
|
use Error;
|
|
use InvalidArgumentException;
|
|
use Pest\Exceptions\InvalidExpectationValue;
|
|
use Pest\Support\Arr;
|
|
use Pest\Support\NullClosure;
|
|
use PHPUnit\Framework\Assert;
|
|
use PHPUnit\Framework\Constraint\Constraint;
|
|
use PHPUnit\Framework\ExpectationFailedException;
|
|
use ReflectionFunction;
|
|
use ReflectionNamedType;
|
|
use SebastianBergmann\Exporter\Exporter;
|
|
use Throwable;
|
|
|
|
/**
|
|
* @internal
|
|
*
|
|
* @template TValue
|
|
*
|
|
* @mixin \Pest\Expectation<TValue>
|
|
*/
|
|
final class Expectation
|
|
{
|
|
/**
|
|
* The exporter instance, if any.
|
|
*
|
|
* @readonly
|
|
*/
|
|
private Exporter|null $exporter = null;
|
|
|
|
/**
|
|
* Creates a new expectation.
|
|
*
|
|
* @param TValue $value
|
|
*/
|
|
public function __construct(
|
|
public mixed $value
|
|
) {
|
|
// ..
|
|
}
|
|
|
|
/**
|
|
* Asserts that two variables have the same type and
|
|
* value. Used on objects, it asserts that two
|
|
* variables reference the same object.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBe(mixed $expected): self
|
|
{
|
|
Assert::assertSame($expected, $this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is empty.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeEmpty(): self
|
|
{
|
|
Assert::assertEmpty($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is true.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeTrue(): self
|
|
{
|
|
Assert::assertTrue($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is truthy.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeTruthy(): self
|
|
{
|
|
Assert::assertTrue((bool) $this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is false.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeFalse(): self
|
|
{
|
|
Assert::assertFalse($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is falsy.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeFalsy(): self
|
|
{
|
|
Assert::assertFalse((bool) $this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is greater than $expected.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeGreaterThan(int|float $expected): self
|
|
{
|
|
Assert::assertGreaterThan($expected, $this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is greater than or equal to $expected.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeGreaterThanOrEqual(int|float $expected): self
|
|
{
|
|
Assert::assertGreaterThanOrEqual($expected, $this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is less than or equal to $expected.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeLessThan(int|float $expected): self
|
|
{
|
|
Assert::assertLessThan($expected, $this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is less than $expected.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeLessThanOrEqual(int|float $expected): self
|
|
{
|
|
Assert::assertLessThanOrEqual($expected, $this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that $needle is an element of the value.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toContain(mixed ...$needles): self
|
|
{
|
|
foreach ($needles as $needle) {
|
|
if (is_string($this->value)) {
|
|
// @phpstan-ignore-next-line
|
|
Assert::assertStringContainsString((string) $needle, $this->value);
|
|
} else {
|
|
if (! is_iterable($this->value)) {
|
|
InvalidExpectationValue::expected('iterable');
|
|
}
|
|
Assert::assertContains($needle, $this->value);
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value starts with $expected.
|
|
*
|
|
* @param non-empty-string $expected
|
|
*@return self<TValue>
|
|
*/
|
|
public function toStartWith(string $expected): self
|
|
{
|
|
if (! is_string($this->value)) {
|
|
InvalidExpectationValue::expected('string');
|
|
}
|
|
|
|
Assert::assertStringStartsWith($expected, $this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value ends with $expected.
|
|
*
|
|
* @param non-empty-string $expected
|
|
*@return self<TValue>
|
|
*/
|
|
public function toEndWith(string $expected): self
|
|
{
|
|
if (! is_string($this->value)) {
|
|
InvalidExpectationValue::expected('string');
|
|
}
|
|
|
|
Assert::assertStringEndsWith($expected, $this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that $number matches value's Length.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toHaveLength(int $number): self
|
|
{
|
|
if (is_string($this->value)) {
|
|
Assert::assertEquals($number, mb_strlen($this->value));
|
|
|
|
return $this;
|
|
}
|
|
|
|
if (is_iterable($this->value)) {
|
|
return $this->toHaveCount($number);
|
|
}
|
|
|
|
if (is_object($this->value)) {
|
|
$array = method_exists($this->value, 'toArray') ? $this->value->toArray() : (array) $this->value;
|
|
|
|
Assert::assertCount($number, $array);
|
|
|
|
return $this;
|
|
}
|
|
|
|
throw new BadMethodCallException('Expectation value length is not countable.');
|
|
}
|
|
|
|
/**
|
|
* Asserts that $count matches the number of elements of the value.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toHaveCount(int $count): self
|
|
{
|
|
if (! is_countable($this->value) && ! is_iterable($this->value)) {
|
|
InvalidExpectationValue::expected('string');
|
|
}
|
|
|
|
Assert::assertCount($count, $this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value contains the property $name.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toHaveProperty(string $name, mixed $value = null): self
|
|
{
|
|
$this->toBeObject();
|
|
|
|
// @phpstan-ignore-next-line
|
|
Assert::assertTrue(property_exists($this->value, $name));
|
|
|
|
if (func_num_args() > 1) {
|
|
/* @phpstan-ignore-next-line */
|
|
Assert::assertEquals($value, $this->value->{$name});
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value contains the provided properties $names.
|
|
*
|
|
* @param iterable<array-key, string> $names
|
|
*@return self<TValue>
|
|
*/
|
|
public function toHaveProperties(iterable $names): self
|
|
{
|
|
foreach ($names as $name) {
|
|
$this->toHaveProperty($name);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that two variables have the same value.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toEqual(mixed $expected): self
|
|
{
|
|
Assert::assertEquals($expected, $this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that two variables have the same value.
|
|
* The contents of $expected and the $this->value are
|
|
* canonicalized before they are compared. For instance, when the two
|
|
* variables $expected and $this->value are arrays, then these arrays
|
|
* are sorted before they are compared. When $expected and $this->value
|
|
* are objects, each object is converted to an array containing all
|
|
* private, protected and public attributes.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toEqualCanonicalizing(mixed $expected): self
|
|
{
|
|
Assert::assertEqualsCanonicalizing($expected, $this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the absolute difference between the value and $expected
|
|
* is lower than $delta.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toEqualWithDelta(mixed $expected, float $delta): self
|
|
{
|
|
Assert::assertEqualsWithDelta($expected, $this->value, $delta);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is one of the given values.
|
|
*
|
|
* @param iterable<int|string, mixed> $values
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeIn(iterable $values): self
|
|
{
|
|
Assert::assertContains($this->value, $values);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is infinite.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeInfinite(): self
|
|
{
|
|
Assert::assertInfinite($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is an instance of $class.
|
|
*
|
|
* @param class-string $class
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeInstanceOf(string $class): self
|
|
{
|
|
Assert::assertInstanceOf($class, $this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is an array.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeArray(): self
|
|
{
|
|
Assert::assertIsArray($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is of type bool.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeBool(): self
|
|
{
|
|
Assert::assertIsBool($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is of type callable.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeCallable(): self
|
|
{
|
|
Assert::assertIsCallable($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is of type float.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeFloat(): self
|
|
{
|
|
Assert::assertIsFloat($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is of type int.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeInt(): self
|
|
{
|
|
Assert::assertIsInt($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is of type iterable.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeIterable(): self
|
|
{
|
|
Assert::assertIsIterable($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is of type numeric.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeNumeric(): self
|
|
{
|
|
Assert::assertIsNumeric($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is of type object.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeObject(): self
|
|
{
|
|
Assert::assertIsObject($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is of type resource.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeResource(): self
|
|
{
|
|
Assert::assertIsResource($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is of type scalar.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeScalar(): self
|
|
{
|
|
Assert::assertIsScalar($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is of type string.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeString(): self
|
|
{
|
|
Assert::assertIsString($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is a JSON string.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeJson(): self
|
|
{
|
|
Assert::assertIsString($this->value);
|
|
|
|
// @phpstan-ignore-next-line
|
|
Assert::assertJson($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is NAN.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeNan(): self
|
|
{
|
|
Assert::assertNan($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is null.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeNull(): self
|
|
{
|
|
Assert::assertNull($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value array has the provided $key.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toHaveKey(string|int $key, mixed $value = null): self
|
|
{
|
|
if (is_object($this->value) && method_exists($this->value, 'toArray')) {
|
|
$array = $this->value->toArray();
|
|
} else {
|
|
$array = (array) $this->value;
|
|
}
|
|
|
|
try {
|
|
Assert::assertTrue(Arr::has($array, $key));
|
|
|
|
/* @phpstan-ignore-next-line */
|
|
} catch (ExpectationFailedException $exception) {
|
|
throw new ExpectationFailedException("Failed asserting that an array has the key '$key'", $exception->getComparisonFailure());
|
|
}
|
|
|
|
if (func_num_args() > 1) {
|
|
Assert::assertEquals($value, Arr::get($array, $key));
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value array has the provided $keys.
|
|
*
|
|
* @param array<int, int|string|array<int-string, mixed>> $keys
|
|
* @return self<TValue>
|
|
*/
|
|
public function toHaveKeys(array $keys): self
|
|
{
|
|
foreach ($keys as $k => $key) {
|
|
if (is_array($key)) {
|
|
$this->toHaveKeys(array_keys(Arr::dot($key, $k.'.')));
|
|
} else {
|
|
$this->toHaveKey($key);
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is a directory.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeDirectory(): self
|
|
{
|
|
if (! is_string($this->value)) {
|
|
InvalidExpectationValue::expected('string');
|
|
}
|
|
|
|
Assert::assertDirectoryExists($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is a directory and is readable.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeReadableDirectory(): self
|
|
{
|
|
if (! is_string($this->value)) {
|
|
InvalidExpectationValue::expected('string');
|
|
}
|
|
|
|
Assert::assertDirectoryIsReadable($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is a directory and is writable.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeWritableDirectory(): self
|
|
{
|
|
if (! is_string($this->value)) {
|
|
InvalidExpectationValue::expected('string');
|
|
}
|
|
|
|
Assert::assertDirectoryIsWritable($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is a file.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeFile(): self
|
|
{
|
|
if (! is_string($this->value)) {
|
|
InvalidExpectationValue::expected('string');
|
|
}
|
|
|
|
Assert::assertFileExists($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is a file and is readable.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeReadableFile(): self
|
|
{
|
|
if (! is_string($this->value)) {
|
|
InvalidExpectationValue::expected('string');
|
|
}
|
|
|
|
Assert::assertFileIsReadable($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value is a file and is writable.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toBeWritableFile(): self
|
|
{
|
|
if (! is_string($this->value)) {
|
|
InvalidExpectationValue::expected('string');
|
|
}
|
|
Assert::assertFileIsWritable($this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value array matches the given array subset.
|
|
*
|
|
* @param iterable<int|string, mixed> $array
|
|
* @return self<TValue>
|
|
*/
|
|
public function toMatchArray(iterable $array): self
|
|
{
|
|
if (is_object($this->value) && method_exists($this->value, 'toArray')) {
|
|
$valueAsArray = $this->value->toArray();
|
|
} else {
|
|
$valueAsArray = (array) $this->value;
|
|
}
|
|
|
|
foreach ($array as $key => $value) {
|
|
Assert::assertArrayHasKey($key, $valueAsArray);
|
|
|
|
Assert::assertEquals(
|
|
$value,
|
|
$valueAsArray[$key],
|
|
sprintf(
|
|
'Failed asserting that an array has a key %s with the value %s.',
|
|
$this->export($key),
|
|
$this->export($valueAsArray[$key]),
|
|
),
|
|
);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value object matches a subset
|
|
* of the properties of an given object.
|
|
*
|
|
* @param iterable<string, mixed> $object
|
|
* @return self<TValue>
|
|
*/
|
|
public function toMatchObject(iterable $object): self
|
|
{
|
|
foreach ((array) $object as $property => $value) {
|
|
if (! is_object($this->value) && ! is_string($this->value)) {
|
|
InvalidExpectationValue::expected('object|string');
|
|
}
|
|
|
|
Assert::assertTrue(property_exists($this->value, $property));
|
|
|
|
/* @phpstan-ignore-next-line */
|
|
$propertyValue = $this->value->{$property};
|
|
Assert::assertEquals(
|
|
$value,
|
|
$propertyValue,
|
|
sprintf(
|
|
'Failed asserting that an object has a property %s with the value %s.',
|
|
$this->export($property),
|
|
$this->export($propertyValue),
|
|
),
|
|
);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value matches a regular expression.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toMatch(string $expression): self
|
|
{
|
|
if (! is_string($this->value)) {
|
|
InvalidExpectationValue::expected('string');
|
|
}
|
|
Assert::assertMatchesRegularExpression($expression, $this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that the value matches a constraint.
|
|
*
|
|
* @return self<TValue>
|
|
*/
|
|
public function toMatchConstraint(Constraint $constraint): self
|
|
{
|
|
Assert::assertThat($this->value, $constraint);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @param class-string $class
|
|
* @return self<TValue>
|
|
*/
|
|
public function toContainOnlyInstancesOf(string $class): self
|
|
{
|
|
if (! is_iterable($this->value)) {
|
|
InvalidExpectationValue::expected('iterable');
|
|
}
|
|
|
|
Assert::assertContainsOnlyInstancesOf($class, $this->value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Asserts that executing value throws an exception.
|
|
*
|
|
* @param (Closure(Throwable): mixed)|string $exception
|
|
* @return self<TValue>
|
|
*/
|
|
public function toThrow(callable|string $exception, string $exceptionMessage = null): self
|
|
{
|
|
$callback = NullClosure::create();
|
|
|
|
if ($exception instanceof Closure) {
|
|
$callback = $exception;
|
|
$parameters = (new ReflectionFunction($exception))->getParameters();
|
|
|
|
if (1 !== count($parameters)) {
|
|
throw new InvalidArgumentException('The given closure must have a single parameter type-hinted as the class string.');
|
|
}
|
|
|
|
if (! ($type = $parameters[0]->getType()) instanceof ReflectionNamedType) {
|
|
throw new InvalidArgumentException('The given closure\'s parameter must be type-hinted as the class string.');
|
|
}
|
|
|
|
$exception = $type->getName();
|
|
}
|
|
|
|
try {
|
|
($this->value)();
|
|
} catch (Throwable $e) {
|
|
if (! class_exists($exception)) {
|
|
if ($e instanceof Error && $e->getMessage() === "Class \"$exception\" not found") {
|
|
throw $e;
|
|
}
|
|
|
|
Assert::assertStringContainsString($exception, $e->getMessage());
|
|
|
|
return $this;
|
|
}
|
|
|
|
if ($exceptionMessage !== null) {
|
|
Assert::assertStringContainsString($exceptionMessage, $e->getMessage());
|
|
}
|
|
|
|
Assert::assertInstanceOf($exception, $e);
|
|
$callback($e);
|
|
|
|
return $this;
|
|
}
|
|
|
|
if (! class_exists($exception)) {
|
|
throw new ExpectationFailedException("Exception with message \"$exception\" not thrown.");
|
|
}
|
|
|
|
throw new ExpectationFailedException("Exception \"$exception\" not thrown.");
|
|
}
|
|
|
|
/**
|
|
* Exports the given value.
|
|
*/
|
|
private function export(mixed $value): string
|
|
{
|
|
if ($this->exporter === null) {
|
|
$this->exporter = new Exporter();
|
|
}
|
|
|
|
return $this->exporter->export($value);
|
|
}
|
|
}
|