diff --git a/src/Concerns/Asserts/AssertsElement.php b/src/Concerns/Asserts/AssertsElement.php index deb4473..45eabb9 100644 --- a/src/Concerns/Asserts/AssertsElement.php +++ b/src/Concerns/Asserts/AssertsElement.php @@ -112,7 +112,7 @@ public function assertDoesntMatchSelector(string $selector, ?string $message = n /* |-------------------------------------------------------------------------- - | Assert Count + | Assert Elements Count |-------------------------------------------------------------------------- */ @@ -207,6 +207,36 @@ public function assertElementsCountLessThanOrEqual(string $selector, int $count, return $this; } + /* + |-------------------------------------------------------------------------- + | Assert Elements Present/Missing + |-------------------------------------------------------------------------- + */ + + /** Assert the element contains one or more elements matching the given selector. */ + public function assertElementsPresent(string $selector, ?string $message = null): static + { + $this->assertElementsCountGreaterThan($selector, 0, $message ?? sprintf( + "The element [%s] doesn't have one or more elements matching the given selector [%s].", + $this->identifier(), + $selector, + )); + + return $this; + } + + /** Assert the element contains no elements matching the given selector. */ + public function assertElementsMissing(string $selector, ?string $message = null): static + { + $this->assertElementsCount($selector, 0, $message ?? sprintf( + 'The element [%s] has one or more elements matching the given selector [%s].', + $this->identifier(), + $selector, + )); + + return $this; + } + /* |-------------------------------------------------------------------------- | Assert Text diff --git a/tests/Integration/AssertableHtmlTest.php b/tests/Integration/AssertableHtmlTest.php index 69c6f3a..0ed1133 100644 --- a/tests/Integration/AssertableHtmlTest.php +++ b/tests/Integration/AssertableHtmlTest.php @@ -41,13 +41,6 @@ public function test_assertable_html(): void
  • Baz
  • - -
    - - - -
    - I am a web component. @@ -62,6 +55,8 @@ public function test_assertable_html(): void $html->one('ul', function (AssertableElement $el): void { $el->assertElementsCount('li', 3); + $el->assertElementsPresent('li'); + $el->assertElementsMissing('foo'); $el->one('li:nth-child(1)', fn (AssertableElement $el) => $el->assertAttributeEquals('id', 'foo')); $el->one('li:nth-child(2)', fn (AssertableElement $el) => $el->assertAttributeEquals('id', 'bar')); diff --git a/tests/Unit/Concerns/Asserts/AssertsElementTest.php b/tests/Unit/Concerns/Asserts/AssertsElementTest.php index 07d98b0..1553d00 100644 --- a/tests/Unit/Concerns/Asserts/AssertsElementTest.php +++ b/tests/Unit/Concerns/Asserts/AssertsElementTest.php @@ -114,11 +114,11 @@ public function test_assert_doesnt_match_selector_fails(): void /* |-------------------------------------------------------------------------- - | Assert Count + | Assert Elements Count |-------------------------------------------------------------------------- */ - public function test_assert_count_comparisons_pass(): void + public function test_elements_assert_count_comparisons_pass(): void { $assertable = $this->getAssertableElement(<<<'HTML'