Skip to content

Commit

Permalink
Enhancement: Assert that JSON is consistently decoded to an object
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Dec 10, 2022
1 parent b3e8d07 commit 5aa53a9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@
<code>\Generator&lt;string, array{0: string}&gt;</code>
</MoreSpecificReturnType>
</file>
<file src="test/Unit/JsonTest.php">
<MixedAssignment occurrences="2">
<code>$decoded</code>
<code>$decoded</code>
</MixedAssignment>
</file>
</files>
20 changes: 19 additions & 1 deletion test/Unit/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ public function testFromStringReturnsJsonWhenValueIsValidJson(string $encoded):

self::assertSame($encoded, $json->toString());
self::assertSame($encoded, $json->encoded());
self::assertJsonStringEqualsJsonString($encoded, \json_encode($json->decoded()));

$decoded = \json_decode(
$encoded,
false,
512,
\JSON_THROW_ON_ERROR,
);

self::assertEquals($decoded, $json->decoded());
}

public function testFromFileThrowsWhenFileDoesNotExist(): void
Expand Down Expand Up @@ -90,5 +98,15 @@ public function testFromFileReturnsJsonWhenFileContainsValidJson(string $file):
$json = Json::fromFile($file);

self::assertStringEqualsFile($file, $json->toString());
self::assertStringEqualsFile($file, $json->encoded());

$decoded = \json_decode(
\file_get_contents($file),
false,
512,
\JSON_THROW_ON_ERROR,
);

self::assertEquals($decoded, $json->decoded());
}
}

0 comments on commit 5aa53a9

Please sign in to comment.