toBe(['enabled' => true, 'limits' => [1, 2.5, null]]); }); test('accepts a caller defined maximum depth', function () { $this->expectException(UnexpectedValueException::class); JsonValueGuard::assertValue([['leaf']], 1); }); test('decode enforces the caller defined maximum depth', function () { $this->expectException(UnexpectedValueException::class); JsonValueGuard::decode(nestedJson(2), 1); }); test('decode8 accepts eight nested arrays', function () { expect(JsonValueGuard::decode8(nestedJson(8)))->toBe(nestedValue(8)); }); test('decode8 rejects nine nested arrays', function () { $this->expectException(UnexpectedValueException::class); JsonValueGuard::decode8(nestedJson(9)); }); test('rejects objects', function () { $this->expectException(UnexpectedValueException::class); JsonValueGuard::assertValue(new stdClass()); }); test('accepts json empty objects', function () { expect(fn () => JsonValueGuard::assertValue(new JsonEmptyObject()))->not->toThrow(UnexpectedValueException::class); }); test('nullable strings preserve strings and null', function () { expect(JsonValueGuard::nullableString('value'))->toBe('value'); expect(JsonValueGuard::nullableString(null))->toBeNull(); }); test('string lists preserve sequential string values', function () { expect(JsonValueGuard::stringList(['first', 'second']))->toBe(['first', 'second']); }); test('string lists reject invalid shapes', function (mixed $value) { $this->expectException(UnexpectedValueException::class); JsonValueGuard::stringList($value); })->with([ 'associative array' => [['name' => 'value']], 'non-string item' => [['value', 1]], ]); test('booleans accept what the boolean validation rule accepts', function (mixed $value, bool $expected) { expect(JsonValueGuard::boolean($value))->toBe($expected); })->with([[true, true], [1, true], ['1', true], [false, false], [0, false], ['0', false]]); test('booleans reject anything else', function (mixed $value) { expect(fn (): bool => JsonValueGuard::boolean($value))->toThrow(UnexpectedValueException::class, 'Expected a boolean value.'); })->with([[null], ['true'], ['yes'], [2], [1.0], [[]]]); test('integers accept what the integer validation rule accepts', function (mixed $value, int $expected) { expect(JsonValueGuard::integer($value))->toBe($expected); })->with([[5, 5], ['5', 5], ['-5', -5], [1024.0, 1024]]); test('integers reject anything else', function (mixed $value) { expect(fn (): int => JsonValueGuard::integer($value))->toThrow(UnexpectedValueException::class, 'Expected an integer value.'); })->with([[null], [true], [1024.5], ['1024.0'], ['1e3'], [[]]]); test('rejects invalid json', function () { $this->expectException(JsonException::class); JsonValueGuard::decode('{'); }); function nestedJson(int $depth): string { return str_repeat('[', $depth).'"leaf"'.str_repeat(']', $depth); } function nestedValue(int $depth): array|string { $value = 'leaf'; for ($index = 0; $index < $depth; $index++) { $value = [$value]; } return $value; }