-
Notifications
You must be signed in to change notification settings - Fork 431
Expand file tree
/
Copy pathJSONTest.php
More file actions
104 lines (84 loc) · 2.73 KB
/
Copy pathJSONTest.php
File metadata and controls
104 lines (84 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace Enqueue\Tests\Util;
use Enqueue\Tests\Util\Fixtures\JsonSerializableClass;
use Enqueue\Tests\Util\Fixtures\SimpleClass;
use Enqueue\Util\JSON;
use PHPUnit\Framework\TestCase;
class JSONTest extends TestCase
{
public function testShouldDecodeString()
{
$this->assertSame(['foo' => 'fooVal'], JSON::decode('{"foo": "fooVal"}'));
}
public function testThrowIfMalformedJson()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The malformed json given. ');
$this->assertSame(['foo' => 'fooVal'], JSON::decode('{]'));
}
public function nonStringDataProvider()
{
$resource = fopen('php://memory', 'r');
fclose($resource);
return [
[null],
[true],
[false],
[new \stdClass()],
[123],
[123.45],
[$resource],
];
}
/**
* @dataProvider nonStringDataProvider
*/
public function testShouldThrowExceptionIfInputIsNotString($value)
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Accept only string argument but got:');
$this->assertSame(0, JSON::decode($value));
}
public function testShouldReturnNullIfInputStringIsEmpty()
{
$this->assertNull(JSON::decode(''));
}
public function testShouldEncodeArray()
{
$this->assertEquals('{"key":"value"}', JSON::encode(['key' => 'value']));
}
public function testShouldEncodeString()
{
$this->assertEquals('"string"', JSON::encode('string'));
}
public function testShouldEncodeNumeric()
{
$this->assertEquals('123.45', JSON::encode(123.45));
}
public function testShouldEncodeNull()
{
$this->assertEquals('null', JSON::encode(null));
}
public function testShouldEncodeObjectOfStdClass()
{
$obj = new \stdClass();
$obj->key = 'value';
$this->assertEquals('{"key":"value"}', JSON::encode($obj));
}
public function testShouldEncodeObjectOfSimpleClass()
{
$this->assertEquals('{"keyPublic":"public"}', JSON::encode(new SimpleClass()));
}
public function testShouldEncodeObjectOfJsonSerializableClass()
{
$this->assertEquals('{"key":"value"}', JSON::encode(new JsonSerializableClass()));
}
public function testThrowIfValueIsResource()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Could not encode value into json. Error 8 and message Type is not supported');
$resource = fopen('php://memory', 'r');
fclose($resource);
JSON::encode($resource);
}
}