Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix arraydeepcompare and json excecption #36

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/Context/JsonContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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');
Expand Down
14 changes: 11 additions & 3 deletions src/Helper/ArrayDeepCompare.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -106,15 +113,15 @@ 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;
}
}

// 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);

Expand All @@ -123,4 +130,5 @@ protected function hasDiff($a, $b, string $path = '', bool $reverseCheck = true)

return false;
}

}
20 changes: 11 additions & 9 deletions tests/Helper/ArrayDeepCompareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']]]]],
];
}

Expand All @@ -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: <string> != <array>'],
[['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: <string> != <array>'],
[['result' => ['apples' => [['appleId' => '10'], ['appleId' => '20']]]], /* contains */ ['result' => ['apples' => [['appleId' => '30']]]], 'result.apples.0: {"appleId":"30"} Missing'],
];
}

Expand All @@ -66,7 +68,7 @@ public function arrayEqualsProvider()
{
return [
[['a'], /* equals */ ['a']],
[['a', 'b'], /* equals */ ['b','a']],
[['a', 'b'], /* equals */ ['b', 'a']],
];
}

Expand All @@ -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'],
];
}

Expand Down