Skip to content

Commit 76e55c7

Browse files
committed
Merge branch 'main' into wip-custom-el-matcher
2 parents bcadc9c + 8f40220 commit 76e55c7

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

src/Concerns/Asserts/AssertsElement.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function assertDoesntMatchSelector(string $selector, ?string $message = n
112112

113113
/*
114114
|--------------------------------------------------------------------------
115-
| Assert Count
115+
| Assert Elements Count
116116
|--------------------------------------------------------------------------
117117
*/
118118

@@ -207,6 +207,36 @@ public function assertElementsCountLessThanOrEqual(string $selector, int $count,
207207
return $this;
208208
}
209209

210+
/*
211+
|--------------------------------------------------------------------------
212+
| Assert Elements Present/Missing
213+
|--------------------------------------------------------------------------
214+
*/
215+
216+
/** Assert the element contains one or more elements matching the given selector. */
217+
public function assertElementsPresent(string $selector, ?string $message = null): static
218+
{
219+
$this->assertElementsCountGreaterThan($selector, 0, $message ?? sprintf(
220+
"The element [%s] doesn't have one or more elements matching the given selector [%s].",
221+
$this->identifier(),
222+
$selector,
223+
));
224+
225+
return $this;
226+
}
227+
228+
/** Assert the element contains no elements matching the given selector. */
229+
public function assertElementsMissing(string $selector, ?string $message = null): static
230+
{
231+
$this->assertElementsCount($selector, 0, $message ?? sprintf(
232+
'The element [%s] has one or more elements matching the given selector [%s].',
233+
$this->identifier(),
234+
$selector,
235+
));
236+
237+
return $this;
238+
}
239+
210240
/*
211241
|--------------------------------------------------------------------------
212242
| Assert Text

tests/Integration/AssertableHtmlTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public function test_assertable_html(): void
5555

5656
$html->one('ul', function (AssertableElement $el): void {
5757
$el->assertElementsCount('li', 3);
58+
$el->assertElementsPresent('li');
59+
$el->assertElementsMissing('foo');
5860

5961
$el->one('li:nth-child(1)', fn (AssertableElement $el) => $el->assertAttributeEquals('id', 'foo'));
6062
$el->one('li:nth-child(2)', fn (AssertableElement $el) => $el->assertAttributeEquals('id', 'bar'));

tests/Unit/Concerns/Asserts/AssertsElementTest.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ public function test_assert_doesnt_match_selector_fails(): void
114114

115115
/*
116116
|--------------------------------------------------------------------------
117-
| Assert Count
117+
| Assert Elements Count
118118
|--------------------------------------------------------------------------
119119
*/
120120

121-
public function test_assert_count_comparisons_pass(): void
121+
public function test_elements_assert_count_comparisons_pass(): void
122122
{
123123
$assertable = $this->getAssertableElement(<<<'HTML'
124124
<ul>
@@ -148,8 +148,8 @@ public function test_assert_count_comparisons_pass(): void
148148
$assertable->assertNumberOfElements('li', '<=', 4);
149149
}
150150

151-
#[DataProvider('assert_count_comparisons_fail_data_provider')]
152-
public function test_assert_count_comparisons_fail(string $selector, string $comparison, int $expected, string $message): void
151+
#[DataProvider('assert_elements_count_comparisons_fail_data_provider')]
152+
public function test_elements_assert_count_comparisons_fail(string $selector, string $comparison, int $expected, string $message): void
153153
{
154154
$this->expectException(AssertionFailedError::class);
155155
$this->expectExceptionMessage($message);
@@ -173,7 +173,7 @@ public function test_assert_count_comparisons_fail(string $selector, string $com
173173
};
174174
}
175175

176-
public static function assert_count_comparisons_fail_data_provider(): iterable
176+
public static function assert_elements_count_comparisons_fail_data_provider(): iterable
177177
{
178178
yield 'equals' => [
179179
'li', '=', 1, "The element [ul] doesn't have exactly [1] elements matching the given selector [li].",
@@ -200,6 +200,37 @@ public static function assert_count_comparisons_fail_data_provider(): iterable
200200
];
201201
}
202202

203+
/*
204+
|--------------------------------------------------------------------------
205+
| Assert Elements Present/Missing
206+
|--------------------------------------------------------------------------
207+
*/
208+
209+
public function test_assert_elements_present_missing_pass(): void
210+
{
211+
$this->getAssertableElement('<ul><li>Foo</li></ul>')
212+
->assertElementsPresent('li')
213+
->assertElementsMissing('p');
214+
}
215+
216+
public function test_assert_elements_present_fails(): void
217+
{
218+
$this->expectException(AssertionFailedError::class);
219+
$this->expectExceptionMessage("The element [ul] doesn't have one or more elements matching the given selector [p].");
220+
221+
$this->getAssertableElement('<ul><li>Foo</li></ul>')
222+
->assertElementsPresent('p');
223+
}
224+
225+
public function test_assert_elements_missing_fails(): void
226+
{
227+
$this->expectException(AssertionFailedError::class);
228+
$this->expectExceptionMessage('The element [ul] has one or more elements matching the given selector [li].');
229+
230+
$this->getAssertableElement('<ul><li>Foo</li></ul>')
231+
->assertElementsMissing('li');
232+
}
233+
203234
/*
204235
|--------------------------------------------------------------------------
205236
| Assert Text

0 commit comments

Comments
 (0)