From 12e63c7376659dc57ca33ad03b23a420b6e29499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20A=C5=9Fan?= Date: Thu, 16 Sep 2021 18:09:45 +0300 Subject: [PATCH 1/3] add new assertion --- src/Expectation.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Expectation.php b/src/Expectation.php index 253b1e1e..9ec55142 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -154,9 +154,10 @@ final class Expectation throw new BadMethodCallException('Expectation value is not iterable.'); } - $value = is_array($this->value) ? $this->value : iterator_to_array($this->value); - $keys = array_keys($value); - $values = array_values($value); + $value = is_array($this->value) ? $this->value : iterator_to_array($this->value); + $keys = array_keys($value); + $values = array_values($value); + $callbacksCount = count($callbacks); $index = 0; @@ -165,6 +166,10 @@ final class Expectation $index = $index < count($values) - 1 ? $index + 1 : 0; } + if ($callbacksCount > count($values)) { + Assert::assertLessThanOrEqual(count($value), count($callbacks)); + } + foreach ($values as $key => $item) { if (is_callable($callbacks[$key])) { call_user_func($callbacks[$key], new self($item), new self($keys[$key])); From 3dd10b3c7cc6ef24b8f6cc12db36031f5ab5cb57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20A=C5=9Fan?= Date: Thu, 16 Sep 2021 18:09:59 +0300 Subject: [PATCH 2/3] change test --- tests/Features/Expect/sequence.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Features/Expect/sequence.php b/tests/Features/Expect/sequence.php index b4f82cb5..4f11b3a3 100644 --- a/tests/Features/Expect/sequence.php +++ b/tests/Features/Expect/sequence.php @@ -1,5 +1,7 @@ each->sequence(); })->throws(BadMethodCallException::class, 'Expectation value is not iterable.'); @@ -26,16 +28,14 @@ test('loops back to the start if it runs out of sequence items', function () { expect(static::getCount())->toBe(16); }); -test('it works if the number of items in the iterable is smaller than the number of expectations', function () { +test('fails if the number of iterable items is greater than the number of expectations', function () { expect([1, 2]) ->sequence( function ($expectation) { $expectation->toBeInt()->toEqual(1); }, function ($expectation) { $expectation->toBeInt()->toEqual(2); }, function ($expectation) { $expectation->toBeInt()->toEqual(3); }, ); - - expect(static::getCount())->toBe(4); -}); +})->throws(ExpectationFailedException::class); test('it works with associative arrays', function () { expect(['foo' => 'bar', 'baz' => 'boom']) From a16a19e1214f4e9e817b775ab395dcb52e45c42b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20A=C5=9Fan?= Date: Thu, 16 Sep 2021 18:10:15 +0300 Subject: [PATCH 3/3] update snapshots --- tests/.snapshots/success.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 4442bbb9..92cfa162 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -168,7 +168,7 @@ ✓ an exception is thrown if the the type is not iterable ✓ allows for sequences of checks to be run on iterable data ✓ 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 + ✓ fails if the number of iterable items is greater 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