From 4d2e56289c16066785dda5776b66161039c727fd Mon Sep 17 00:00:00 2001 From: Alexey Murz Korepov Date: Wed, 4 May 2022 16:08:05 +0000 Subject: [PATCH 1/3] Add case sensitive comparison option --- src/WebAssert.php | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/WebAssert.php b/src/WebAssert.php index b03c9829e..3a079599e 100644 --- a/src/WebAssert.php +++ b/src/WebAssert.php @@ -494,17 +494,19 @@ 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 + * @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 +520,19 @@ 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 + * @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.', From f9fd54562ae90c7d839f1a6f03bcd254c72fffb3 Mon Sep 17 00:00:00 2001 From: Alexey Murz Korepov Date: Wed, 4 May 2022 17:14:00 +0000 Subject: [PATCH 2/3] Added tests to case sensitive comparison --- tests/WebAssertTest.php | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) 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() From 30cc903ad7ef6e442be8d55379997be4258689ec Mon Sep 17 00:00:00 2001 From: Alexey Murz Korepov Date: Thu, 5 May 2022 08:51:11 +0400 Subject: [PATCH 3/3] Explain case-insensitive by default in funciton docs --- src/WebAssert.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/WebAssert.php b/src/WebAssert.php index 3a079599e..3f5745b36 100644 --- a/src/WebAssert.php +++ b/src/WebAssert.php @@ -494,6 +494,9 @@ public function elementTextNotContains($selectorType, $selector, $text) /** * Checks that specific element contains HTML. * + * 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 @@ -520,6 +523,9 @@ public function elementContains($selectorType, $selector, $html, $caseSensitive /** * Checks that specific element does not contains HTML. * + * 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