implemented pipe closure with $next as the last parameter

This commit is contained in:
Fabio Ivona
2021-10-10 00:16:21 +02:00
parent c3a445534b
commit bc4e5b9b4e
5 changed files with 202 additions and 130 deletions

View File

@ -0,0 +1,18 @@
<?php
namespace Pest\Exceptions;
use Exception;
class PipeException extends Exception
{
public static function optionalParmetersShouldBecomeRequired(string $expectationName): PipeException
{
return new self("You're attempting to pipe '$expectationName', but in pipelines optional parmeters should be declared as required)");
}
public static function expectationNotFound($expectationName): PipeException
{
return new self("Expectation $expectationName was not found in Pest");
}
}

View File

@ -5,9 +5,11 @@ declare(strict_types=1);
namespace Pest;
use BadMethodCallException;
use Closure;
use Pest\Concerns\Extendable;
use Pest\Concerns\RetrievesValues;
use Pest\Support\Pipeline;
use Pest\Exceptions\PipeException;
use Pest\Support\ExpectationPipeline;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\ExpectationFailedException;
@ -17,7 +19,7 @@ use PHPUnit\Framework\ExpectationFailedException;
* @template TValue
*
* @property Expectation $not Creates the opposite expectation.
* @property Each $each Creates an expectation on each element on the traversable value.
* @property Each $each Creates an expectation on each element on the traversable value.
*
* @mixin CoreExpectation
*/
@ -135,16 +137,16 @@ final class Expectation
throw new BadMethodCallException('Expectation value is not iterable.');
}
$value = is_array($this->value) ? $this->value : iterator_to_array($this->value);
$keys = array_keys($value);
$values = array_values($value);
$value = is_array($this->value) ? $this->value : iterator_to_array($this->value);
$keys = array_keys($value);
$values = array_values($value);
$callbacksCount = count($callbacks);
$index = 0;
while (count($callbacks) < count($values)) {
$callbacks[] = $callbacks[$index];
$index = $index < count($values) - 1 ? $index + 1 : 0;
$index = $index < count($values) - 1 ? $index + 1 : 0;
}
if ($callbacksCount > count($values)) {
@ -168,7 +170,7 @@ final class Expectation
*
* @template TMatchSubject of array-key
*
* @param callable(): TMatchSubject|TMatchSubject $subject
* @param callable(): TMatchSubject|TMatchSubject $subject
* @param array<TMatchSubject, (callable(Expectation<TValue>): mixed)|TValue> $expressions
*/
public function match($subject, array $expressions): Expectation
@ -260,28 +262,23 @@ final class Expectation
return new HigherOrderExpectation($this, $this->value->$method(...$parameters));
}
Pipeline::send(...$parameters)
ExpectationPipeline::for($method, $this->getExpectationClosure($method))
->send(...$parameters)
->through($this->pipes($method, $this, Expectation::class))
->finally(function ($parameters) use ($method): void {
$this->callExpectation($method, $parameters);
});
->run();
return $this;
}
/**
* @param array<mixed> $parameters
*/
private function callExpectation(string $name, array $parameters): void
private function getExpectationClosure(string $name): Closure
{
if (method_exists($this->coreExpectation, $name)) {
//@phpstan-ignore-next-line
$this->coreExpectation->{$name}(...$parameters);
} else {
if (self::hasExtend($name)) {
$this->__extendsCall($name, $parameters);
}
return Closure::fromCallable([$this->coreExpectation, $name]);
} elseif (self::hasExtend($name)) {
return self::$extends[$name];
}
throw PipeException::expectationNotFound($name);
}
private function hasExpectation(string $name): bool

View File

@ -0,0 +1,114 @@
<?php
declare(strict_types=1);
namespace Pest\Support;
use Closure;
use Pest\Exceptions\PipeException;
use ReflectionFunction;
final class ExpectationPipeline
{
/** @var array<Closure> */
private $pipes = [];
/** @var array<mixed> */
private $passable;
/** @var Closure */
private $expectationClosure;
/** @var string */
private $expectationName;
public function __construct(string $expectationName, Closure $expectationClosure)
{
$this->expectationClosure = $expectationClosure;
$this->expectationName = $expectationName;
}
public static function for(string $expectationName, Closure $expectationClosure): self
{
return new self($expectationName, $expectationClosure);
}
/**
* @param array<mixed> $passable
*/
public function send(...$passable): self
{
$this->passable = $passable;
return $this;
}
/**
* @param array<Closure> $pipes
*/
public function through(array $pipes): self
{
$this->pipes = $pipes;
return $this;
}
public function run(): void
{
$pipeline = array_reduce(
array_reverse($this->pipes),
$this->carry(),
$this->expectationClosure
);
$pipeline(...$this->passable);
}
public function carry(): Closure
{
return function ($stack, $pipe): Closure {
return function (...$passable) use ($stack, $pipe) {
$this->checkOptionalParametersBecomeRequired($pipe);
$passable = $this->preparePassable($passable);
$passable[] = $stack;
return $pipe(...$passable);
};
};
}
private function preparePassable(array $passable): array
{
$reflection = new ReflectionFunction($this->expectationClosure);
$requiredParametersCount = $reflection->getNumberOfParameters();
if (count($passable) < $requiredParametersCount) {
foreach ($reflection->getParameters() as $index => $parameter) {
if (!isset($passable[$index])) {
$passable[$index] = $parameter->getDefaultValue();
}
}
}
return $passable;
}
private function checkOptionalParametersBecomeRequired($pipe)
{
$reflection = new ReflectionFunction($pipe);
foreach ($reflection->getParameters() as $parameter) {
if ($parameter->isDefaultValueAvailable()) {
/*
* TODO add pipeline blame in the exception message and a stronger clarification like
* “Youre attempting to pipe toBe, but havent added the $actual parameter to your pipe handler”
*/
throw PipeException::optionalParmetersShouldBecomeRequired($this->expectationName);
}
}
}
}

View File

@ -1,71 +0,0 @@
<?php
declare(strict_types=1);
namespace Pest\Support;
use Closure;
final class Pipeline
{
/** @var array<Closure> */
private $pipes = [];
/** @var array<mixed> */
private $passable;
/**
* @param array<mixed> $passable
*/
public function __construct(...$passable)
{
$this->passable = $passable;
}
/**
* @param array<mixed> $passable
*/
public static function send(...$passable): self
{
return new self(...$passable);
}
/**
* @param array<Closure> $pipes
*/
public function through(array $pipes): self
{
$this->pipes = $pipes;
return $this;
}
public function finally(Closure $finalClosure): void
{
$pipeline = array_reduce(
array_reverse($this->pipes),
$this->carry(),
$this->prepareFinalClosure($finalClosure)
);
$pipeline(...$this->passable);
}
public function carry(): Closure
{
return function ($stack, $pipe): Closure {
return function (...$passable) use ($stack, $pipe) {
$passable[] = $stack;
return $pipe(...$passable);
};
};
}
private function prepareFinalClosure(Closure $finalClosure): Closure
{
return function (...$passable) use ($finalClosure) {
return $finalClosure($passable);
};
}
}