Skip to content

Commit

Permalink
implement findAll to allow fluent assertions against all elements tha…
Browse files Browse the repository at this point in the history
…t match selector
  • Loading branch information
FRFlor committed Jun 17, 2024
1 parent 970caf0 commit 566bb8e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Asserts/Traits/UsesElementAsserts.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ public function find(string $selector, $callback = null): self
return $this;
}

public function findAll(string $selector, $callback): self
{
$elements = $this->getParser()->queryAll($selector);
Assert::assertNotCount(
0,
$elements,
sprintf('Could not find any matching element for selector "%s"', $selector)
);

foreach ($elements as $element) {
$elementAssert = new AssertElement($this->getContent(), $element);
$callback($elementAssert);
}

return $this;
}

public function contains(string $selector, $attributes = null, $count = 0): self
{
Assert::assertNotNull(
Expand Down
16 changes: 16 additions & 0 deletions tests/DomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,22 @@
});
})->throws(AssertionFailedError::class, 'Could not find a matching "div" with data:');

it('can run assertions against all elements that match the selection', function () {
$this->get('form')
->assertOk()
->assertElementExists(fn (AssertElement $view) => $view
->findAll('select', fn (AssertElement $select) => $select->has('name'))
);
});

it('fails when findAll is used but no elements match the selector', function () {
$this->get('form')
->assertOk()
->assertElementExists(fn (AssertElement $view) => $view
->findAll('img', fn (AssertElement $image) => $image->has('alt'))
);
})->throws(AssertionFailedError::class);

it('can find a nested element with content functional', function () {
$this->get('nesting')
->assertElementExists(function (AssertElement $element) {
Expand Down

0 comments on commit 566bb8e

Please sign in to comment.