Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add case sensitive comparison option to elementContains and elementNotContains #830

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
30 changes: 20 additions & 10 deletions src/WebAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,17 +494,22 @@ public function elementTextNotContains($selectorType, $selector, $text)
/**
* Checks that specific element contains HTML.
*
* @param string $selectorType element selector type (css, xpath)
* @param string|array $selector element selector
* @param string $html expected text
* Comparison is case-insensitive by default. You can enable case-sensitive
* mode via passing `true` in the fourth argument.
*
* @param string $selectorType element selector type (css, xpath)
* @param string|array $selector element selector
* @param string $html expected text
* @param bool $caseSensitive use case sensitive comparison
*
* @throws ElementHtmlException
*/
public function elementContains($selectorType, $selector, $html)
public function elementContains($selectorType, $selector, $html, $caseSensitive = false)
{
$element = $this->elementExists($selectorType, $selector);
$actual = $element->getHtml();
$regex = '/'.preg_quote($html, '/').'/ui';
$regex = '/'.preg_quote($html, '/').'/u';
if ($caseSensitive === false) $regex .= 'i';

$message = sprintf(
'The string "%s" was not found in the HTML of the %s.',
Expand All @@ -518,17 +523,22 @@ public function elementContains($selectorType, $selector, $html)
/**
* Checks that specific element does not contains HTML.
*
* @param string $selectorType element selector type (css, xpath)
* @param string|array $selector element selector
* @param string $html expected text
* Comparison is case-insensitive by default. You can enable case-sensitive
* mode via passing `true` in the fourth argument.
*
* @param string $selectorType element selector type (css, xpath)
* @param string|array $selector element selector
* @param string $html expected text
* @param bool $caseSensitive use case sensitive comparison
*
* @throws ElementHtmlException
*/
public function elementNotContains($selectorType, $selector, $html)
public function elementNotContains($selectorType, $selector, $html, $caseSensitive = false)
{
$element = $this->elementExists($selectorType, $selector);
$actual = $element->getHtml();
$regex = '/'.preg_quote($html, '/').'/ui';
$regex = '/'.preg_quote($html, '/').'/u';
if ($caseSensitive === false) $regex .= 'i';

$message = sprintf(
'The string "%s" appears in the HTML of the %s, but it should not.',
Expand Down
32 changes: 23 additions & 9 deletions tests/WebAssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -927,31 +927,38 @@ public function testElementContains()
;

$this->session
->expects($this->exactly(2))
->expects($this->exactly(4))
->method('getPage')
->will($this->returnValue($page))
;

$page
->expects($this->exactly(2))
->expects($this->exactly(4))
->method('find')
->with('css', 'h2 > span')
->will($this->returnValue($element))
;

$element
->expects($this->exactly(2))
->expects($this->exactly(4))
->method('getHtml')
->will($this->returnValue('element html'))
;

$this->assertCorrectAssertion('elementContains', array('css', 'h2 > span', 'html'));
$this->assertCorrectAssertion('elementContains', array('css', 'h2 > span', 'HTML'));
$this->assertCorrectAssertion('elementContains', array('css', 'h2 > span', 'html', true));
$this->assertWrongAssertion(
'elementContains',
array('css', 'h2 > span', 'text'),
'Behat\\Mink\\Exception\\ExpectationException',
'The string "text" was not found in the HTML of the element matching css "h2 > span".'
);
$this->assertWrongAssertion(
'elementContains',
array('css', 'h2 > span', 'Html', true),
'Behat\\Mink\\Exception\\ExpectationException',
'The string "Html" was not found in the HTML of the element matching css "h2 > span".'
);
}

public function testElementNotContains()
Expand All @@ -967,31 +974,38 @@ public function testElementNotContains()
;

$this->session
->expects($this->exactly(2))
->expects($this->exactly(4))
->method('getPage')
->will($this->returnValue($page))
;

$page
->expects($this->exactly(2))
->expects($this->exactly(4))
->method('find')
->with('css', 'h2 > span')
->will($this->returnValue($element))
;

$element
->expects($this->exactly(2))
->expects($this->exactly(4))
->method('getHtml')
->will($this->returnValue('element html'))
->will($this->returnValue('element Html'))
;

$this->assertCorrectAssertion('elementNotContains', array('css', 'h2 > span', 'text'));
$this->assertCorrectAssertion('elementNotContains', array('css', 'h2 > span', 'Text'));
$this->assertCorrectAssertion('elementNotContains', array('css', 'h2 > span', 'html', true));
$this->assertWrongAssertion(
'elementNotContains',
array('css', 'h2 > span', 'html'),
'Behat\\Mink\\Exception\\ExpectationException',
'The string "html" appears in the HTML of the element matching css "h2 > span", but it should not.'
);
$this->assertWrongAssertion(
'elementNotContains',
array('css', 'h2 > span', 'Html', true),
'Behat\\Mink\\Exception\\ExpectationException',
'The string "Html" appears in the HTML of the element matching css "h2 > span", but it should not.'
);
}

public function testElementAttributeContains()
Expand Down