Adds support for receiving datasets in higher order tests

This commit is contained in:
luke
2021-07-21 07:40:19 +01:00
parent 47ceb2419b
commit 371620d161
3 changed files with 26 additions and 2 deletions

View File

@ -35,7 +35,7 @@ final class HigherOrderCallables
*/ */
public function expect($value) 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) public function tap(callable $callable)
{ {
Reflection::bindCallable($callable); Reflection::bindCallableWithData($callable);
return $this->target; return $this->target;
} }

View File

@ -60,6 +60,21 @@ final class Reflection
return Closure::fromCallable($callable)->bindTo(TestSuite::getInstance()->test)(...$args); 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. * Infers the file name from the given closure.
*/ */

View File

@ -27,4 +27,13 @@ it('can tap into the test')
->toBe('foo') ->toBe('foo')
->and('hello world')->toBeString(); ->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); afterEach()->assertTrue(true);