Skip to content

Commit

Permalink
Merge branch 'master' into skip-closure-support
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Support/HigherOrderMessage.php
  • Loading branch information
lukeraymonddowning committed Jul 8, 2021
2 parents fa3959d + d838456 commit 5049b99
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/PendingObjects/TestCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
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
{
Expand Down
65 changes: 65 additions & 0 deletions src/Support/HigherOrderCallables.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
18 changes: 16 additions & 2 deletions src/Support/HigherOrderMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Pest\Support;

use Closure;
use const PHP_MAJOR_VERSION;
use ReflectionClass;
use Throwable;

Expand Down Expand Up @@ -84,6 +83,11 @@ public function call(object $target)
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) {
Expand Down Expand Up @@ -114,9 +118,19 @@ public function when(callable $condition): self
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)));
}

Expand Down
16 changes: 13 additions & 3 deletions src/Support/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,25 @@ public static function call(object $object, string $method, array $args = [])
}

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.
*/
Expand Down
4 changes: 3 additions & 1 deletion tests/.snapshots/success.txt
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@
PASS Tests\Features\HigherOrderTests
✓ it proxies calls to object
✓ it is capable doing multiple assertions
✓ it resolves expect callables correctly
✓ it can tap into the test

WARN Tests\Features\Incompleted
… incompleted
Expand Down Expand Up @@ -579,5 +581,5 @@
✓ it is a test
✓ it uses correct parent class

Tests: 4 incompleted, 7 skipped, 363 passed
Tests: 4 incompleted, 7 skipped, 365 passed

16 changes: 16 additions & 0 deletions tests/Features/HigherOrderTests.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use PHPUnit\Framework\TestCase;

beforeEach()->assertTrue(true);

it('proxies calls to object')->assertTrue(true);
Expand All @@ -8,4 +10,18 @@
->assertTrue(true)
->assertFalse(false);

it('resolves expect callables correctly')
->expect(function () { return 'foo'; })
->toBeString()
->toBe('foo')
->and('bar')
->toBeString()
->toBe('bar');

it('can tap into the test')
->expect('foo')->toBeString()
->tap(function () { expect($this)->toBeInstanceOf(TestCase::class); })
->toBe('foo')
->and('hello world')->toBeString();

afterEach()->assertTrue(true);

0 comments on commit 5049b99

Please sign in to comment.