diff --git a/src/PendingObjects/TestCall.php b/src/PendingObjects/TestCall.php index 69227aa6..40574ed1 100644 --- a/src/PendingObjects/TestCall.php +++ b/src/PendingObjects/TestCall.php @@ -7,15 +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; -/* - * @mixin \Pest\Support\HigherOrderCallables - * @method \Pest\Expectations\Expectation expect(mixed $value) - * +/** * @internal + * + * @mixin HigherOrderCallables */ final class TestCall { diff --git a/src/Plugins/Coverage.php b/src/Plugins/Coverage.php index cc03bd17..6d993717 100644 --- a/src/Plugins/Coverage.php +++ b/src/Plugins/Coverage.php @@ -81,7 +81,6 @@ final class Coverage implements AddsOutput, HandlesArguments } if ($input->getOption(self::MIN_OPTION) !== null) { - /* @phpstan-ignore-next-line */ $this->coverageMin = (float) $input->getOption(self::MIN_OPTION); } diff --git a/src/Support/HigherOrderCallables.php b/src/Support/HigherOrderCallables.php index 08231c76..46503e67 100644 --- a/src/Support/HigherOrderCallables.php +++ b/src/Support/HigherOrderCallables.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Pest\Support; +use Pest\Expectation; + /** * @internal */ @@ -19,6 +21,34 @@ final class HigherOrderCallables $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 + */ + 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 + */ + public function and($value) + { + return $this->expect($value); + } + /** * @template TValue * @@ -28,6 +58,8 @@ final class HigherOrderCallables */ public function tap(callable $callable) { - return Reflection::bindCallable($callable) ?? $this->target; + Reflection::bindCallable($callable); + + return $this->target; } } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 96cd47ea..d943da92 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -410,8 +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 - ✓ it can use the returned instance from a tap WARN Tests\Features\Incompleted … incompleted diff --git a/tests/Features/HigherOrderTests.php b/tests/Features/HigherOrderTests.php index ea5b80e1..f871d919 100644 --- a/tests/Features/HigherOrderTests.php +++ b/tests/Features/HigherOrderTests.php @@ -10,14 +10,18 @@ it('is capable doing multiple assertions') ->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()->toBe('foo') + ->expect('foo')->toBeString() ->tap(function () { expect($this)->toBeInstanceOf(TestCase::class); }) + ->toBe('foo') ->and('hello world')->toBeString(); -it('can use the returned instance from a tap') - ->expect('foo')->toBeString()->toBe('foo') - ->tap(function () { return expect($this); }) - ->toBeInstanceOf(TestCase::class); - afterEach()->assertTrue(true);