Skip to content

Commit 880cd90

Browse files
committed
feat: add empty assertions
1 parent e5c9cd6 commit 880cd90

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

src/ViewAssertion.php

+27-1
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,40 @@ 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, is empty.
108+
*/
109+
public function empty(): ViewAssertion
110+
{
111+
Assert::assertEmpty(
112+
$this->crawler->html(),
113+
"Failed asserting that the text `{$this->crawler->html()}` is empty."
114+
);
115+
116+
return $this;
117+
}
118+
119+
/**
120+
* Asserts that the view, at given selector, is not empty.
121+
*/
122+
public function notEmpty(): ViewAssertion
123+
{
124+
Assert::assertNotEmpty(
125+
$this->crawler->html(),
126+
"Failed asserting that the text `{$this->crawler->html()}` is not empty."
127+
);
128+
129+
return $this;
130+
}
131+
106132
/**
107133
* Asserts that the view, at the **root element**, contains the given attribute value.
108134
*/

tests/AssertEmpty.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}
21+
22+
public function testNotEmpty(): void
23+
{
24+
$this->expectException(AssertionFailedError::class);
25+
26+
$this->assertView('button')->in('.btn')->empty();
27+
}
28+
}

tests/AssertNotEmpty.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 AssertNotEmpty extends TestCase
14+
{
15+
use InteractsWithViews;
16+
17+
public function testEmpty(): void
18+
{
19+
$this->assertView('button')->in('.btn')->notEmpty();
20+
}
21+
22+
public function testNotEmpty(): void
23+
{
24+
$this->expectException(AssertionFailedError::class);
25+
26+
$this->assertView('empty')->in('.empty-div')->notEmpty();
27+
}
28+
}

tests/fixtures/empty.html

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
<div class="empty-div"></div>
3+
</div>

0 commit comments

Comments
 (0)