Merge pull request #460 from danharrin/feature/each-keys

feature: Pass `each()` keys to closures
This commit is contained in:
Nuno Maduro
2022-01-20 10:36:09 +00:00
committed by GitHub
2 changed files with 10 additions and 2 deletions

View File

@ -125,8 +125,8 @@ final class Expectation
} }
if (is_callable($callback)) { if (is_callable($callback)) {
foreach ($this->value as $item) { foreach ($this->value as $key => $item) {
$callback(new self($item)); $callback(new self($item), $key);
} }
} }

View File

@ -87,3 +87,11 @@ it('accepts callables', function () {
expect(static::getCount())->toBe(12); 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);
});