diff --git a/src/CoreExpectation.php b/src/BaseExpectation.php similarity index 77% rename from src/CoreExpectation.php rename to src/BaseExpectation.php index 153a935d..b193c397 100644 --- a/src/CoreExpectation.php +++ b/src/BaseExpectation.php @@ -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 */ -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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 - * * @param non-empty-string $expected + * + *@return BaseExpectation */ - 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 - * * @param non-empty-string $expected + * + *@return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 - * * @param iterable $names + * + *@return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 $values * - * @return CoreExpectation + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 $keys * - * @return CoreExpectation + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 $array * - * @return CoreExpectation + * @return BaseExpectation */ - 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|object $object * - * @return CoreExpectation + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - 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 + * @return BaseExpectation */ - public function toThrow(callable|string $exception, string $exceptionMessage = null): CoreExpectation + public function toThrow(callable|string $exception, string $exceptionMessage = null): BaseExpectation { $callback = NullClosure::create(); diff --git a/src/Concerns/Extendable.php b/src/Concerns/Extendable.php index 6f83693a..a2f7e40b 100644 --- a/src/Concerns/Extendable.php +++ b/src/Concerns/Extendable.php @@ -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> */ - 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 - */ - 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 $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); - } } diff --git a/src/Concerns/Pipeable.php b/src/Concerns/Pipeable.php new file mode 100644 index 00000000..79f32055 --- /dev/null +++ b/src/Concerns/Pipeable.php @@ -0,0 +1,65 @@ +> + */ + 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 + */ + private function pipes(string $name, object $context, string $scope): array + { + return array_map(fn (Closure $pipe) => $pipe->bindTo($context, $scope), self::$pipes[$name] ?? []); + } +} diff --git a/src/Concerns/RetrievesValues.php b/src/Concerns/Retrievable.php similarity index 96% rename from src/Concerns/RetrievesValues.php rename to src/Concerns/Retrievable.php index c7c6cdd9..81dd75a3 100644 --- a/src/Concerns/RetrievesValues.php +++ b/src/Concerns/Retrievable.php @@ -7,7 +7,7 @@ namespace Pest\Concerns; /** * @internal */ -trait RetrievesValues +trait Retrievable { /** * @template TRetrievableValue diff --git a/src/Expectation.php b/src/Expectation.php index 86e1f121..818a1606 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -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 + * @mixin BaseExpectation */ final class Expectation { - use RetrievesValues, Extendable { - __call as __extendsCall; - } - - /** @var CoreExpectation */ - 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 $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|OppositeExpectation|Each|HigherOrderExpectation, 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); } } diff --git a/src/Functions.php b/src/Functions.php index 80e0d9e0..00eed63d 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -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|Extendable + * @return Expectation */ - 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); } } diff --git a/src/HigherOrderExpectation.php b/src/HigherOrderExpectation.php index 8c4dcc4e..6480f0d7 100644 --- a/src/HigherOrderExpectation.php +++ b/src/HigherOrderExpectation.php @@ -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|Each diff --git a/src/Support/ExpectationPipeline.php b/src/Support/ExpectationPipeline.php index 0ddc9260..11481576 100644 --- a/src/Support/ExpectationPipeline.php +++ b/src/Support/ExpectationPipeline.php @@ -11,33 +11,55 @@ use Closure; */ final class ExpectationPipeline { - /** @var array */ + /** + * The list of pipes. + * + * @var array + */ private array $pipes = []; - /** @var array */ - private array $passable; + /** + * The list of passables. + * + * @var array + */ + 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 $pipes + * Sets the list of pipes. + * + * @param array $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); }; }; } diff --git a/src/Support/Extendable.php b/src/Support/Extendable.php deleted file mode 100644 index fba8e17b..00000000 --- a/src/Support/Extendable.php +++ /dev/null @@ -1,40 +0,0 @@ -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); - } -}