Skip to content

Commit 3cc5ad6

Browse files
committed
Add test for StringJson exception handling on invalid JSON
- Introduced a unit test to validate `StringJson` constructor behavior when handling invalid JSON inputs. - Verified that the correct exception (`JsonStringTypeException`) is thrown, with appropriate message, code, and previous exception (`JsonException`).
1 parent bd92003 commit 3cc5ad6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

tests/Unit/String/StringJsonTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use PhpTypedValues\Exception\JsonStringTypeException;
56
use PhpTypedValues\String\StringJson;
67
use PhpTypedValues\Undefined\Alias\Undefined;
78

@@ -44,3 +45,21 @@
4445
->and($arr['x'])->toBe(10)
4546
->and($arr['y'])->toBe(20);
4647
});
48+
49+
it('constructor throws with code 0 and previous JsonException on invalid JSON', function (): void {
50+
$invalid = '{invalid}';
51+
52+
try {
53+
new StringJson($invalid);
54+
expect()->fail('Exception was not thrown');
55+
} catch (Throwable $e) {
56+
expect($e)
57+
->toBeInstanceOf(JsonStringTypeException::class)
58+
->and($e->getMessage())
59+
->toBe(\sprintf('String "%s" has no valid JSON value', $invalid))
60+
->and($e->getCode())
61+
->toBe(0)
62+
->and($e->getPrevious())
63+
->toBeInstanceOf(JsonException::class);
64+
}
65+
});

0 commit comments

Comments
 (0)