Skip to content
Open
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
20 changes: 20 additions & 0 deletions src/WebAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
28 changes: 28 additions & 0 deletions tests/WebAssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down