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 (#22)

* implement each method to allow fluent assertions against all elements that match selector

* update readme to describe the each() method

* use assertNotEmpty instead of assertNotCount

* fix readme section that was using contains() with a callback argument
  • Loading branch information
FRFlor authored Jun 17, 2024
1 parent 970caf0 commit bd882c2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,32 @@ $this->get('/some-route')
});
});
```
If you want to make an assertion against all elements that match the selection, you may use 'each'.

This means that you can infinitely assert down the dom structure.
```php
$this->get('/some-route')
->assertElementExists('#overview', function (AssertElement $assert) {
$assert->each('li', function (AssertElement $element) {
$element->has('class', 'list-item');
});
});
```

You can also infinitely assert down the dom structure.
```php
$this->get('/some-route')
->assertElementExists(function (AssertElement $element) {
$element->find('div', function (AssertElement $element) {
$element->is('div');
$element->contains('p', function (AssertElement $element) {
$element->find('p', function (AssertElement $element) {
$element->is('p');
$element->contains('#label', function (AssertElement $element) {
$element->find('#label', function (AssertElement $element) {
$element->is('span');
});
});
$element->contains('p:nth-of-type(2)', function (AssertElement $element) {
$element->find('p:nth-of-type(2)', function (AssertElement $element) {
$element->is('p');
$element->contains('.sub-header', function (AssertElement $element) {
$element->find('.sub-header', function (AssertElement $element) {
$element->is('h4');
});
});
Expand Down
16 changes: 16 additions & 0 deletions src/Asserts/Traits/UsesElementAsserts.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ public function find(string $selector, $callback = null): self
return $this;
}

public function each(string $selector, $callback): self
{
$elements = $this->getParser()->queryAll($selector);
Assert::assertNotEmpty(
$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
->each('select', fn (AssertElement $select) => $select->has('name'))
);
});

it('fails when each() is used but no elements match the selector', function () {
$this->get('form')
->assertOk()
->assertElementExists(fn (AssertElement $view) => $view
->each('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 bd882c2

Please sign in to comment.