diff --git a/src/Asserts/Traits/UsesElementAsserts.php b/src/Asserts/Traits/UsesElementAsserts.php index 92fb5e3..7daeb4d 100644 --- a/src/Asserts/Traits/UsesElementAsserts.php +++ b/src/Asserts/Traits/UsesElementAsserts.php @@ -147,6 +147,42 @@ public function doesntContain(string $elementName, array $attributes = []): self return $this; } + public function containsText(string $needle, bool $ignoreCase = false): self + { + $text = $this->getAttribute('text'); + + $assertFunction = $ignoreCase ? + 'assertStringContainsStringIgnoringCase' : + 'assertStringContainsString'; + + call_user_func( + [PHPUnit::class, $assertFunction], + $needle, + $text, + sprintf('Could not find text content "%s" containing %s', $text, $needle) + ); + + return $this; + } + + public function doesntContainText(string $needle, bool $ignoreCase = false): self + { + $text = $this->getAttribute('text'); + + $assertFunction = $ignoreCase ? + 'assertStringNotContainsStringIgnoringCase' : + 'assertStringNotContainsString'; + + call_user_func( + [PHPUnit::class, $assertFunction], + $needle, + $text, + sprintf('Found text content "%s" containing %s', $text, $needle) + ); + + return $this; + } + public function is(string $type): self { PHPUnit::assertEquals( diff --git a/tests/DomTest.php b/tests/DomTest.php index 7be6f7d..42be8f5 100644 --- a/tests/DomTest.php +++ b/tests/DomTest.php @@ -182,6 +182,27 @@ }); }); +it('can match text content containing a string', function () { + $this->get('nesting') + ->assertElementExists('p.foo.bar', function (AssertElement $element) { + $element->containsText('Bar'); + }); +}); + +it('can match text content containing a string ignoring case', function () { + $this->get('nesting') + ->assertElementExists('p.foo.bar', function (AssertElement $element) { + $element->containsText('bar', true); + }); +}); + +it('can match text content not containing a string', function () { + $this->get('nesting') + ->assertElementExists('p.foo.bar', function (AssertElement $element) { + $element->doesntContainText('bar'); + }); +}); + it('can match a class no matter the order', function () { $this->get('nesting') ->assertElementExists(function (AssertElement $element) {