diff --git a/src/WebAssert.php b/src/WebAssert.php index b03c9829e..3f5745b36 100644 --- a/src/WebAssert.php +++ b/src/WebAssert.php @@ -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.', @@ -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.', diff --git a/tests/WebAssertTest.php b/tests/WebAssertTest.php index 22c58b7bc..467c8f5d4 100644 --- a/tests/WebAssertTest.php +++ b/tests/WebAssertTest.php @@ -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() @@ -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()