Skip to content

Commit

Permalink
Merge pull request #11 from balping/main
Browse files Browse the repository at this point in the history
Match string within text content
  • Loading branch information
sinnbeck authored Feb 13, 2023
2 parents b3bc3ed + dc9270e commit 2e8b093
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,10 @@ Livewire::test(UserForm::class)
| `->isDiv()` | Magic method. Same as `->is('div')` |
| `->contains($selector, $attributes, $count)` | Checks for any children of the current element |
| `->containsDiv, ['class' => 'foo'], 3)` | Magic method. Same as `->contains('div', ['class' => 'foo'], 3)` |
| `->containsText($needle, $ignoreCase)` | Checks if the element's text content contains a specified string |
| `->doesntContain($selector, $attributes)` | Ensures that there are no matching children |
| `->doesntContainDiv, ['class' => 'foo'])` | Magic method. Same as `->doesntContain('div', ['class' => 'foo'])` |
| `->doesntContainText($needle, $ignoreCase)` | Checks if the element's text content doesn't contain a specified string |
| `->find($selector, $callback)` | Find a specific child element and get a new AssertElement. Returns the first match. |
| `->findDiv(fn (AssertElement $element) => {})` | Magic method. Same as `->find('div', fn (AssertElement $element) => {})` |

Expand Down
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 2e8b093

Please sign in to comment.