diff --git a/src/Each.php b/src/Each.php index e9722d2..9955f66 100644 --- a/src/Each.php +++ b/src/Each.php @@ -4,8 +4,6 @@ namespace Pest\Expectations; -use BadMethodCallException; - /** * @internal * @@ -31,35 +29,6 @@ public function __construct(Expectation $original) $this->original = $original; } - /** - * Allows you to specify a sequential set of - * expectations for each item in a - * traversable "value". - * - * @param callable ...$expectations - */ - public function sequence(...$expectations): Each - { - if (!is_iterable($this->original->value)) { - throw new BadMethodCallException('Expectation value is not traversable.'); - } - - $expectationIndex = 0; - - /* @phpstan-ignore-next-line */ - while (count($expectations) < count($this->original->value)) { - $expectations[] = $expectations[$expectationIndex]; - /* @phpstan-ignore-next-line */ - $expectationIndex = $expectationIndex < count($this->original->value) - 1 ? $expectationIndex + 1 : 0; - } - - foreach ($this->original->value as $index => $item) { - call_user_func($expectations[$index], expect($item)); - } - - return $this; - } - /** * Creates a new expectation. * diff --git a/src/Expectation.php b/src/Expectation.php index 404d31a..f38ba65 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -72,7 +72,7 @@ public function not(): OppositeExpectation public function each(callable $callback = null): Each { if (!is_iterable($this->value)) { - throw new BadMethodCallException('Expectation value is not traversable.'); + throw new BadMethodCallException('Expectation value is not iterable.'); } if (is_callable($callback)) { @@ -84,6 +84,32 @@ public function each(callable $callback = null): Each return new Each($this); } + /** + * Allows you to specify a sequential set of expectations for each item in a iterable "value". + */ + public function sequence(callable ...$callbacks): Expectation + { + if (!is_iterable($this->value)) { + throw new BadMethodCallException('Expectation value is not iterable.'); + } + + $index = 0; + + /* @phpstan-ignore-next-line */ + while (count($callbacks) < count($this->value)) { + $callbacks[] = $callbacks[$index]; + /* @phpstan-ignore-next-line */ + $index = $index < count($this->value) - 1 ? $index + 1 : 0; + } + + /* @phpstan-ignore-next-line */ + foreach ($this->value as $index => $item) { + call_user_func($callbacks[$index], expect($item)); + } + + return $this; + } + /** * Asserts that two variables have the same type and * value. Used on objects, it asserts that two diff --git a/tests/Expect/each.php b/tests/Expect/each.php index b0a1b01..2857cd2 100644 --- a/tests/Expect/each.php +++ b/tests/Expect/each.php @@ -4,7 +4,7 @@ test('an exception is thrown if the the type is not iterable', function () { expect('Foobar')->each()->toEqual('Foobar'); -})->throws(BadMethodCallException::class, 'Expectation value is not traversable.'); +})->throws(BadMethodCallException::class, 'Expectation value is not iterable.'); it('expects on each item', function () { expect([1, 1, 1]) diff --git a/tests/Expect/sequence.php b/tests/Expect/sequence.php index 8f1e953..4502bc7 100644 --- a/tests/Expect/sequence.php +++ b/tests/Expect/sequence.php @@ -2,11 +2,10 @@ test('an exception is thrown if the the type is not iterable', function () { expect('Foobar')->each->sequence(); -})->throws(BadMethodCallException::class, 'Expectation value is not traversable.'); +})->throws(BadMethodCallException::class, 'Expectation value is not iterable.'); -test('allows for sequences of checks to be run on traversable data', function () { +test('allows for sequences of checks to be run on iterable data', function () { expect([1, 2, 3]) - ->each ->sequence( function ($expectation) { $expectation->toBeInt()->toEqual(1); }, function ($expectation) { $expectation->toBeInt()->toEqual(2); }, @@ -18,7 +17,6 @@ function ($expectation) { $expectation->toBeInt()->toEqual(3); }, test('loops back to the start if it runs out of sequence items', function () { expect([1, 2, 3, 1, 2, 3, 1, 2]) - ->each ->sequence( function ($expectation) { $expectation->toBeInt()->toEqual(1); }, function ($expectation) { $expectation->toBeInt()->toEqual(2); }, @@ -28,9 +26,8 @@ function ($expectation) { $expectation->toBeInt()->toEqual(3); }, expect(static::getCount())->toBe(16); }); -test('it works if the number of items in the traversable is smaller than the number of expectations', function () { +test('it works if the number of items in the iterable is smaller than the number of expectations', function () { expect([1, 2]) - ->each ->sequence( function ($expectation) { $expectation->toBeInt()->toEqual(1); }, function ($expectation) { $expectation->toBeInt()->toEqual(2); },