Skip to content

Commit fc18bba

Browse files
committed
add tests
1 parent 4ab10a8 commit fc18bba

7 files changed

Lines changed: 68 additions & 89 deletions

File tree

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
}
1111
],
1212
"require-dev": {
13+
"devlop/phpunit-exception-assertions": "^1.0",
1314
"phpunit/phpunit": "^9.5",
1415
"symfony/var-dumper": "^5.2",
1516
"symfony/polyfill-php80": "^1.22",
@@ -25,6 +26,8 @@
2526
"Devlop\\Json\\Tests\\": "tests/"
2627
}
2728
},
29+
"minimum-stability": "dev",
30+
"prefer-stable": true,
2831
"extra": {
2932
"branch-alias": {
3033
"dev-master": "1.x-dev"

src/Json.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private function __construct()
2222

2323
private static function assertCanEncode($value) : void
2424
{
25-
if (! is_array($value) && ! is_object($value)) {
25+
if (! \is_array($value) && ! \is_object($value)) {
2626
throw new JsonException(
2727
'A value of a type that cannot be encoded was given, only arrays and objects are supported.',
2828
\JSON_ERROR_UNSUPPORTED_TYPE,
@@ -32,9 +32,9 @@ private static function assertCanEncode($value) : void
3232

3333
private static function assertCanDecode($value) : void
3434
{
35-
$ends = mb_substr($value, 0, 1) . mb_substr($value, -1);
35+
$ends = \mb_substr($value, 0, 1) . \mb_substr($value, -1);
3636

37-
if (! in_array($ends, ['[]', '{}'], true)) {
37+
if (! \in_array($ends, ['[]', '{}'], true)) {
3838
throw new JsonException(
3939
'Invalid or malformed JSON, only arrays and objects are supported.',
4040
\JSON_ERROR_UNSUPPORTED_TYPE,

tests/Assertions/AssertException.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

tests/JsonDecodeAssocTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,4 @@ public function test_decode_assoc_decodes_key_value_object_as_assoc_array() : vo
3939

4040
$this->assertEquals($expectedOutput, $output);
4141
}
42-
43-
public function test_pretty_internally_invokes_encode_method() : void
44-
{
45-
// learn mocking
46-
}
4742
}

tests/JsonDecodeTest.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
namespace Devlop\Json\Tests;
66

77
use Devlop\Json\Json;
8-
use Devlop\Json\Tests\Assertions\AssertException;
8+
use Devlop\PHPUnit\ExceptionAssertions;
9+
use JsonException;
910
use PHPUnit\Framework\TestCase;
1011

1112
final class JsonDecodeTest extends TestCase
1213
{
13-
use AssertException;
14+
use ExceptionAssertions;
1415

1516
public function test_decodes_integer_values_array() : void
1617
{
@@ -61,12 +62,23 @@ public function test_decodes_key_value_object_as_object() : void
6162

6263
public function test_decode_flags_argument() : void
6364
{
64-
//
65+
$output = Json::decode(
66+
'{"number":1234567890123456789012345678901234567890}',
67+
\JSON_BIGINT_AS_STRING
68+
);
69+
70+
$expectedOutput = (object) [
71+
'number' => '1234567890123456789012345678901234567890',
72+
];
73+
74+
$this->assertEquals($expectedOutput, $output);
6575
}
6676

6777
public function test_decode_depth_argument() : void
6878
{
69-
//
79+
$this->assertExceptionThrown(JsonException::class, function () {
80+
Json::decode('{"first":["second"]}', Json::NO_FLAGS, 1);
81+
});
7082
}
7183

7284
public function test_does_not_decode_scalar_or_null() : void
@@ -82,14 +94,9 @@ public function test_does_not_decode_scalar_or_null() : void
8294
];
8395

8496
foreach ($arguments as $argument) {
85-
$this->assertException(\JsonException::class, function () use ($argument) {
97+
$this->assertExceptionThrown(JsonException::class, function () use ($argument) {
8698
Json::decodeAssoc($argument);
8799
});
88100
}
89101
}
90-
91-
public function test_pretty_internally_invokes_encode_method() : void
92-
{
93-
// learn mocking
94-
}
95102
}

tests/JsonEncodeTest.php

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,33 @@
55
namespace Devlop\Json\Tests;
66

77
use Devlop\Json\Json;
8-
use Devlop\Json\Tests\Assertions\AssertException;
8+
use Devlop\PHPUnit\ExceptionAssertions;
9+
use JsonException;
910
use PHPUnit\Framework\TestCase;
11+
use Throwable;
1012

1113
final class JsonEncodeTest extends TestCase
1214
{
13-
use AssertException;
15+
use ExceptionAssertions;
16+
17+
public function test_encode_throws_exception_on_error()
18+
{
19+
$recursion = [
20+
&$recursion,
21+
];
22+
23+
$arguments = [
24+
$recursion,
25+
INF,
26+
NAN,
27+
];
28+
29+
foreach ($arguments as $argument) {
30+
$this->assertExceptionThrown(JsonException::class, function () use ($argument) {
31+
Json::encode($argument);
32+
});
33+
}
34+
}
1435

1536
public function test_encodes_integer_values_array() : void
1637
{
@@ -128,23 +149,24 @@ public function test_encode_flags_argument() : void
128149

129150
public function test_encode_depth_argument() : void
130151
{
131-
$this->expectException(\JsonException::class);
132-
133-
try {
134-
$output = Json::encode([
135-
'first' => 'one',
136-
'second' => 'two',
137-
'third' => [
138-
'three',
139-
'four',
140-
'five',
141-
],
142-
], Json::NO_FLAGS, 1);
143-
} catch (\JsonException $e) {
144-
$this->assertEquals('Maximum stack depth exceeded', $e->getMessage());
145-
146-
throw $e;
147-
}
152+
$argument = [
153+
'first' => 'one',
154+
'second' => 'two',
155+
'third' => [
156+
'three',
157+
'four',
158+
'five',
159+
],
160+
];
161+
162+
$this->assertExceptionThrown(JsonException::class, function () use ($argument) {
163+
Json::encode($argument, Json::NO_FLAGS, 1);
164+
}, function (Throwable $exception) : void {
165+
$this->assertEquals(
166+
'Maximum stack depth exceeded',
167+
$exception->getMessage(),
168+
);
169+
});
148170
}
149171

150172
public function test_does_not_encode_scalar_or_null_arguments() : void
@@ -159,7 +181,7 @@ public function test_does_not_encode_scalar_or_null_arguments() : void
159181
];
160182

161183
foreach ($arguments as $argument) {
162-
$this->assertException(\JsonException::class, function () use ($argument) {
184+
$this->assertExceptionThrown(JsonException::class, function () use ($argument) {
163185
Json::pretty($argument);
164186
});
165187
}
@@ -173,9 +195,9 @@ public function test_encodes_array_and_objects() : void
173195
];
174196

175197
foreach ($arguments as $argument) {
176-
// $this->assertException(\JsonException::class, function () use ($argument) {
177-
// Json::pretty($argument);
178-
// });
198+
$this->assertExceptionNotThrown(JsonException::class, function () use ($argument) {
199+
Json::pretty($argument);
200+
});
179201
}
180202
}
181203
}

tests/JsonPrettyTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
namespace Devlop\Json\Tests;
66

77
use Devlop\Json\Json;
8-
use Devlop\Json\Tests\Assertions\AssertException;
98
use PHPUnit\Framework\TestCase;
109

1110
final class JsonPrettyTest extends TestCase
1211
{
13-
use AssertException;
14-
1512
public function test_pretty_produces_formatted_output() : void
1613
{
1714
$output = Json::pretty([
@@ -30,9 +27,4 @@ public function test_pretty_produces_formatted_output() : void
3027

3128
$this->assertEquals($expectedOutput, $output);
3229
}
33-
34-
public function test_pretty_internally_invokes_encode_method() : void
35-
{
36-
// learn mocking
37-
}
3830
}

0 commit comments

Comments
 (0)