Skip to content

Commit 87cdc17

Browse files
authored
🐛 Fix for json path expecting an array (#41)
I believe simple assertions worked because the top level object was being cast into an array, but if any specific path returned an object you'd get a type error.
1 parent 553b28e commit 87cdc17

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/Constraint/JsonValueMatches.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function toString(): string
6262
protected function matches($other): bool
6363
{
6464
if (is_string($other)) {
65-
$other = json_decode($other);
65+
$other = json_decode($other, true);
6666
}
6767

6868
$result = (new JSONPath($other))->find($this->jsonPath);

tests/Functional/JsonValueMatchesTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ public function dataForJsonValueEquals()
4747
array($json, '$.products[*].name', 'Roggenbrot'),
4848
array($json, '$.products[0].name', 'Roggenbrot'),
4949
array($json, '$.products.*.name', 'Graubrot'),
50+
array($json, '$.owner', [
51+
'identifier' => '4321',
52+
'name' => 'Max Mustermann',
53+
]),
54+
array(json_encode($json), '$.owner', [
55+
'identifier' => '4321',
56+
'name' => 'Max Mustermann',
57+
]),
5058
];
5159
}
5260

@@ -64,6 +72,8 @@ public function dataForJsonValueEqualsCanFail()
6472
array($json, '$.owner.name', 'Horst Mustermann'),
6573
array($json, '$.products.*.name', 'Weißbrot'),
6674
array($json, '$.products[0].name', 'Graubrot'),
75+
array($json, '$.owner', []),
76+
array(json_encode($json), '$.owner', []),
6777
];
6878
}
6979

0 commit comments

Comments
 (0)