From 8050501195de8a32be458a785a83ec1aadbded51 Mon Sep 17 00:00:00 2001 From: Peter Philipp Date: Fri, 24 May 2019 17:37:55 +1200 Subject: [PATCH] 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 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/WebAssert.php b/src/WebAssert.php index 75cf7b5b4..f429056f4 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. *