From 962ff828d146589ee3533209535ef802a22a4c98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sylv=C3=A8re=20Gr=C3=A9geois?= Date: Tue, 19 Dec 2017 09:49:16 +0100 Subject: [PATCH 1/3] add JSON node should match context --- src/Context/JsonContext.php | 18 ++++++++++++++++++ tests/features/json.feature | 3 +++ 2 files changed, 21 insertions(+) diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index a8dc8dc1..684b0b30 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -76,6 +76,24 @@ public function theJsonNodesShouldBeEqualTo(TableNode $nodes) } } + /** + * Checks, that given JSON node matches given pattern + * + * @Then the JSON node :node should match :pattern + */ + public function theJsonNodeShouldMatch($node, $pattern) + { + $json = $this->getJson(); + + $actual = $this->inspector->evaluate($json, $node); + + if (!((bool) preg_match($pattern, $actual))) { + throw new \Exception( + sprintf("The node value is '%s'", json_encode($actual)) + ); + } + } + /** * Checks, that given JSON node is null * diff --git a/tests/features/json.feature b/tests/features/json.feature index 824dbaf1..cbc980a1 100644 --- a/tests/features/json.feature +++ b/tests/features/json.feature @@ -28,6 +28,9 @@ Feature: Testing JSONContext And the JSON node "numbers[3].complexeshizzle" should be equal to "true" And the JSON node "numbers[3].so[0]" should be equal to "very" And the JSON node "numbers[3].so[1].complicated" should be equal to "indeed" + And the JSON node "numbers[0]" should match "/o.{1}e/" + And the JSON node "numbers[1]" should match "/.{2}o/" + And the JSON node "numbers[2]" should match "/[a-z]{3}e.+/" And the JSON nodes should be equal to: | foo | bar | From e809513d1f0e72c2972c9d0a7a9db46a26b5218c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sylv=C3=A8re=20Gr=C3=A9geois?= Date: Tue, 19 Dec 2017 11:13:08 +0100 Subject: [PATCH 2/3] add entry to en.xliff.dist --- i18n/en.xliff.dist | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/i18n/en.xliff.dist b/i18n/en.xliff.dist index ad092dfe..efc49c32 100644 --- a/i18n/en.xliff.dist +++ b/i18n/en.xliff.dist @@ -199,6 +199,10 @@ the JSON node :node should be equal to :text + + the JSON node :node should match :pattern + + the JSON nodes should be equal to: From 3f7038a12aa1af7a7dd304c08ac6fdd9f6d31995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sylv=C3=A8re=20Gr=C3=A9geois?= Date: Tue, 19 Dec 2017 11:15:47 +0100 Subject: [PATCH 3/3] replace cast to bool with 0 comparison --- src/Context/JsonContext.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index 684b0b30..bdb7185b 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -87,7 +87,7 @@ public function theJsonNodeShouldMatch($node, $pattern) $actual = $this->inspector->evaluate($json, $node); - if (!((bool) preg_match($pattern, $actual))) { + if (preg_match($pattern, $actual) === 0) { throw new \Exception( sprintf("The node value is '%s'", json_encode($actual)) );