From 7b829a8fbd127039f050668a76734f591534b4b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=A4rz?= Date: Wed, 31 Jul 2024 10:09:50 +0200 Subject: [PATCH 1/3] - Fix ArrayDeepCompare to break when array is too deep - Add Handling of JSONException in JsonContext --- src/Context/JsonContext.php | 5 ++++- src/Helper/ArrayDeepCompare.php | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index c998b80..13f0d85 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -84,7 +84,10 @@ public function theResponseJsonDoesNotContain(PyStringNode $string): void { try { $this->theResponseJsonContains($string); - } catch (\DomainException|\JsonException) { + } catch (\DomainException|\JsonException $e) { + if ($e instanceof \JsonException) { + throw new \JsonException('JSON Syntax Error: ' . $e->getMessage()); + } return; } throw new \DomainException('the response json contains exact this data'); diff --git a/src/Helper/ArrayDeepCompare.php b/src/Helper/ArrayDeepCompare.php index de9545e..6bb3fa2 100644 --- a/src/Helper/ArrayDeepCompare.php +++ b/src/Helper/ArrayDeepCompare.php @@ -62,7 +62,14 @@ protected function hasDiff($a, $b, string $path = '', bool $reverseCheck = true) if (!\is_array($a) && !\is_array($b)) { // Scalar values -> compare if ($a !== $b) { - $this->difference = sprintf('%s: (%s) %s != (%s) %s', $path, \gettype($a), (string)($a ?? ''), \gettype($b), (string)($b ?? '')); + $this->difference = sprintf( + '%s: (%s) %s != (%s) %s', + $path, + \gettype($a), + (string)($a ?? ''), + \gettype($b), + (string)($b ?? '') + ); return true; } @@ -106,7 +113,7 @@ protected function hasDiff($a, $b, string $path = '', bool $reverseCheck = true) continue 2; } } - $this->difference = sprintf('%s: %s Missing', $subpath, (string) $v); + $this->difference = sprintf('%s: %s Missing', $subpath, is_array($v) ? json_encode($v) : (string) $v); return true; } @@ -114,7 +121,7 @@ protected function hasDiff($a, $b, string $path = '', bool $reverseCheck = true) // Still entries left in b? -> unequal if ($reverseCheck && \count($b)) { - $item = (string) array_reverse($b)[0]; + $item = is_array(array_reverse($b)[0]) ? json_encode(array_reverse($b)[0]) : (string) array_reverse($b)[0]; $subpath = ($path ? $path.'.' : '').$item; $this->difference = sprintf('%s: Extra', $subpath); @@ -123,4 +130,5 @@ protected function hasDiff($a, $b, string $path = '', bool $reverseCheck = true) return false; } -} + +} \ No newline at end of file From 61dcf60d1bbe40a04073bba3142def46bc183ade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=A4rz?= Date: Wed, 31 Jul 2024 10:47:05 +0200 Subject: [PATCH 2/3] - Fix ArrayDeepCompare to break when array is too deep - Add Handling of JSONException in JsonContext --- src/Context/JsonContext.php | 20 ++++++++++---------- src/Helper/ArrayDeepCompare.php | 4 ++-- tests/Helper/ArrayDeepCompareTest.php | 1 + 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index 13f0d85..da612f6 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -15,10 +15,11 @@ class JsonContext implements Context use RequestTrait; public function __construct( - protected KernelInterface $kernel, + protected KernelInterface $kernel, protected ArrayDeepCompare $arrayComp, - protected State $state, - ) { + protected State $state, + ) + { } #[When('I make a :method request with json data to :url')] @@ -29,10 +30,10 @@ public function iMakeARequestWithJsonDataTo(string $method, string $url, ?PyStri $rawData = $data->getRaw(); $server['CONTENT_TYPE'] = 'application/json'; if (str_contains($rawData, "\n\n")) { - [$headers,$rawData] = explode("\n\n", $rawData); + [$headers, $rawData] = explode("\n\n", $rawData); foreach (explode("\n", $headers) as $headerRow) { - [$headerKey,$headerValue] = explode(':', $headerRow, 2); - $server['HTTP_'.strtoupper($headerKey)] = trim($headerValue); + [$headerKey, $headerValue] = explode(':', $headerRow, 2); + $server['HTTP_' . strtoupper($headerKey)] = trim($headerValue); } } } @@ -84,10 +85,9 @@ public function theResponseJsonDoesNotContain(PyStringNode $string): void { try { $this->theResponseJsonContains($string); - } catch (\DomainException|\JsonException $e) { - if ($e instanceof \JsonException) { - throw new \JsonException('JSON Syntax Error: ' . $e->getMessage()); - } + } catch (\JsonException $e) { + throw new \JsonException('JSON Syntax Error: ' . $e->getMessage()); + } catch (\DomainException) { return; } throw new \DomainException('the response json contains exact this data'); diff --git a/src/Helper/ArrayDeepCompare.php b/src/Helper/ArrayDeepCompare.php index 6bb3fa2..23c7f4a 100644 --- a/src/Helper/ArrayDeepCompare.php +++ b/src/Helper/ArrayDeepCompare.php @@ -66,9 +66,9 @@ protected function hasDiff($a, $b, string $path = '', bool $reverseCheck = true) '%s: (%s) %s != (%s) %s', $path, \gettype($a), - (string)($a ?? ''), + $a ?? '', \gettype($b), - (string)($b ?? '') + $b ?? '' ); return true; diff --git a/tests/Helper/ArrayDeepCompareTest.php b/tests/Helper/ArrayDeepCompareTest.php index 91ba435..ef8b2f0 100644 --- a/tests/Helper/ArrayDeepCompareTest.php +++ b/tests/Helper/ArrayDeepCompareTest.php @@ -32,6 +32,7 @@ public function arrayContainsProvider() [['a' => [['b' => 'c', 'd' => 'e']]], /* contains */ ['a' => [['d' => 'e']]]], [['apple','banana'], /* contains */ ['banana']], [['a' => ['apple','banana']], /* contains */ ['a' => []]], + [['a' => ['apple' => [['a' => 'b']]]], /* contains */ ['apple' => [['a' => 'b']]]], ]; } From 4048662e5a59c72fa7ad514459d79781579dfd8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=A4rz?= Date: Wed, 31 Jul 2024 14:35:03 +0200 Subject: [PATCH 3/3] - Fix ArrayDeepCompare to break when array is too deep - Add Handling of JSONException in JsonContext --- src/Context/JsonContext.php | 3 +-- src/Helper/ArrayDeepCompare.php | 2 +- tests/Helper/ArrayDeepCompareTest.php | 21 +++++++++++---------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index da612f6..b7a8fef 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -18,8 +18,7 @@ public function __construct( protected KernelInterface $kernel, protected ArrayDeepCompare $arrayComp, protected State $state, - ) - { + ) { } #[When('I make a :method request with json data to :url')] diff --git a/src/Helper/ArrayDeepCompare.php b/src/Helper/ArrayDeepCompare.php index 23c7f4a..9817955 100644 --- a/src/Helper/ArrayDeepCompare.php +++ b/src/Helper/ArrayDeepCompare.php @@ -131,4 +131,4 @@ protected function hasDiff($a, $b, string $path = '', bool $reverseCheck = true) return false; } -} \ No newline at end of file +} diff --git a/tests/Helper/ArrayDeepCompareTest.php b/tests/Helper/ArrayDeepCompareTest.php index ef8b2f0..ed96860 100644 --- a/tests/Helper/ArrayDeepCompareTest.php +++ b/tests/Helper/ArrayDeepCompareTest.php @@ -30,9 +30,9 @@ public function arrayContainsProvider() [['a' => 'b', 'c' => 'd'], /* contains */ ['c' => 'd']], [['a' => [['b' => 'c', 'd' => 'e']]], /* contains */ ['a' => [['b' => 'c']]]], [['a' => [['b' => 'c', 'd' => 'e']]], /* contains */ ['a' => [['d' => 'e']]]], - [['apple','banana'], /* contains */ ['banana']], - [['a' => ['apple','banana']], /* contains */ ['a' => []]], - [['a' => ['apple' => [['a' => 'b']]]], /* contains */ ['apple' => [['a' => 'b']]]], + [['apple', 'banana'], /* contains */ ['banana']], + [['a' => ['apple', 'banana']], /* contains */ ['a' => []]], + [['result' => ['apples' => [['appleId' => '10'], ['appleId' => '20']]]], /* contains */ ['result' => ['apples' => [['appleId' => '10']]]]], ]; } @@ -49,11 +49,12 @@ public function arrayContainsNotProvider() { return [ [['a', 'b'], /* doesn't contain */ ['c'], /* because */ '0: c Missing'], - [['a' => 'b', 'c' => 'd'], /* doesn't contain */ ['a' => 'c'], /* because */ 'a: (string) c != (string) b'], - [['a' => 'b', 'c' => 'd'], /* doesn't contain */ ['c' => 'b'], /* because */ 'c: (string) b != (string) d'], - [['a' => [['b' => 'c', 'd' => 'e']]], /* doesn't contain */ ['a' => ['b' => 3]], /* because */ 'a.b: Missing'], - [['a' => [['b' => 'c', 'd' => 'e']]], /* doesn't contain */ ['a' => ['d' => 'c']], /* because */ 'a.d: Missing'], - [['a' => ['apple','banana']], /* doesn't contain */ ['a' => 'apple'], /* because */ 'a: != '], + [['a' => 'b', 'c' => 'd'], /* doesn't contain */ ['a' => 'c'], /* because */ 'a: (string) c != (string) b'], + [['a' => 'b', 'c' => 'd'], /* doesn't contain */ ['c' => 'b'], /* because */ 'c: (string) b != (string) d'], + [['a' => [['b' => 'c', 'd' => 'e']]], /* doesn't contain */ ['a' => ['b' => 3]], /* because */ 'a.b: Missing'], + [['a' => [['b' => 'c', 'd' => 'e']]], /* doesn't contain */ ['a' => ['d' => 'c']], /* because */ 'a.d: Missing'], + [['a' => ['apple', 'banana']], /* doesn't contain */ ['a' => 'apple'], /* because */ 'a: != '], + [['result' => ['apples' => [['appleId' => '10'], ['appleId' => '20']]]], /* contains */ ['result' => ['apples' => [['appleId' => '30']]]], 'result.apples.0: {"appleId":"30"} Missing'], ]; } @@ -67,7 +68,7 @@ public function arrayEqualsProvider() { return [ [['a'], /* equals */ ['a']], - [['a', 'b'], /* equals */ ['b','a']], + [['a', 'b'], /* equals */ ['b', 'a']], ]; } @@ -85,7 +86,7 @@ public function arrayEqualsNotProvider() return [ [['a', 'b'], /* doesn't equal */ ['a'], /* because */ '1: b Missing'], [['a', 'b'], /* doesn't equal */ ['b'], /* because */ '0: a Missing'], - [['a'], /* doesn't equal */ ['a','b'], /* because */ 'b: Extra'], + [['a'], /* doesn't equal */ ['a', 'b'], /* because */ 'b: Extra'], ]; }