refacto: pipes

This commit is contained in:
Nuno Maduro
2021-12-05 14:03:09 +00:00
parent 6c3a8be049
commit 98db677646
9 changed files with 239 additions and 271 deletions

View File

@ -7,7 +7,7 @@ namespace Pest;
use BadMethodCallException;
use Closure;
use InvalidArgumentException;
use Pest\Concerns\RetrievesValues;
use Pest\Concerns\Retrievable;
use Pest\Exceptions\InvalidExpectationValue;
use Pest\Support\Arr;
use Pest\Support\NullClosure;
@ -26,10 +26,8 @@ use Throwable;
*
* @mixin Expectation<TValue>
*/
final class CoreExpectation
final class BaseExpectation
{
use RetrievesValues;
/**
* The exporter instance, if any.
*
@ -53,9 +51,9 @@ final class CoreExpectation
* value. Used on objects, it asserts that two
* variables reference the same object.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBe(mixed $expected): CoreExpectation
public function toBe(mixed $expected): BaseExpectation
{
Assert::assertSame($expected, $this->value);
@ -65,9 +63,9 @@ final class CoreExpectation
/**
* Asserts that the value is empty.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeEmpty(): CoreExpectation
public function toBeEmpty(): BaseExpectation
{
Assert::assertEmpty($this->value);
@ -77,9 +75,9 @@ final class CoreExpectation
/**
* Asserts that the value is true.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeTrue(): CoreExpectation
public function toBeTrue(): BaseExpectation
{
Assert::assertTrue($this->value);
@ -89,9 +87,9 @@ final class CoreExpectation
/**
* Asserts that the value is truthy.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeTruthy(): CoreExpectation
public function toBeTruthy(): BaseExpectation
{
Assert::assertTrue((bool) $this->value);
@ -101,9 +99,9 @@ final class CoreExpectation
/**
* Asserts that the value is false.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeFalse(): CoreExpectation
public function toBeFalse(): BaseExpectation
{
Assert::assertFalse($this->value);
@ -113,9 +111,9 @@ final class CoreExpectation
/**
* Asserts that the value is falsy.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeFalsy(): CoreExpectation
public function toBeFalsy(): BaseExpectation
{
Assert::assertFalse((bool) $this->value);
@ -125,9 +123,9 @@ final class CoreExpectation
/**
* Asserts that the value is greater than $expected.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeGreaterThan(int|float $expected): CoreExpectation
public function toBeGreaterThan(int|float $expected): BaseExpectation
{
Assert::assertGreaterThan($expected, $this->value);
@ -137,9 +135,9 @@ final class CoreExpectation
/**
* Asserts that the value is greater than or equal to $expected.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeGreaterThanOrEqual(int|float $expected): CoreExpectation
public function toBeGreaterThanOrEqual(int|float $expected): BaseExpectation
{
Assert::assertGreaterThanOrEqual($expected, $this->value);
@ -149,9 +147,9 @@ final class CoreExpectation
/**
* Asserts that the value is less than or equal to $expected.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeLessThan(int|float $expected): CoreExpectation
public function toBeLessThan(int|float $expected): BaseExpectation
{
Assert::assertLessThan($expected, $this->value);
@ -161,9 +159,9 @@ final class CoreExpectation
/**
* Asserts that the value is less than $expected.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeLessThanOrEqual(int|float $expected): CoreExpectation
public function toBeLessThanOrEqual(int|float $expected): BaseExpectation
{
Assert::assertLessThanOrEqual($expected, $this->value);
@ -173,9 +171,9 @@ final class CoreExpectation
/**
* Asserts that $needle is an element of the value.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toContain(mixed ...$needles): CoreExpectation
public function toContain(mixed ...$needles): BaseExpectation
{
foreach ($needles as $needle) {
if (is_string($this->value)) {
@ -195,11 +193,11 @@ final class CoreExpectation
/**
* Asserts that the value starts with $expected.
*
* @return CoreExpectation<TValue>
*
* @param non-empty-string $expected
*
*@return BaseExpectation<TValue>
*/
public function toStartWith(string $expected): CoreExpectation
public function toStartWith(string $expected): BaseExpectation
{
if (!is_string($this->value)) {
InvalidExpectationValue::expected('string');
@ -213,11 +211,11 @@ final class CoreExpectation
/**
* Asserts that the value ends with $expected.
*
* @return CoreExpectation<TValue>
*
* @param non-empty-string $expected
*
*@return BaseExpectation<TValue>
*/
public function toEndWith(string $expected): CoreExpectation
public function toEndWith(string $expected): BaseExpectation
{
if (!is_string($this->value)) {
InvalidExpectationValue::expected('string');
@ -231,9 +229,9 @@ final class CoreExpectation
/**
* Asserts that $number matches value's Length.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toHaveLength(int $number): CoreExpectation
public function toHaveLength(int $number): BaseExpectation
{
if (is_string($this->value)) {
Assert::assertEquals($number, mb_strlen($this->value));
@ -263,9 +261,9 @@ final class CoreExpectation
/**
* Asserts that $count matches the number of elements of the value.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toHaveCount(int $count): CoreExpectation
public function toHaveCount(int $count): BaseExpectation
{
if (!is_countable($this->value) && !is_iterable($this->value)) {
InvalidExpectationValue::expected('string');
@ -279,9 +277,9 @@ final class CoreExpectation
/**
* Asserts that the value contains the property $name.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toHaveProperty(string $name, mixed $value = null): CoreExpectation
public function toHaveProperty(string $name, mixed $value = null): BaseExpectation
{
$this->toBeObject();
@ -299,11 +297,11 @@ final class CoreExpectation
/**
* Asserts that the value contains the provided properties $names.
*
* @return CoreExpectation<TValue>
*
* @param iterable<array-key, string> $names
*
*@return BaseExpectation<TValue>
*/
public function toHaveProperties(iterable $names): CoreExpectation
public function toHaveProperties(iterable $names): BaseExpectation
{
foreach ($names as $name) {
$this->toHaveProperty($name);
@ -315,9 +313,9 @@ final class CoreExpectation
/**
* Asserts that two variables have the same value.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toEqual(mixed $expected): CoreExpectation
public function toEqual(mixed $expected): BaseExpectation
{
Assert::assertEquals($expected, $this->value);
@ -333,9 +331,9 @@ final class CoreExpectation
* are objects, each object is converted to an array containing all
* private, protected and public attributes.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toEqualCanonicalizing(mixed $expected): CoreExpectation
public function toEqualCanonicalizing(mixed $expected): BaseExpectation
{
Assert::assertEqualsCanonicalizing($expected, $this->value);
@ -346,9 +344,9 @@ final class CoreExpectation
* Asserts that the absolute difference between the value and $expected
* is lower than $delta.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toEqualWithDelta(mixed $expected, float $delta): CoreExpectation
public function toEqualWithDelta(mixed $expected, float $delta): BaseExpectation
{
Assert::assertEqualsWithDelta($expected, $this->value, $delta);
@ -360,9 +358,9 @@ final class CoreExpectation
*
* @param iterable<int|string, mixed> $values
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeIn(iterable $values): CoreExpectation
public function toBeIn(iterable $values): BaseExpectation
{
Assert::assertContains($this->value, $values);
@ -372,9 +370,9 @@ final class CoreExpectation
/**
* Asserts that the value is infinite.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeInfinite(): CoreExpectation
public function toBeInfinite(): BaseExpectation
{
Assert::assertInfinite($this->value);
@ -386,9 +384,9 @@ final class CoreExpectation
*
* @param class-string $class
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeInstanceOf(string $class): CoreExpectation
public function toBeInstanceOf(string $class): BaseExpectation
{
Assert::assertInstanceOf($class, $this->value);
@ -398,9 +396,9 @@ final class CoreExpectation
/**
* Asserts that the value is an array.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeArray(): CoreExpectation
public function toBeArray(): BaseExpectation
{
Assert::assertIsArray($this->value);
@ -410,9 +408,9 @@ final class CoreExpectation
/**
* Asserts that the value is of type bool.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeBool(): CoreExpectation
public function toBeBool(): BaseExpectation
{
Assert::assertIsBool($this->value);
@ -422,9 +420,9 @@ final class CoreExpectation
/**
* Asserts that the value is of type callable.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeCallable(): CoreExpectation
public function toBeCallable(): BaseExpectation
{
Assert::assertIsCallable($this->value);
@ -434,9 +432,9 @@ final class CoreExpectation
/**
* Asserts that the value is of type float.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeFloat(): CoreExpectation
public function toBeFloat(): BaseExpectation
{
Assert::assertIsFloat($this->value);
@ -446,9 +444,9 @@ final class CoreExpectation
/**
* Asserts that the value is of type int.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeInt(): CoreExpectation
public function toBeInt(): BaseExpectation
{
Assert::assertIsInt($this->value);
@ -458,9 +456,9 @@ final class CoreExpectation
/**
* Asserts that the value is of type iterable.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeIterable(): CoreExpectation
public function toBeIterable(): BaseExpectation
{
Assert::assertIsIterable($this->value);
@ -470,9 +468,9 @@ final class CoreExpectation
/**
* Asserts that the value is of type numeric.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeNumeric(): CoreExpectation
public function toBeNumeric(): BaseExpectation
{
Assert::assertIsNumeric($this->value);
@ -482,9 +480,9 @@ final class CoreExpectation
/**
* Asserts that the value is of type object.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeObject(): CoreExpectation
public function toBeObject(): BaseExpectation
{
Assert::assertIsObject($this->value);
@ -494,9 +492,9 @@ final class CoreExpectation
/**
* Asserts that the value is of type resource.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeResource(): CoreExpectation
public function toBeResource(): BaseExpectation
{
Assert::assertIsResource($this->value);
@ -506,9 +504,9 @@ final class CoreExpectation
/**
* Asserts that the value is of type scalar.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeScalar(): CoreExpectation
public function toBeScalar(): BaseExpectation
{
Assert::assertIsScalar($this->value);
@ -518,9 +516,9 @@ final class CoreExpectation
/**
* Asserts that the value is of type string.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeString(): CoreExpectation
public function toBeString(): BaseExpectation
{
Assert::assertIsString($this->value);
@ -530,9 +528,9 @@ final class CoreExpectation
/**
* Asserts that the value is a JSON string.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeJson(): CoreExpectation
public function toBeJson(): BaseExpectation
{
Assert::assertIsString($this->value);
@ -545,9 +543,9 @@ final class CoreExpectation
/**
* Asserts that the value is NAN.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeNan(): CoreExpectation
public function toBeNan(): BaseExpectation
{
Assert::assertNan($this->value);
@ -557,9 +555,9 @@ final class CoreExpectation
/**
* Asserts that the value is null.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeNull(): CoreExpectation
public function toBeNull(): BaseExpectation
{
Assert::assertNull($this->value);
@ -569,9 +567,9 @@ final class CoreExpectation
/**
* Asserts that the value array has the provided $key.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toHaveKey(string|int $key, mixed $value = null): CoreExpectation
public function toHaveKey(string|int $key, mixed $value = null): BaseExpectation
{
if (is_object($this->value) && method_exists($this->value, 'toArray')) {
$array = $this->value->toArray();
@ -599,9 +597,9 @@ final class CoreExpectation
*
* @param array<int, int|string> $keys
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toHaveKeys(array $keys): CoreExpectation
public function toHaveKeys(array $keys): BaseExpectation
{
foreach ($keys as $key) {
$this->toHaveKey($key);
@ -613,9 +611,9 @@ final class CoreExpectation
/**
* Asserts that the value is a directory.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeDirectory(): CoreExpectation
public function toBeDirectory(): BaseExpectation
{
if (!is_string($this->value)) {
InvalidExpectationValue::expected('string');
@ -629,9 +627,9 @@ final class CoreExpectation
/**
* Asserts that the value is a directory and is readable.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeReadableDirectory(): CoreExpectation
public function toBeReadableDirectory(): BaseExpectation
{
if (!is_string($this->value)) {
InvalidExpectationValue::expected('string');
@ -645,9 +643,9 @@ final class CoreExpectation
/**
* Asserts that the value is a directory and is writable.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeWritableDirectory(): CoreExpectation
public function toBeWritableDirectory(): BaseExpectation
{
if (!is_string($this->value)) {
InvalidExpectationValue::expected('string');
@ -661,9 +659,9 @@ final class CoreExpectation
/**
* Asserts that the value is a file.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeFile(): CoreExpectation
public function toBeFile(): BaseExpectation
{
if (!is_string($this->value)) {
InvalidExpectationValue::expected('string');
@ -677,9 +675,9 @@ final class CoreExpectation
/**
* Asserts that the value is a file and is readable.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeReadableFile(): CoreExpectation
public function toBeReadableFile(): BaseExpectation
{
if (!is_string($this->value)) {
InvalidExpectationValue::expected('string');
@ -693,9 +691,9 @@ final class CoreExpectation
/**
* Asserts that the value is a file and is writable.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toBeWritableFile(): CoreExpectation
public function toBeWritableFile(): BaseExpectation
{
if (!is_string($this->value)) {
InvalidExpectationValue::expected('string');
@ -710,9 +708,9 @@ final class CoreExpectation
*
* @param iterable<int|string, mixed> $array
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toMatchArray(iterable|object $array): CoreExpectation
public function toMatchArray(iterable|object $array): BaseExpectation
{
if (is_object($this->value) && method_exists($this->value, 'toArray')) {
$valueAsArray = $this->value->toArray();
@ -743,9 +741,9 @@ final class CoreExpectation
*
* @param iterable<string, mixed>|object $object
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toMatchObject(iterable|object $object): CoreExpectation
public function toMatchObject(iterable|object $object): BaseExpectation
{
foreach ((array) $object as $property => $value) {
if (!is_object($this->value) && !is_string($this->value)) {
@ -773,9 +771,9 @@ final class CoreExpectation
/**
* Asserts that the value matches a regular expression.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toMatch(string $expression): CoreExpectation
public function toMatch(string $expression): BaseExpectation
{
if (!is_string($this->value)) {
InvalidExpectationValue::expected('string');
@ -788,9 +786,9 @@ final class CoreExpectation
/**
* Asserts that the value matches a constraint.
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toMatchConstraint(Constraint $constraint): CoreExpectation
public function toMatchConstraint(Constraint $constraint): BaseExpectation
{
Assert::assertThat($this->value, $constraint);
@ -802,9 +800,9 @@ final class CoreExpectation
*
* @param (Closure(Throwable): mixed)|string $exception
*
* @return CoreExpectation<TValue>
* @return BaseExpectation<TValue>
*/
public function toThrow(callable|string $exception, string $exceptionMessage = null): CoreExpectation
public function toThrow(callable|string $exception, string $exceptionMessage = null): BaseExpectation
{
$callback = NullClosure::create();

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Pest\Concerns;
use BadMethodCallException;
use Closure;
/**
@ -19,51 +18,14 @@ trait Extendable
*/
private static array $extends = [];
/** @var array<string, array<Closure(Closure, mixed ...$arguments): void>> */
private static array $pipes = [];
/**
* Register a new extend.
*/
public static function extend(string $name, Closure $extend): void
public function extend(string $name, Closure $extend): void
{
static::$extends[$name] = $extend;
}
/**
* Register a pipe to be applied before an expectation is checked.
*/
public static function pipe(string $name, Closure $pipe): void
{
self::$pipes[$name][] = $pipe;
}
/**
* Register an interceptor that should replace an existing expectation.
*
* @param string|Closure(mixed $value, mixed ...$arguments):bool $filter
*/
public static function intercept(string $name, string|Closure $filter, Closure $handler): void
{
if (is_string($filter)) {
$filter = function ($value) use ($filter): bool {
return $value instanceof $filter;
};
}
self::pipe($name, function ($next, ...$arguments) use ($handler, $filter) {
/* @phpstan-ignore-next-line */
if ($filter($this->value, ...$arguments)) {
//@phpstan-ignore-next-line
$handler->bindTo($this, get_class($this))(...$arguments);
return;
}
$next();
});
}
/**
* Checks if given extend name is registered.
*/
@ -71,29 +33,4 @@ trait Extendable
{
return array_key_exists($name, static::$extends);
}
/**
* @return array<int, Closure>
*/
private function pipes(string $name, object $context, string $scope): array
{
return array_map(fn (Closure $pipe) => $pipe->bindTo($context, $scope), self::$pipes[$name] ?? []);
}
/**
* Dynamically handle calls to the class.
*
* @param array<int, mixed> $parameters
*/
public function __call(string $method, array $parameters): mixed
{
if (!static::hasExtend($method)) {
throw new BadMethodCallException("$method is not a callable method name.");
}
/** @var Closure $extend */
$extend = static::$extends[$method]->bindTo($this, static::class);
return $extend(...$parameters);
}
}

65
src/Concerns/Pipeable.php Normal file
View File

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace Pest\Concerns;
use Closure;
use Pest\Expectation;
/**
* @internal
*/
trait Pipeable
{
/**
* The list of pipes.
*
* @var array<string, array<Closure(Closure, mixed ...$arguments): void>>
*/
private static array $pipes = [];
/**
* Register a pipe to be applied before an expectation is checked.
*/
public function pipe(string $name, Closure $pipe): void
{
self::$pipes[$name][] = $pipe;
}
/**
* Register an interceptor that should replace an existing expectation.
*
* @param string|Closure(mixed $value, mixed ...$arguments):bool $filter
*/
public function intercept(string $name, string|Closure $filter, Closure $handler): void
{
if (is_string($filter)) {
$filter = function ($value) use ($filter): bool {
return $value instanceof $filter;
};
}
$this->pipe($name, function ($next, ...$arguments) use ($handler, $filter) {
/* @phpstan-ignore-next-line */
if ($filter($this->value, ...$arguments)) {
//@phpstan-ignore-next-line
$handler->bindTo($this, get_class($this))(...$arguments);
return;
}
$next();
});
}
/**
* Get th list of pipes by the given name.
*
* @return array<int, Closure>
*/
private function pipes(string $name, object $context, string $scope): array
{
return array_map(fn (Closure $pipe) => $pipe->bindTo($context, $scope), self::$pipes[$name] ?? []);
}
}

View File

@ -7,7 +7,7 @@ namespace Pest\Concerns;
/**
* @internal
*/
trait RetrievesValues
trait Retrievable
{
/**
* @template TRetrievableValue

View File

@ -7,7 +7,8 @@ namespace Pest;
use BadMethodCallException;
use Closure;
use Pest\Concerns\Extendable;
use Pest\Concerns\RetrievesValues;
use Pest\Concerns\Pipeable;
use Pest\Concerns\Retrievable;
use Pest\Exceptions\InvalidExpectationValue;
use Pest\Exceptions\PipeException;
use Pest\Support\ExpectationPipeline;
@ -22,25 +23,23 @@ use PHPUnit\Framework\ExpectationFailedException;
* @property Expectation $not Creates the opposite expectation.
* @property Each $each Creates an expectation on each element on the traversable value.
*
* @mixin CoreExpectation<TValue>
* @mixin BaseExpectation<TValue>
*/
final class Expectation
{
use RetrievesValues, Extendable {
__call as __extendsCall;
}
/** @var CoreExpectation<TValue> */
private CoreExpectation $coreExpectation;
use Retrievable;
use Pipeable;
use Extendable;
/**
* Creates a new expectation.
*
* @param TValue $value
*/
public function __construct(mixed $value)
{
$this->coreExpectation = new CoreExpectation($value);
public function __construct(
public mixed $value
) {
// ..
}
/**
@ -257,8 +256,7 @@ final class Expectation
}
/**
* Dynamically handle calls to the class or
* creates a new higher order expectation.
* Dynamically calls methods on the class or creates a new higher order expectation.
*
* @param array<int, mixed> $parameters
*
@ -266,7 +264,7 @@ final class Expectation
*/
public function __call(string $method, array $parameters): Expectation|HigherOrderExpectation
{
if (!$this->hasExpectation($method)) {
if (!self::hasMethod($method)) {
/* @phpstan-ignore-next-line */
return new HigherOrderExpectation($this, $this->value->$method(...$parameters));
}
@ -281,9 +279,9 @@ final class Expectation
private function getExpectationClosure(string $name): Closure
{
if (method_exists($this->coreExpectation, $name)) {
if (method_exists(BaseExpectation::class, $name)) {
//@phpstan-ignore-next-line
return Closure::fromCallable([$this->coreExpectation, $name]);
return Closure::fromCallable([new BaseExpectation($this->value), $name]);
}
if (self::hasExtend($name)) {
@ -297,32 +295,14 @@ final class Expectation
throw PipeException::expectationNotFound($name);
}
private function hasExpectation(string $name): bool
{
if (method_exists($this->coreExpectation, $name)) {
return true;
}
if (self::hasExtend($name)) {
return true;
}
return false;
}
/**
* Dynamically calls methods on the class without any arguments
* or creates a new higher order expectation.
* Dynamically calls methods on the class without any arguments or creates a new higher order expectation.
*
* @return Expectation<TValue>|OppositeExpectation<TValue>|Each<TValue>|HigherOrderExpectation<Expectation<TValue>, TValue|null>|TValue
*/
public function __get(string $name)
{
if ($name === 'value') {
return $this->coreExpectation->value;
}
if (!method_exists($this, $name) && !method_exists($this->coreExpectation, $name) && !Expectation::hasExtend($name)) {
if (!self::hasMethod($name)) {
/* @phpstan-ignore-next-line */
return new HigherOrderExpectation($this, $this->retrieve($name, $this->value));
}
@ -331,8 +311,13 @@ final class Expectation
return $this->{$name}();
}
/**
* Checks if the given expectation method exists.
*/
public static function hasMethod(string $name): bool
{
return method_exists(CoreExpectation::class, $name);
return method_exists(self::class, $name)
|| method_exists(BaseExpectation::class, $name)
|| self::hasExtend($name);
}
}

View File

@ -9,7 +9,6 @@ use Pest\PendingCalls\BeforeEachCall;
use Pest\PendingCalls\TestCall;
use Pest\PendingCalls\UsesCall;
use Pest\Support\Backtrace;
use Pest\Support\Extendable;
use Pest\Support\HigherOrderTapProxy;
use Pest\TestSuite;
use PHPUnit\Framework\TestCase;
@ -22,14 +21,10 @@ if (!function_exists('expect')) {
*
* @param TValue $value the Value
*
* @return Expectation<TValue>|Extendable
* @return Expectation<TValue>
*/
function expect($value = null): Expectation|Extendable
function expect($value = null): Expectation
{
if (func_num_args() === 0) {
return new Extendable(Expectation::class);
}
return new Expectation($value);
}
}

View File

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Pest;
use Pest\Concerns\RetrievesValues;
use Pest\Concerns\Retrievable;
/**
* @internal
@ -16,7 +16,7 @@ use Pest\Concerns\RetrievesValues;
*/
final class HigherOrderExpectation
{
use RetrievesValues;
use Retrievable;
/**
* @var Expectation<TValue>|Each<TValue>

View File

@ -11,33 +11,55 @@ use Closure;
*/
final class ExpectationPipeline
{
/** @var array<Closure> */
/**
* The list of pipes.
*
* @var array<int, Closure>
*/
private array $pipes = [];
/** @var array<mixed> */
private array $passable;
/**
* The list of passables.
*
* @var array<int, mixed>
*/
private array $passables;
private Closure $expectationClosure;
/**
* The expectation closure.
*/
private Closure $closure;
public function __construct(Closure $expectationClosure)
/**
* Creates a new instance of Expectation Pipeline.
*/
public function __construct(Closure $closure)
{
$this->expectationClosure = $expectationClosure;
$this->closure = $closure;
}
public static function for(Closure $expectationClosure): self
/**
* Creates a new instance of Expectation Pipeline with given closure.
*/
public static function for(Closure $closure): self
{
return new self($expectationClosure);
return new self($closure);
}
public function send(mixed ...$passable): self
/**
* Sets the list of passables.
*/
public function send(mixed ...$passables): self
{
$this->passable = $passable;
$this->passables = array_values($passables);
return $this;
}
/**
* @param array<Closure> $pipes
* Sets the list of pipes.
*
* @param array<int, Closure> $pipes
*/
public function through(array $pipes): self
{
@ -46,24 +68,30 @@ final class ExpectationPipeline
return $this;
}
/**
* Runs the pipeline.
*/
public function run(): void
{
$pipeline = array_reduce(
array_reverse($this->pipes),
$this->carry(),
function (): void {
($this->expectationClosure)(...$this->passable);
($this->closure)(...$this->passables);
}
);
$pipeline();
}
/**
* Get a Closure that will carry of the expectation.
*/
public function carry(): Closure
{
return function ($stack, $pipe): Closure {
return function () use ($stack, $pipe) {
return $pipe($stack, ...$this->passable);
return $pipe($stack, ...$this->passables);
};
};
}

View File

@ -1,40 +0,0 @@
<?php
declare(strict_types=1);
namespace Pest\Support;
use Closure;
final class Extendable
{
/**
* Creates a new extendable instance.
*/
public function __construct(
private string $extendableClass
) {
// ..
}
/**
* Register a custom extend.
*/
public function extend(string $name, Closure $extend): void
{
$this->extendableClass::extend($name, $extend);
}
public function pipe(string $name, Closure $pipe): void
{
$this->extendableClass::pipe($name, $pipe);
}
/**
* @param string|Closure $filter
*/
public function intercept(string $name, $filter, Closure $handler): void
{
$this->extendableClass::intercept($name, $filter, $handler);
}
}