Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/Concerns/Asserts/AssertsElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function assertDoesntMatchSelector(string $selector, ?string $message = n

/*
|--------------------------------------------------------------------------
| Assert Count
| Assert Elements Count
|--------------------------------------------------------------------------
*/

Expand Down Expand Up @@ -207,6 +207,36 @@ public function assertElementsCountLessThanOrEqual(string $selector, int $count,
return $this;
}

/*
|--------------------------------------------------------------------------
| Assert Elements Present/Missing
|--------------------------------------------------------------------------
*/

/** Assert the element contains one or more elements matching the given selector. */
public function assertElementsPresent(string $selector, ?string $message = null): static
{
$this->assertElementsCountGreaterThan($selector, 0, $message ?? sprintf(
"The element [%s] doesn't have one or more elements matching the given selector [%s].",
$this->identifier(),
$selector,
));

return $this;
}

/** Assert the element contains no elements matching the given selector. */
public function assertElementsMissing(string $selector, ?string $message = null): static
{
$this->assertElementsCount($selector, 0, $message ?? sprintf(
'The element [%s] has one or more elements matching the given selector [%s].',
$this->identifier(),
$selector,
));

return $this;
}

/*
|--------------------------------------------------------------------------
| Assert Text
Expand Down
9 changes: 2 additions & 7 deletions tests/Integration/AssertableHtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ public function test_assertable_html(): void
<li id="baz">Baz</li>
</ul>

<!-- Form -->
<form method="get" action="/foo/bar" enctype="multipart/form-data">
<label>Name <input type="text" name="name" value="Foo Bar"></label>
<label>Age <input type="number" name="age" value="42"></label>
<button type="submit">Save</button>
</form>

<!-- Custom Element -->
<my-web-component>I am a web component.</my-web-component>
</body>
Expand All @@ -62,6 +55,8 @@ public function test_assertable_html(): void

$html->one('ul', function (AssertableElement $el): void {
$el->assertElementsCount('li', 3);
$el->assertElementsPresent('li');
$el->assertElementsMissing('foo');

$el->one('li:nth-child(1)', fn (AssertableElement $el) => $el->assertAttributeEquals('id', 'foo'));
$el->one('li:nth-child(2)', fn (AssertableElement $el) => $el->assertAttributeEquals('id', 'bar'));
Expand Down
41 changes: 36 additions & 5 deletions tests/Unit/Concerns/Asserts/AssertsElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ public function test_assert_doesnt_match_selector_fails(): void

/*
|--------------------------------------------------------------------------
| Assert Count
| Assert Elements Count
|--------------------------------------------------------------------------
*/

public function test_assert_count_comparisons_pass(): void
public function test_elements_assert_count_comparisons_pass(): void
{
$assertable = $this->getAssertableElement(<<<'HTML'
<ul>
Expand Down Expand Up @@ -148,8 +148,8 @@ public function test_assert_count_comparisons_pass(): void
$assertable->assertNumberOfElements('li', '<=', 4);
}

#[DataProvider('assert_count_comparisons_fail_data_provider')]
public function test_assert_count_comparisons_fail(string $selector, string $comparison, int $expected, string $message): void
#[DataProvider('assert_elements_count_comparisons_fail_data_provider')]
public function test_elements_assert_count_comparisons_fail(string $selector, string $comparison, int $expected, string $message): void
{
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage($message);
Expand All @@ -173,7 +173,7 @@ public function test_assert_count_comparisons_fail(string $selector, string $com
};
}

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

/*
|--------------------------------------------------------------------------
| Assert Elements Present/Missing
|--------------------------------------------------------------------------
*/

public function test_assert_elements_present_missing_pass(): void
{
$this->getAssertableElement('<ul><li>Foo</li></ul>')
->assertElementsPresent('li')
->assertElementsMissing('p');
}

public function test_assert_elements_present_fails(): void
{
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage("The element [ul] doesn't have one or more elements matching the given selector [p].");

$this->getAssertableElement('<ul><li>Foo</li></ul>')
->assertElementsPresent('p');
}

public function test_assert_elements_missing_fails(): void
{
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('The element [ul] has one or more elements matching the given selector [li].');

$this->getAssertableElement('<ul><li>Foo</li></ul>')
->assertElementsMissing('li');
}

/*
|--------------------------------------------------------------------------
| Assert Text
Expand Down