Issues (41)

tests/src/JsonTest.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Nip\Utility\Tests;
4
5
6
use Nip\Utility\Json;
7
use Nip\Utility\Tests\Fixtures\BaseClass;
8
use Nip\Utility\Tests\Fixtures\JsonModel;
9
use stdClass;
10
11
/**
12
 * Class JsonTest
13
 * @package Nip\Utility\Tests
14
 */
15
class JsonTest extends AbstractTest
16
{
17
18
    public function testEncode()
19
    {
20
        // Arrayable data encoding
21
        $dataArrayable = $this->getMockBuilder(BaseClass::class)->getMock();
22
        $dataArrayable->method('toArray')->willReturn([]);
23
        $actual = Json::encode($dataArrayable);
24
        static::assertSame('{}', $actual);
25
26
        // basic data encoding
27
        $data = '1';
28
        static::assertSame('"1"', Json::encode($data));
29
30
        // simple array encoding
31
        $data = [1, 2];
32
        static::assertSame('[1,2]', Json::encode($data));
33
        $data = ['a' => 1, 'b' => 2];
34
        static::assertSame('{"a":1,"b":2}', Json::encode($data));
35
36
        // simple object encoding
37
        $data = new stdClass();
38
        $data->a = 1;
39
        $data->b = 2;
40
        static::assertSame('{"a":1,"b":2}', Json::encode($data));
41
42
        // empty data encoding
43
        $data = [];
44
        static::assertSame('[]', Json::encode($data));
45
        $data = new stdClass();
46
        static::assertSame('{}', Json::encode($data));
47
48
        // https://github.com/yiisoft/yii2/issues/957
49
        $data = (object) null;
50
        static::assertSame('{}', Json::encode($data));
51
    }
52
53
    public function testHtmlEncode()
54
    {
55
        // HTML escaped chars
56
        $data = '&<>"\'/';
57
        static::assertSame('"\u0026\u003C\u003E\u0022\u0027\/"', Json::htmlEncode($data));
58
59
        // basic data encoding
60
        $data = '1';
61
        static::assertSame('"1"', Json::htmlEncode($data));
62
63
        // simple array encoding
64
        $data = [1, 2];
65
        static::assertSame('[1,2]', Json::htmlEncode($data));
66
        $data = ['a' => 1, 'b' => 2];
67
        static::assertSame('{"a":1,"b":2}', Json::htmlEncode($data));
68
69
        // simple object encoding
70
        $data = new stdClass();
71
        $data->a = 1;
72
        $data->b = 2;
73
        static::assertSame('{"a":1,"b":2}', Json::htmlEncode($data));
74
75
        // https://github.com/yiisoft/yii2/issues/957
76
        $data = (object) null;
77
        static::assertSame('{}', Json::htmlEncode($data));
78
79
        // JsonSerializable
80
        $data = new JsonModel();
81
        static::assertSame('{"json":"serializable"}', Json::htmlEncode($data));
82
83
        // https://github.com/yiisoft/yii2/issues/10278
84
        $xml = '<?xml version="1.0" encoding="UTF-8"?>
85
<file>
86
  <apiKey>ieu2iqw4o</apiKey>
87
  <methodProperties>
88
    <FindByString>Kiev</FindByString>
89
  </methodProperties>
90
</file>';
91
92
        $document = simplexml_load_string($xml);
93
        static::assertSame(
94
            '{"apiKey":"ieu2iqw4o","methodProperties":{"FindByString":"Kiev"}}',
95
            Json::encode($document)
96
        );
97
    }
98
99
    public function testDecode()
100
    {
101
        // empty value
102
        $json = '';
103
        $actual = Json::decode($json);
0 ignored issues
show
Are you sure the assignment to $actual is correct as Nip\Utility\Json::decode($json) targeting Nip\Utility\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...
104
        static::assertNull($actual);
105
106
        // basic data decoding
107
        $json = '"1"';
108
        static::assertSame('1', Json::decode($json));
109
110
        // array decoding
111
        $json = '{"a":1,"b":2}';
112
        static::assertSame(['a' => 1, 'b' => 2], Json::decode($json));
113
114
        // exception
115
        $json = '{"a":1,"b":2';
116
        $this->expectException(\InvalidArgumentException::class);
117
        Json::decode($json);
118
    }
119
120
    public function testDecodeInvalidParamException()
121
    {
122
        $this->expectExceptionMessage("Invalid JSON data.");
123
        $this->expectException(\InvalidArgumentException::class);
124
        /** @noinspection PhpParamsInspection */
125
        Json::decode([]);
0 ignored issues
show
array() of type array is incompatible with the type string expected by parameter $json of Nip\Utility\Json::decode(). ( Ignorable by Annotation )

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

125
        Json::decode(/** @scrutinizer ignore-type */ []);
Loading history...
126
    }
127
128
    public function testHandleJsonError()
129
    {
130
        // Basic syntax error
131
        try {
132
            $json = "{'a': '1'}";
133
            Json::decode($json);
134
        } catch (\InvalidArgumentException $e) {
135
            static::assertSame(Json::$jsonErrorMessages['JSON_ERROR_SYNTAX'], $e->getMessage());
136
        }
137
138
        // Unsupported type since PHP 5.5
139
        try {
140
            $fp = fopen('php://stdin', 'r');
141
            $data = ['a' => $fp];
142
            Json::encode($data);
143
            fclose($fp);
144
        } catch (\InvalidArgumentException $e) {
145
            if (PHP_VERSION_ID >= 50500) {
146
                static::assertSame(Json::$jsonErrorMessages['JSON_ERROR_UNSUPPORTED_TYPE'], $e->getMessage());
147
            } else {
148
                static::assertSame(Json::$jsonErrorMessages['JSON_ERROR_SYNTAX'], $e->getMessage());
149
            }
150
        }
151
    }
152
153
    /**
154
     * @link https://github.com/yiisoft/yii2/issues/17760
155
     */
156
    public function testEncodeDateTime()
157
    {
158
        $input = new \DateTime('October 12, 2014', new \DateTimeZone('UTC'));
159
        $output = Json::encode($input);
160
        static::assertEquals('{"date":"2014-10-12 00:00:00.000000","timezone_type":3,"timezone":"UTC"}', $output);
161
    }
162
}