Skip to content

Commit d33134b

Browse files
committed
Implement tests
1 parent ea8b51b commit d33134b

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

.github/workflows/test.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
on: push
2+
name: Test
3+
jobs:
4+
test:
5+
name: Test
6+
runs-on: ubuntu-latest
7+
strategy:
8+
matrix:
9+
php: ['7.4', '8.0', '8.1']
10+
11+
steps:
12+
- uses: actions/checkout@master
13+
14+
- name: Setup PHP
15+
uses: shivammathur/setup-php@v2
16+
with:
17+
php-version: ${{ matrix.php }}
18+
coverage: none
19+
20+
- name: Install Dependencies
21+
run: composer install
22+
23+
- name: Test
24+
run: make tests

specs/json_decode.spec.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
use function Technically\Json\json_decode;
4+
5+
describe('json_decode', function () {
6+
$inputDatasets = [
7+
'null' => 'null',
8+
'false' => 'false',
9+
'true' => 'true',
10+
'zero' => '0',
11+
'negative' => '-1000',
12+
'PHP_INT_MAX' => '9223372036854775807',
13+
'float' => '24.5',
14+
'empty array' => '[]',
15+
'empty object' => '{}',
16+
'array' => '["technically/json"]',
17+
'object' => '{"name":"technically/json","type":"library"}',
18+
'nested' => '{"level 1":{"level 2":{"level 3":[1,2,3]}}}',
19+
];
20+
21+
$flagDatasets = [
22+
'JSON_FORCE_OBJECT' => JSON_FORCE_OBJECT,
23+
'JSON_UNESCAPED_SLASHES' => JSON_UNESCAPED_SLASHES,
24+
'JSON_UNESCAPED_UNICODE' => JSON_UNESCAPED_UNICODE,
25+
];
26+
27+
foreach ($inputDatasets as $inputLabel => $input) {
28+
it("should decode JSON same as native json_decode() ($inputLabel)", function () use ($input) {
29+
assert(json_decode($input) == \json_decode($input));
30+
});
31+
}
32+
33+
foreach ($flagDatasets as $flagsLabel => $flags) {
34+
foreach ($inputDatasets as $inputLabel => $input) {
35+
it("should support flags same as native json_decode() ($flagsLabel x $inputLabel)", function () use ($input, $flags) {
36+
assert(json_decode($input, $flags) == \json_decode($input, $flags));
37+
});
38+
}
39+
}
40+
41+
it('should throw on max depth limit', function () use ($inputDatasets) {
42+
try {
43+
json_decode($inputDatasets['nested'], 0, $depth = 2);
44+
} catch (JsonException $exception) {
45+
// passthru
46+
}
47+
48+
assert(isset($exception));
49+
assert($exception instanceof JsonException);
50+
});
51+
52+
it('should throw on malformed JSON', function () use ($inputDatasets) {
53+
try {
54+
json_decode('{[');
55+
} catch (JsonException $exception) {
56+
// passthru
57+
}
58+
59+
assert(isset($exception));
60+
assert($exception instanceof JsonException);
61+
});
62+
});

specs/json_encode.spec.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
use function Technically\Json\json_encode;
4+
5+
describe('json_encode', function () {
6+
$inputDatasets = [
7+
'null' => null,
8+
'false' => false,
9+
'true' => true,
10+
'zero' => 0,
11+
'negative' => -1000,
12+
'PHP_INT_MAX' => PHP_INT_MAX,
13+
'float' => 24.5,
14+
'empty array' => [],
15+
'empty object' => (object) [],
16+
'array' => ['technically/json'],
17+
'assoc array' => ['name' => 'technically/json', 'type' => 'library'],
18+
'object' => (object) ['name' => 'technically/json', 'type' => 'library'],
19+
'nested' => [
20+
'level 1' => [
21+
'level 2' => [
22+
'level 3' => [1, 2, 3],
23+
],
24+
],
25+
],
26+
];
27+
28+
$flagDatasets = [
29+
'JSON_FORCE_OBJECT' => JSON_FORCE_OBJECT,
30+
'JSON_UNESCAPED_SLASHES' => JSON_UNESCAPED_SLASHES,
31+
'JSON_UNESCAPED_UNICODE' => JSON_UNESCAPED_UNICODE,
32+
];
33+
34+
foreach ($inputDatasets as $inputLabel => $input) {
35+
it("should encode JSON same as native json_encode() ($inputLabel)", function () use ($input) {
36+
assert(json_encode($input) === \json_encode($input));
37+
});
38+
}
39+
40+
foreach ($flagDatasets as $flagsLabel => $flags) {
41+
foreach ($inputDatasets as $inputLabel => $input) {
42+
it("should support flags same as native json_encode() ($flagsLabel x $inputLabel)", function () use ($input, $flags) {
43+
assert(json_encode($input, $flags) === \json_encode($input, $flags));
44+
});
45+
}
46+
}
47+
48+
it('should throw on max depth limit', function () use ($inputDatasets) {
49+
try {
50+
json_encode($inputDatasets['nested'], 0, $max_depth = 2);
51+
} catch (JsonException $exception) {
52+
// passthru
53+
}
54+
55+
assert(isset($exception));
56+
assert($exception instanceof JsonException);
57+
});
58+
});

0 commit comments

Comments
 (0)