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;
@ -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);
};
}
}

View File

@ -1,6 +1,7 @@
<?php
use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertEqualsIgnoringCase;
use function PHPUnit\Framework\assertInstanceOf;
use function PHPUnit\Framework\assertIsNumeric;
@ -65,7 +66,7 @@ class State
$state = new State();
/*
* Asserts two Characters are the same
* Overrides toBe to assert two Characters are the same
*/
expect()->pipe('toBe', function ($expected, $next) use ($state) {
$state->runCount['character']++;
@ -82,7 +83,7 @@ expect()->pipe('toBe', function ($expected, $next) use ($state) {
});
/*
* Asserts two Numbers are the same
* Overrides toBe to assert two Numbers are the same
*/
expect()->intercept('toBe', Number::class, function ($expected) use ($state) {
$state->runCount['number']++;
@ -91,7 +92,7 @@ expect()->intercept('toBe', Number::class, function ($expected) use ($state) {
});
/*
* Asserts all integers are allowed if value is an '*'
* Overrides toBe to assert all integers are allowed if value is an '*'
*/
expect()->intercept('toBe', function ($value) {
return $value === '*';
@ -102,7 +103,7 @@ expect()->intercept('toBe', function ($value) {
});
/*
* Asserts two Symbols are the same
* Overrides toBe to assert two Symbols are the same
*/
expect()->pipe('toBe', function ($expected, $next) use ($state) {
$state->runCount['symbol']++;
@ -118,6 +119,13 @@ expect()->pipe('toBe', function ($expected, $next) use ($state) {
$next($expected);
});
expect()->intercept('toHaveProperty', function ($value) {
return $value instanceof Symbol && $value->value == '£';
}, function (string $propertyName, $propertyValue = null) {
assertEquals("£", $this->value->value);
});
test('pipe is applied and can stop pipeline', function () use ($state) {
$letter = new Character('A');
@ -137,7 +145,8 @@ test('pipe is applied and can stop pipeline', function () use ($state) {
'wildcard' => 0,
'symbol' => 0,
]);
});
})
;
test('pipe is run and can let the pipeline keep going', function () use ($state) {
$state->reset();
@ -207,3 +216,8 @@ test('intercept can be filtered with a closure', function () use ($state) {
->runCount->toHaveKey('wildcard', 1)
->appliedCount->toHaveKey('wildcard', 1);
});
test('intercept can handle default values', function(){
expect(new Symbol("£"))->toHaveProperty('value');
expect(new Symbol("£"))->toHaveProperty('value', '£');
})->only();