From 371620d16147e1e562a1c65e2b1f2912d06a02c1 Mon Sep 17 00:00:00 2001 From: luke Date: Wed, 21 Jul 2021 07:40:19 +0100 Subject: [PATCH 1/5] Adds support for receiving datasets in higher order tests --- src/Support/HigherOrderCallables.php | 4 ++-- src/Support/Reflection.php | 15 +++++++++++++++ tests/Features/HigherOrderTests.php | 9 +++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) 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); From 3bdba9210d19575bec6613f3af430ba72fbd0e3a Mon Sep 17 00:00:00 2001 From: luke Date: Wed, 21 Jul 2021 07:44:05 +0100 Subject: [PATCH 2/5] Adds another test --- tests/Features/HigherOrderTests.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/Features/HigherOrderTests.php b/tests/Features/HigherOrderTests.php index ba716f17..e2ff0686 100644 --- a/tests/Features/HigherOrderTests.php +++ b/tests/Features/HigherOrderTests.php @@ -36,4 +36,11 @@ it('can pass datasets into the tap callable') ->with([[1, 2, 3]]) ->tap(function (...$numbers) { expect($numbers)->toBe([1, 2, 3]); }); +it('can pass shared datasets into callables') + ->with('numbers.closure.wrapped') + ->expect(function ($value) { return $value; }) + ->and(function ($value) { return $value; }) + ->tap(function ($value) { expect($value)->toBeInt(); }) + ->toBeInt(); + afterEach()->assertTrue(true); From 82c18d38486fa16042efd9f2f7b2d77d07d0e2db Mon Sep 17 00:00:00 2001 From: luke Date: Wed, 21 Jul 2021 07:58:05 +0100 Subject: [PATCH 3/5] Type fixes --- src/Support/Reflection.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Support/Reflection.php b/src/Support/Reflection.php index e06b0280..819ad4a5 100644 --- a/src/Support/Reflection.php +++ b/src/Support/Reflection.php @@ -109,10 +109,6 @@ final class Reflection } } - if ($reflectionProperty === null) { - throw ShouldNotHappen::fromMessage('Reflection property not found.'); - } - $reflectionProperty->setAccessible(true); return $reflectionProperty->getValue($object); @@ -143,10 +139,6 @@ final class Reflection } } - if ($reflectionProperty === null) { - throw ShouldNotHappen::fromMessage('Reflection property not found.'); - } - $reflectionProperty->setAccessible(true); $reflectionProperty->setValue($object, $value); } From 09682dd393225c9d1cb29b9439ec500531c09dfc Mon Sep 17 00:00:00 2001 From: luke Date: Wed, 21 Jul 2021 08:16:54 +0100 Subject: [PATCH 4/5] Alters test to prove order doesn't matter --- tests/Features/HigherOrderTests.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Features/HigherOrderTests.php b/tests/Features/HigherOrderTests.php index e2ff0686..5505c8a0 100644 --- a/tests/Features/HigherOrderTests.php +++ b/tests/Features/HigherOrderTests.php @@ -37,10 +37,10 @@ it('can pass datasets into the tap callable') ->tap(function (...$numbers) { expect($numbers)->toBe([1, 2, 3]); }); it('can pass shared datasets into callables') - ->with('numbers.closure.wrapped') ->expect(function ($value) { return $value; }) ->and(function ($value) { return $value; }) ->tap(function ($value) { expect($value)->toBeInt(); }) - ->toBeInt(); + ->toBeInt() + ->with('numbers.closure.wrapped'); afterEach()->assertTrue(true); From 4c8c42cd203dd14de90b4a5d3169d2a5147ec0c3 Mon Sep 17 00:00:00 2001 From: luke Date: Wed, 21 Jul 2021 08:47:55 +0100 Subject: [PATCH 5/5] Refactor --- tests/Features/HigherOrderTests.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Features/HigherOrderTests.php b/tests/Features/HigherOrderTests.php index 5505c8a0..e2ff0686 100644 --- a/tests/Features/HigherOrderTests.php +++ b/tests/Features/HigherOrderTests.php @@ -37,10 +37,10 @@ it('can pass datasets into the tap callable') ->tap(function (...$numbers) { expect($numbers)->toBe([1, 2, 3]); }); it('can pass shared datasets into callables') + ->with('numbers.closure.wrapped') ->expect(function ($value) { return $value; }) ->and(function ($value) { return $value; }) ->tap(function ($value) { expect($value)->toBeInt(); }) - ->toBeInt() - ->with('numbers.closure.wrapped'); + ->toBeInt(); afterEach()->assertTrue(true);