From 0e4a12e14a75b0ca6f5fcfb831bf75470cd78801 Mon Sep 17 00:00:00 2001 From: Nick Wilde Date: Tue, 17 Oct 2017 13:50:08 -0700 Subject: [PATCH 1/2] Add new function WebAssert::responseContainsCount --- src/WebAssert.php | 20 ++++++++++++++++++++ tests/WebAssertTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/WebAssert.php b/src/WebAssert.php index 0f0e6fa71..be2d2b0e8 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 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($actual, $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') From 843b889443377b5c7a512e2696b66c5e8728c839 Mon Sep 17 00:00:00 2001 From: Nick Wilde Date: Thu, 11 Jan 2018 10:10:02 -0800 Subject: [PATCH 2/2] Update WebAssert.php Make responseContainsCount case insensitive --- src/WebAssert.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/WebAssert.php b/src/WebAssert.php index be2d2b0e8..7bdb0a8f1 100644 --- a/src/WebAssert.php +++ b/src/WebAssert.php @@ -342,7 +342,7 @@ public function responseNotContains($text) } /** - * Checks that page HTML (response content) contains text n times. + * Checks that page HTML (response content) contains case insensitive text n times. * * @param string $text expected text * @@ -358,7 +358,7 @@ public function responseContainsCount($text, $number) $text, $number ); - $this->assert(substr_count($actual, $text) === $number, $message); + $this->assert(substr_count(strtolower($actual), strtolower($text)) === $number, $message); } /**