Skip to content

Commit

Permalink
Find text content
Browse files Browse the repository at this point in the history
  • Loading branch information
balazs authored and balazs committed Feb 10, 2023
1 parent b3bc3ed commit 32ba65a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Asserts/Traits/UsesElementAsserts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
21 changes: 21 additions & 0 deletions tests/DomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 32ba65a

Please sign in to comment.