Passed
Push — master ( fedf3c...78592b )
by Alexander
11:31
created

JsonTest::testEncodeDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
namespace Yiisoft\Json\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use Yiisoft\Json\Json;
6
7
class JsonTest extends TestCase
8
{
9
    public function testEncodeBasic(): void
10
    {
11
        $data = '1';
12
        $this->assertSame('"1"', Json::encode($data));
13
    }
14
15
    public function testEncodeSimpleArray(): void
16
    {
17
        $data = [1, 2];
18
        $this->assertSame('[1,2]', Json::encode($data));
19
        $data = ['a' => 1, 'b' => 2];
20
        $this->assertSame('{"a":1,"b":2}', Json::encode($data));
21
    }
22
23
    public function testEncodeSimpleObject(): void
24
    {
25
        $data = new \stdClass();
26
        $data->a = 1;
27
        $data->b = 2;
28
        $this->assertSame('{"a":1,"b":2}', Json::encode($data));
29
    }
30
31
    public function testEncodeEmpty(): void
32
    {
33
        $data = [];
34
        $this->assertSame('[]', Json::encode($data));
35
        $data = new \stdClass();
36
        $this->assertSame('{}', Json::encode($data));
37
    }
38
39
    /**
40
     * @see https://github.com/yiisoft/yii2/issues/957
41
     */
42
    public function testEncodeNullObject(): void
43
    {
44
        $data = (object)null;
45
        $this->assertSame('{}', Json::encode($data));
46
    }
47
48
49
    public function testEncodeJsonSerializable(): void
50
    {
51
        $data = new Post(42, 'json serializable');
52
        $this->assertSame('{"id":42,"title":"json serializable"}', Json::encode($data));
53
    }
54
55
    /**
56
     * @see https://github.com/yiisoft/yii2/issues/12043
57
     */
58
    public function testEncodeWithSerializableReturningEmptyArray(): void
59
    {
60
        $data = new Data([]);
61
        $this->assertSame('[]', Json::encode($data));
62
    }
63
64
    /**
65
     * @see https://github.com/yiisoft/yii2/issues/12043
66
     */
67
    public function testEncodeWithSerializableReturningEmptyObject(): void
68
    {
69
        $data = new Data((object)null);
70
        $this->assertSame('{}', Json::encode($data));
71
    }
72
73
    public function testsHtmlEncodeEscapesCharacters(): void
74
    {
75
        $data = '&<>"\'/';
76
        $this->assertSame('"\u0026\u003C\u003E\u0022\u0027\/"', Json::htmlEncode($data));
77
    }
78
79
    public function testHtmlEncodeBasic(): void
80
    {
81
        $data = '1';
82
        $this->assertSame('"1"', Json::htmlEncode($data));
83
    }
84
85
    public function testHtmlEncodeSimpleArray(): void
86
    {
87
        $data = [1, 2];
88
        $this->assertSame('[1,2]', Json::htmlEncode($data));
89
        $data = ['a' => 1, 'b' => 2];
90
        $this->assertSame('{"a":1,"b":2}', Json::htmlEncode($data));
91
    }
92
93
    public function testHtmlEncodeSimpleObject(): void
94
    {
95
        $data = new \stdClass();
96
        $data->a = 1;
97
        $data->b = 2;
98
        $this->assertSame('{"a":1,"b":2}', Json::htmlEncode($data));
99
    }
100
101
    /**
102
     * @see https://github.com/yiisoft/yii2/issues/957
103
     */
104
    public function testHtmlEncodeNullObject(): void
105
    {
106
        $data = (object) null;
107
        $this->assertSame('{}', Json::htmlEncode($data));
108
    }
109
110
    public function testHtmlEncodeJsonSerializable(): void
111
    {
112
        $data = new Post(42, 'json serializable');
113
        $this->assertSame('{"id":42,"title":"json serializable"}', Json::htmlEncode($data));
114
    }
115
116
    /**
117
     * @see https://github.com/yiisoft/yii2/issues/10278
118
     */
119
    public function testsHtmlEncodeXmlDocument(): void
120
    {
121
        $xml = '<?xml version="1.0" encoding="UTF-8"?>
122
        <file>
123
          <apiKey>ieu2iqw4o</apiKey>
124
          <methodProperties>
125
            <FindByString>Kiev</FindByString>
126
          </methodProperties>
127
        </file>';
128
129
        $document = simplexml_load_string($xml);
130
        $this->assertSame('{"apiKey":"ieu2iqw4o","methodProperties":{"FindByString":"Kiev"}}', Json::encode($document));
131
    }
132
133
    public function testsHtmlEncodeSplStack(): void
134
    {
135
        $postsStack = new \SplStack();
136
        $postsStack->push(new Post(915, 'record1'));
137
        $postsStack->push(new Post(456, 'record2'));
138
139
        $this->assertSame('{"1":{"id":456,"title":"record2"},"0":{"id":915,"title":"record1"}}', Json::encode($postsStack));
140
    }
141
142
    public function testDecodeEmptyValue(): void
143
    {
144
        $json = '';
145
        $actual = Json::decode($json);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $actual is correct as Yiisoft\Json\Json::decode($json) targeting Yiisoft\Json\Json::decode() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
146
        $this->assertNull($actual);
147
    }
148
149
    public function testDecodeBasic(): void
150
    {
151
        $json = '"1"';
152
        $this->assertSame('1', Json::decode($json));
153
    }
154
155
    public function testsDecodeArray(): void
156
    {
157
        $json = '{"a":1,"b":2}';
158
        $this->assertSame(['a' => 1, 'b' => 2], Json::decode($json));
159
    }
160
161
    public function testsDecodeInvalidJsonThrowsException(): void
162
    {
163
        $json = '{"a":1,"b":2';
164
        $this->expectException(\JsonException::class);
165
        Json::decode($json);
166
    }
167
168
    public function testHandleJsonError(): void
169
    {
170
        $jsonClass = new \Reflectionclass(Json::class);
171
        $errors = $jsonClass->getConstant('ERRORS');
172
173
        // Basic syntax error
174
        try {
175
            $json = "{'a': '1'}";
176
            Json::decode($json);
177
        } catch (\JsonException $e) {
178
            $this->assertSame($errors['JSON_ERROR_SYNTAX'], $e->getMessage());
179
        }
180
181
        // Unsupported type since PHP 5.5
182
        try {
183
            $fp = fopen('php://stdin', 'r');
184
            $data = ['a' => $fp];
185
            Json::encode($data);
186
            fclose($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

186
            fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
187
        } catch (\JsonException $e) {
188
            $this->assertSame($errors['JSON_ERROR_UNSUPPORTED_TYPE'], $e->getMessage());
189
        }
190
    }
191
192
    /**
193
     * @link https://github.com/yiisoft/yii2/issues/17760
194
     */
195
    public function testEncodeDateTime()
196
    {
197
        $input = new \DateTime('October 12, 2014');
198
        $output = Json::encode($input);
199
        $this->assertEquals('{"date":"2014-10-12 00:00:00.000000","timezone_type":3,"timezone":"UTC"}', $output);
200
    }
201
}
202