diff --git a/src/WebAssert.php b/src/WebAssert.php index 0f0e6fa71..7bdb0a8f1 100644 --- a/src/WebAssert.php +++ b/src/WebAssert.php @@ -341,6 +341,26 @@ public function responseNotContains($text) $this->assert(!preg_match($regex, $actual), $message); } + /** + * Checks that page HTML (response content) contains case insensitive text n times. + * + * @param string $text expected text + * + * @param integer $number expected number of occurrences + * + * @throws ExpectationException + */ + public function responseContainsCount($text, $number) + { + $actual = $this->session->getPage()->getContent(); + $message = sprintf( + 'The string "%s" was not found the expected %d times in the HTML response of the current page.', + $text, + $number + ); + $this->assert(substr_count(strtolower($actual), strtolower($text)) === $number, $message); + } + /** * Checks that page HTML (response content) matches regex. * diff --git a/tests/WebAssertTest.php b/tests/WebAssertTest.php index ef55d2d2e..2221818bd 100644 --- a/tests/WebAssertTest.php +++ b/tests/WebAssertTest.php @@ -494,6 +494,34 @@ public function testResponseNotContains() ); } + public function testResponseContainsCount() + { + $page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement') + ->disableOriginalConstructor() + ->getMock() + ; + + $this->session + ->expects($this->exactly(2)) + ->method('getPage') + ->will($this->returnValue($page)) + ; + + $page + ->expects($this->exactly(2)) + ->method('getContent') + ->will($this->returnValue('Some page text with page in it twice')) + ; + + $this->assertCorrectAssertion('responseContainsCount', array('page', 2)); + $this->assertWrongAssertion( + 'responseContainsCount', + array('page', 3), + 'Behat\\Mink\\Exception\\ExpectationException', + 'The string "page" was not found the expected 3 times in the HTML response of the current page.' + ); + } + public function testResponseMatches() { $page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement')