diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index c998b80..b7a8fef 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -15,9 +15,9 @@ class JsonContext implements Context use RequestTrait; public function __construct( - protected KernelInterface $kernel, + protected KernelInterface $kernel, protected ArrayDeepCompare $arrayComp, - protected State $state, + protected State $state, ) { } @@ -29,10 +29,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,7 +84,9 @@ public function theResponseJsonDoesNotContain(PyStringNode $string): void { try { $this->theResponseJsonContains($string); - } catch (\DomainException|\JsonException) { + } 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 de9545e..9817955 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), + $a ?? '', + \gettype($b), + $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; } + } diff --git a/tests/Helper/ArrayDeepCompareTest.php b/tests/Helper/ArrayDeepCompareTest.php index 91ba435..ed96860 100644 --- a/tests/Helper/ArrayDeepCompareTest.php +++ b/tests/Helper/ArrayDeepCompareTest.php @@ -30,8 +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' => []]], + [['apple', 'banana'], /* contains */ ['banana']], + [['a' => ['apple', 'banana']], /* contains */ ['a' => []]], + [['result' => ['apples' => [['appleId' => '10'], ['appleId' => '20']]]], /* contains */ ['result' => ['apples' => [['appleId' => '10']]]]], ]; } @@ -48,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'], ]; } @@ -66,7 +68,7 @@ public function arrayEqualsProvider() { return [ [['a'], /* equals */ ['a']], - [['a', 'b'], /* equals */ ['b','a']], + [['a', 'b'], /* equals */ ['b', 'a']], ]; } @@ -84,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'], ]; }