Merge branch 'master' into skip-closure-support

# Conflicts:
#	src/Support/HigherOrderMessage.php
This commit is contained in:
luke
2021-07-08 17:50:48 +01:00
6 changed files with 116 additions and 8 deletions

View File

@ -7,14 +7,15 @@ namespace Pest\PendingObjects;
use Closure;
use Pest\Factories\TestCaseFactory;
use Pest\Support\Backtrace;
use Pest\Support\HigherOrderCallables;
use Pest\Support\NullClosure;
use Pest\TestSuite;
use SebastianBergmann\Exporter\Exporter;
/**
* @method \Pest\Expectations\Expectation expect(mixed $value)
*
* @internal
*
* @mixin HigherOrderCallables
*/
final class TestCall
{

View File

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace Pest\Support;
use Pest\Expectation;
/**
* @internal
*/
final class HigherOrderCallables
{
/**
* @var object
*/
private $target;
public function __construct(object $target)
{
$this->target = $target;
}
/**
* @template TValue
*
* Create a new expectation. Callable values will be executed prior to returning the new expectation.
*
* @param callable|TValue $value
*
* @return Expectation<TValue>
*/
public function expect($value)
{
return new Expectation(is_callable($value) ? Reflection::bindCallable($value) : $value);
}
/**
* @template TValue
*
* Create a new expectation. Callable values will be executed prior to returning the new expectation.
*
* @param callable|TValue $value
*
* @return Expectation<TValue>
*/
public function and($value)
{
return $this->expect($value);
}
/**
* @template TValue
*
* @param callable(): TValue $callable
*
* @return TValue|object
*/
public function tap(callable $callable)
{
Reflection::bindCallable($callable);
return $this->target;
}
}

View File

@ -5,7 +5,6 @@ declare(strict_types=1);
namespace Pest\Support;
use Closure;
use const PHP_MAJOR_VERSION;
use ReflectionClass;
use Throwable;
@ -84,6 +83,11 @@ final class HigherOrderMessage
return $target;
}
if ($this->hasHigherOrderCallable()) {
/* @phpstan-ignore-next-line */
return (new HigherOrderCallables($target))->{$this->methodName}(...$this->arguments);
}
try {
return Reflection::call($target, $this->methodName, $this->arguments);
} catch (Throwable $throwable) {
@ -114,9 +118,19 @@ final class HigherOrderMessage
return $this;
}
/**
* Determines whether or not there exists a higher order callable with the message name.
*
* @return bool
*/
private function hasHigherOrderCallable()
{
return in_array($this->methodName, get_class_methods(HigherOrderCallables::class), true);
}
private static function getUndefinedMethodMessage(object $target, string $methodName): string
{
if (PHP_MAJOR_VERSION >= 8) {
if (\PHP_MAJOR_VERSION >= 8) {
return sprintf(sprintf(self::UNDEFINED_METHOD, sprintf('%s::%s()', get_class($target), $methodName)));
}

View File

@ -41,15 +41,25 @@ final class Reflection
}
if (is_callable($method)) {
return Closure::fromCallable($method)->bindTo(
TestSuite::getInstance()->test
)(...$args);
return static::bindCallable($method, $args);
}
throw $exception;
}
}
/**
* Bind a callable to the TestCase and return the result.
*
* @param array<int, mixed> $args
*
* @return mixed
*/
public static function bindCallable(callable $callable, array $args = [])
{
return Closure::fromCallable($callable)->bindTo(TestSuite::getInstance()->test)(...$args);
}
/**
* Infers the file name from the given closure.
*/