Skip to content

Commit 8e6e1b0

Browse files
authored
Give each() callback access to foreach index (#33)
1 parent 1521c92 commit 8e6e1b0

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/Asserts/Traits/UsesElementAsserts.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ public function each(string $selector, $callback): self
8484
sprintf('Could not find any matching element for selector "%s"', $selector)
8585
);
8686

87-
foreach ($elements as $element) {
87+
foreach ($elements as $index => $element) {
8888
$elementAssert = new AssertElement($this->getContent(), $element);
89-
$callback($elementAssert);
89+
$callback($elementAssert, $index);
9090
}
9191

9292
return $this;

tests/DomTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,15 @@
283283
);
284284
});
285285

286+
it('can run assertions against indexed elements that match the selection', function () {
287+
$this->get('form')
288+
->assertOk()
289+
->assertElementExists(function (AssertElement $view) {
290+
$all = $view->getParser()->queryAll('select');
291+
$view->each('select', fn (AssertElement $select, int $index) => $select->containsText($all->item($index)->nodeValue));
292+
});
293+
});
294+
286295
it('fails when each() is used but no elements match the selector', function () {
287296
$this->get('form')
288297
->assertOk()

0 commit comments

Comments
 (0)