Skip to content

Commit eb28bef

Browse files
authored
Merge pull request #22 from chaseconey/feat/empty-assertions
feat: add empty assertions
2 parents e5c9cd6 + e58cba1 commit eb28bef

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ $this->assertView('welcome')->in('title')->contains('Laravel');
6767
$this->assertView('welcome')->in('.content')->contains('Nova');
6868
```
6969

70+
### `empty`
71+
72+
Asserts that the view has no text content.
73+
74+
_Note: empty html nodes are not considered in this check._
75+
76+
```php
77+
$this->assertView('empty')->in('.empty-div')->empty();
78+
```
79+
7080
### `first`
7181

7282
Filters the view and returns only the first element matching the selector.

src/ViewAssertion.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,32 @@ public function last(string $selector): ViewAssertion
9595
public function contains(string $text): ViewAssertion
9696
{
9797
Assert::assertStringContainsString(
98-
(string) $text,
98+
(string)$text,
9999
$this->html,
100100
"Failed asserting that the text `{$text}` exists within `{$this->html}`."
101101
);
102102

103103
return $this;
104104
}
105105

106+
/**
107+
* Asserts that the view, at given selector, has no content.
108+
*/
109+
public function empty(): ViewAssertion
110+
{
111+
$content = "";
112+
foreach ($this->crawler->getIterator() as $node) {
113+
$content .= trim($node->textContent);
114+
}
115+
116+
Assert::assertEmpty(
117+
$content,
118+
"Failed asserting that the text `{$content}` is empty."
119+
);
120+
121+
return $this;
122+
}
123+
106124
/**
107125
* Asserts that the view, at the **root element**, contains the given attribute value.
108126
*/

tests/AssertEmpty.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
4+
namespace Tests;
5+
6+
use NunoMaduro\LaravelMojito\InteractsWithViews;
7+
use PHPUnit\Framework\AssertionFailedError;
8+
use PHPUnit\Framework\TestCase;
9+
10+
/**
11+
* @internal
12+
*/
13+
final class AssertEmpty extends TestCase
14+
{
15+
use InteractsWithViews;
16+
17+
public function testEmpty(): void
18+
{
19+
$this->assertView('empty')->in('.empty-div')->empty();
20+
$this->assertView('empty')->in('.empty-with-empty-nodes')->empty();
21+
$this->assertView('empty')->in('.empty-with-space')->empty();
22+
}
23+
24+
public function testNotEmpty(): void
25+
{
26+
$this->expectException(AssertionFailedError::class);
27+
28+
$this->assertView('empty')->in('.not-empty')->empty();
29+
}
30+
}

tests/fixtures/empty.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<div>
2+
<div class="empty-div"></div>
3+
<div class="empty-with-space">
4+
5+
</div>
6+
<div class="empty-with-empty-nodes">
7+
<span></span>
8+
</div>
9+
10+
<div class="not-empty-text">
11+
<span></span>
12+
Hello
13+
</div>
14+
</div>

0 commit comments

Comments
 (0)