From 13894a3f44e84719ba64e853f7c138eb6f90eb27 Mon Sep 17 00:00:00 2001 From: Peter Philipp Date: Fri, 24 May 2019 17:37:55 +1200 Subject: [PATCH] Add fieldValueMatches / fieldValueNotMatches: Squash: * Added tests. * Fixed inversion of condition. * Add fieldValueMatches / fieldValueNotMatches Would be hand to allow pattern assertions on field values like on the page text. This was mentioned here #696 but: "PR got lost in the sands of time" :) --- src/WebAssert.php | 38 +++++++++++++++ tests/WebAssertTest.php | 104 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) diff --git a/src/WebAssert.php b/src/WebAssert.php index 75cf7b5b4..e56515a07 100644 --- a/src/WebAssert.php +++ b/src/WebAssert.php @@ -695,6 +695,44 @@ public function fieldValueNotEquals($field, $value, TraversableElement $containe $this->assert(!preg_match($regex, $actual), $message); } + /** + * Checks that specific field matches provided regex. + * + * @param string $field field id|name|label|value + * @param string $regex pattern + * @param TraversableElement $container document to check against + * + * @throws ExpectationException + */ + public function fieldValueMatches($field, $regex, TraversableElement $container = null) + { + $node = $this->fieldExists($field, $container); + $actual = $node->getValue(); + + $message = sprintf('The pattern "%s" was not found in the value "%s" of field "%s".', $regex, $actual, $field); + + $this->assert((bool) preg_match($regex, $actual), $message); + } + + /** + * Checks that specific field have provided value. + * + * @param string $field field id|name|label|value + * @param string $regex pattern + * @param TraversableElement $container document to check against + * + * @throws ExpectationException + */ + public function fieldValueNotMatches($field, $regex, TraversableElement $container = null) + { + $node = $this->fieldExists($field, $container); + $actual = $node->getValue(); + + $message = sprintf('The pattern "%s" was found in the value "%s" of field "%s", but it should not.', $regex, $actual, $field); + + $this->assert(!(bool) preg_match($regex, $actual), $message); + } + /** * Checks that specific checkbox is checked. * diff --git a/tests/WebAssertTest.php b/tests/WebAssertTest.php index f9a52c6d8..f9af64967 100644 --- a/tests/WebAssertTest.php +++ b/tests/WebAssertTest.php @@ -1293,6 +1293,110 @@ public function testFieldValueNotEquals() $this->assertCorrectAssertion('fieldValueNotEquals', array('username', '')); } + public function testFieldValueMatches() + { + $page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement') + ->disableOriginalConstructor() + ->getMock() + ; + + $element = $this->getMockBuilder('Behat\\Mink\\Element\\NodeElement') + ->disableOriginalConstructor() + ->getMock() + ; + + $this->session + ->expects($this->exactly(4)) + ->method('getPage') + ->will($this->returnValue($page)) + ; + + $page + ->expects($this->exactly(4)) + ->method('findField') + ->with('username') + ->will($this->returnValue($element)) + ; + + $element + ->expects($this->exactly(4)) + ->method('getValue') + ->will($this->returnValue(234)) + ; + + $this->assertCorrectAssertion('fieldValueMatches', array('username', '/234/')); + $this->assertWrongAssertion( + 'fieldValueMatches', + array('username', '/235/'), + 'Behat\\Mink\\Exception\\ExpectationException', + 'The pattern "/235/" was not found in the value "234" of field "username".' + ); + $this->assertWrongAssertion( + 'fieldValueMatches', + array('username', '/22/'), + 'Behat\\Mink\\Exception\\ExpectationException', + 'The pattern "/22/" was not found in the value "234" of field "username".' + ); + $this->assertWrongAssertion( + 'fieldValueMatches', + array('username', '/\D+/'), + 'Behat\\Mink\\Exception\\ExpectationException', + 'The pattern "/\D+/" was not found in the value "234" of field "username".' + ); + } + + public function testFieldValueNotMatches() + { + $page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement') + ->disableOriginalConstructor() + ->getMock() + ; + + $element = $this->getMockBuilder('Behat\\Mink\\Element\\NodeElement') + ->disableOriginalConstructor() + ->getMock() + ; + + $this->session + ->expects($this->exactly(4)) + ->method('getPage') + ->will($this->returnValue($page)) + ; + + $page + ->expects($this->exactly(4)) + ->method('findField') + ->with('username') + ->will($this->returnValue($element)) + ; + + $element + ->expects($this->exactly(4)) + ->method('getValue') + ->will($this->returnValue(235)) + ; + + $this->assertCorrectAssertion('fieldValueNotMatches', array('username', '/234/')); + $this->assertWrongAssertion( + 'fieldValueNotMatches', + array('username', '/235/'), + 'Behat\\Mink\\Exception\\ExpectationException', + 'The pattern "/235/" was found in the value "235" of field "username", but it should not.' + ); + $this->assertWrongAssertion( + 'fieldValueNotMatches', + array('username', '/23/'), + 'Behat\\Mink\\Exception\\ExpectationException', + 'The pattern "/23/" was found in the value "235" of field "username", but it should not.' + ); + $this->assertWrongAssertion( + 'fieldValueNotMatches', + array('username', '/\d+/'), + 'Behat\\Mink\\Exception\\ExpectationException', + 'The pattern "/\d+/" was found in the value "235" of field "username", but it should not.' + ); + } + public function testCheckboxChecked() { $page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement')