diff --git a/src/Support/HigherOrderCallables.php b/src/Support/HigherOrderCallables.php index 8b4dc817..b3bb633e 100644 --- a/src/Support/HigherOrderCallables.php +++ b/src/Support/HigherOrderCallables.php @@ -35,7 +35,7 @@ final class HigherOrderCallables */ public function expect($value) { - return new Expectation($value instanceof Closure ? Reflection::bindCallable($value) : $value); + return new Expectation($value instanceof Closure ? Reflection::bindCallableWithData($value) : $value); } /** @@ -59,7 +59,7 @@ final class HigherOrderCallables */ public function tap(callable $callable) { - Reflection::bindCallable($callable); + Reflection::bindCallableWithData($callable); return $this->target; } diff --git a/src/Support/Reflection.php b/src/Support/Reflection.php index fbe7ba40..e06b0280 100644 --- a/src/Support/Reflection.php +++ b/src/Support/Reflection.php @@ -60,6 +60,21 @@ final class Reflection return Closure::fromCallable($callable)->bindTo(TestSuite::getInstance()->test)(...$args); } + /** + * Bind a callable to the TestCase and return the result, + * passing in the current dataset values as arguments. + * + * @return mixed + */ + public static function bindCallableWithData(callable $callable) + { + $test = TestSuite::getInstance()->test; + + return $test === null + ? static::bindCallable($callable) + : Closure::fromCallable($callable)->bindTo($test)(...$test->getProvidedData()); + } + /** * Infers the file name from the given closure. */ diff --git a/tests/Features/HigherOrderTests.php b/tests/Features/HigherOrderTests.php index b45568e9..ba716f17 100644 --- a/tests/Features/HigherOrderTests.php +++ b/tests/Features/HigherOrderTests.php @@ -27,4 +27,13 @@ it('can tap into the test') ->toBe('foo') ->and('hello world')->toBeString(); +it('can pass datasets into the expect callables') + ->with([[1, 2, 3]]) + ->expect(function (...$numbers) { return $numbers; })->toBe([1, 2, 3]) + ->and(function (...$numbers) { return $numbers; })->toBe([1, 2, 3]); + +it('can pass datasets into the tap callable') + ->with([[1, 2, 3]]) + ->tap(function (...$numbers) { expect($numbers)->toBe([1, 2, 3]); }); + afterEach()->assertTrue(true);