From 241d4cf94c33f23ab4160cb188dcbd1e2121de4c Mon Sep 17 00:00:00 2001 From: luke Date: Wed, 16 Jun 2021 20:34:17 +0100 Subject: [PATCH 1/4] Reimplements non-callable sequence values. --- src/Expectation.php | 13 ++++++++++--- tests/.snapshots/success.txt | 4 +++- tests/Features/Expect/sequence.php | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/Expectation.php b/src/Expectation.php index e35acc5f..f14491c3 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -110,7 +110,7 @@ final class Expectation if (is_callable($callback)) { foreach ($this->value as $item) { - $callback(expect($item)); + $callback(new Expectation($item)); } } @@ -119,8 +119,10 @@ final class Expectation /** * Allows you to specify a sequential set of expectations for each item in a iterable "value". + * + * @param mixed ...$callbacks */ - public function sequence(callable ...$callbacks): Expectation + public function sequence(...$callbacks): Expectation { if (!is_iterable($this->value)) { throw new BadMethodCallException('Expectation value is not iterable.'); @@ -138,7 +140,12 @@ final class Expectation } foreach ($values as $key => $item) { - call_user_func($callbacks[$key], expect($item), expect($keys[$key])); + if (is_callable($callbacks[$key])) { + call_user_func($callbacks[$key], new Expectation($item), new Expectation($keys[$key])); + continue; + } + + (new Expectation($item))->toEqual($callbacks[$key]); } return $this; diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 9f57d4fa..2929bb74 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -152,6 +152,8 @@ ✓ loops back to the start if it runs out of sequence items ✓ it works if the number of items in the iterable is smaller than the number of expectations ✓ it works with associative arrays + ✓ it can be passed non-callable values + ✓ it can be passed a mixture of value types PASS Tests\Features\Expect\toBe ✓ strict comparisons @@ -554,5 +556,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 7 skipped, 340 passed + Tests: 4 incompleted, 7 skipped, 342 passed \ No newline at end of file diff --git a/tests/Features/Expect/sequence.php b/tests/Features/Expect/sequence.php index fe6cb635..b4f82cb5 100644 --- a/tests/Features/Expect/sequence.php +++ b/tests/Features/Expect/sequence.php @@ -44,3 +44,19 @@ test('it works with associative arrays', function () { function ($expectation, $key) { $expectation->toEqual('boom'); $key->toEqual('baz'); }, ); }); + +test('it can be passed non-callable values', function () { + expect(['foo', 'bar', 'baz'])->sequence('foo', 'bar', 'baz'); + + expect(static::getCount())->toBe(3); +}); + +test('it can be passed a mixture of value types', function () { + expect(['foo', 'bar', 'baz'])->sequence( + 'foo', + function ($expectation) { $expectation->toEqual('bar')->toBeString(); }, + 'baz' + ); + + expect(static::getCount())->toBe(4); +}); From 579bb1b90caa3cc4490f8cf4175016995911023e Mon Sep 17 00:00:00 2001 From: luke Date: Thu, 17 Jun 2021 00:15:42 +0100 Subject: [PATCH 2/4] Updates DocBlock type hinting --- src/Expectation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Expectation.php b/src/Expectation.php index f14491c3..424f4b60 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -120,7 +120,7 @@ final class Expectation /** * Allows you to specify a sequential set of expectations for each item in a iterable "value". * - * @param mixed ...$callbacks + * @param callable(Expectation, Expectation): void|mixed ...$callbacks */ public function sequence(...$callbacks): Expectation { From 8a384a6d657e466f3c17d672197917fff2a14197 Mon Sep 17 00:00:00 2001 From: luke Date: Fri, 18 Jun 2021 21:46:57 +0100 Subject: [PATCH 3/4] Merge fixes --- src/Expectation.php | 3 ++- tests/.snapshots/success.txt | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Expectation.php b/src/Expectation.php index 57a7758a..53f17a3c 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -128,9 +128,10 @@ final class Expectation } /** + * @template TValue * Allows you to specify a sequential set of expectations for each item in a iterable "value". * - * @param callable(Expectation, Expectation): void|mixed ...$callbacks + * @param callable(Expectation, Expectation): void|TValue ...$callbacks */ public function sequence(...$callbacks): Expectation { diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 422bfc2f..3dd36cae 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -156,6 +156,8 @@ ✓ loops back to the start if it runs out of sequence items ✓ it works if the number of items in the iterable is smaller than the number of expectations ✓ it works with associative arrays + ✓ it can be passed non-callable values + ✓ it can be passed a mixture of value types PASS Tests\Features\Expect\toBe ✓ strict comparisons @@ -573,5 +575,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 7 skipped, 357 passed + Tests: 4 incompleted, 7 skipped, 359 passed \ No newline at end of file From aeded0a356a520631b2041d779306e883b8ec763 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 18 Jun 2021 22:01:14 +0100 Subject: [PATCH 4/4] refacto: coding style updates --- src/Expectation.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Expectation.php b/src/Expectation.php index 53f17a3c..e619a6ae 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -120,7 +120,7 @@ final class Expectation if (is_callable($callback)) { foreach ($this->value as $item) { - $callback(new Expectation($item)); + $callback(new self($item)); } } @@ -128,10 +128,11 @@ final class Expectation } /** - * @template TValue * Allows you to specify a sequential set of expectations for each item in a iterable "value". * - * @param callable(Expectation, Expectation): void|TValue ...$callbacks + * @template TValue + * + * @param callable(self, self): void|TValue ...$callbacks */ public function sequence(...$callbacks): Expectation { @@ -152,11 +153,11 @@ final class Expectation foreach ($values as $key => $item) { if (is_callable($callbacks[$key])) { - call_user_func($callbacks[$key], new Expectation($item), new Expectation($keys[$key])); + call_user_func($callbacks[$key], new self($item), new self($keys[$key])); continue; } - (new Expectation($item))->toEqual($callbacks[$key]); + (new self($item))->toEqual($callbacks[$key]); } return $this;