diff --git a/src/Expectation.php b/src/Expectation.php index 8dc287a0..300820c1 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -125,8 +125,8 @@ final class Expectation } if (is_callable($callback)) { - foreach ($this->value as $item) { - $callback(new self($item)); + foreach ($this->value as $key => $item) { + $callback(new self($item), $key); } } diff --git a/tests/Features/Expect/each.php b/tests/Features/Expect/each.php index 0f26a974..63b3d60c 100644 --- a/tests/Features/Expect/each.php +++ b/tests/Features/Expect/each.php @@ -87,3 +87,11 @@ it('accepts callables', function () { expect(static::getCount())->toBe(12); }); + +it('passes the key of the current item to callables', function () { + expect([1, 2, 3])->each(function ($number, $key) { + expect($key)->toBeInt(); + }); + + expect(static::getCount())->toBe(3); +});