Skip to content

Commit 49e9247

Browse files
committed
Add an assertSelectorCount() assertion.
This assertion validates the number of occurrences of a particular selector within a document. Refs #3.
1 parent 970fb24 commit 49e9247

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/MarkupAssertionsTrait.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ public function assertNotContainsSelector($selector, $output = '', $message = ''
4242
$this->assertEquals(0, count($results), $message);
4343
}
4444

45+
/**
46+
* Assert the number of times an element matching the given selector is found.
47+
*
48+
* @param int $count The number of matching elements expected.
49+
* @param string $selector A query selector for the element to find.
50+
* @param string $output The markup to run the assertion against.
51+
* @param string $message A message to display if the assertion fails.
52+
*/
53+
public function assertSelectorCount($count, $selector, $output = '', $message = '')
54+
{
55+
$results = $this->executeDomQuery($output, $selector);
56+
57+
$this->assertCount($count, $results, $message);
58+
}
59+
4560
/**
4661
* Assert that an element with the given attributes exists in the given markup.
4762
*

tests/MarkupAssertionsTraitTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ public function testAssertNotContainsSelector($selector)
5151
);
5252
}
5353

54+
public function testAssertSelectorCount()
55+
{
56+
$this->testcase->assertSelectorCount(
57+
3,
58+
'li',
59+
'<ul><li>1</li><li>2</li><li>3</li></ul>'
60+
);
61+
}
62+
5463
public function testAssertHasElementWithAttributes()
5564
{
5665
$this->testcase->assertHasElementWithAttributes(

0 commit comments

Comments
 (0)