JsonParserTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 240
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 240
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testParse() 0 9 1
B parseJsonData() 0 177 1
A testParseFailures() 0 12 2
A parseFailuresData() 0 13 1
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Test\Unit\Format\Parser;
15
16
use Graze\DataFile\Format\JsonFormat;
17
use Graze\DataFile\Format\JsonFormatInterface;
18
use Graze\DataFile\Format\Parser\JsonParser;
19
use Graze\DataFile\Test\Helper\CreateStreamTrait;
20
use Graze\DataFile\Test\TestCase;
21
use RuntimeException;
22
23
class JsonParserTest extends TestCase
24
{
25
    use CreateStreamTrait;
26
27
    /**
28
     * @dataProvider parseJsonData
29
     *
30
     * @param JsonFormatInterface $format
31
     * @param string              $json
32
     * @param array               $expected
33
     */
34
    public function testParse(JsonFormatInterface $format, $json, array $expected)
35
    {
36
        $parser = new JsonParser($format);
37
38
        $iterator = $parser->parse($this->createStream($json));
39
        $actual = iterator_to_array($iterator);
40
41
        static::assertEquals($expected, $actual);
42
    }
43
44
    /**
45
     * @return array
46
     */
47
    public function parseJsonData()
48
    {
49
        return [
50
            [
51
                new JsonFormat(),
52
                <<<JSON
53
[
54
    {
55
        "name": "value",
56
        "number": 1.2,
57
        "bool": true,
58
        "null": null,
59
        "thing": {
60
            "hi": "there"
61
        },
62
        "array": [
63
            "a",
64
            "1"
65
        ]
66
    },
67
    {
68
        "other": "stuff"
69
    }
70
]
71
JSON
72
                ,
73
                [
74
                    (object) [
75
                        'name'   => 'value',
76
                        'number' => 1.2,
77
                        'bool'   => true,
78
                        'null'   => null,
79
                        'thing'  => (object) [
80
                            'hi' => 'there',
81
                        ],
82
                        'array'  => ['a', '1'],
83
                    ],
84
                    (object) [
85
                        'other' => 'stuff',
86
                    ],
87
                ],
88
            ],
89
            [
90
                new JsonFormat([
91
                    JsonFormat::OPTION_DECODE_ASSOC => true,
92
                ]),
93
                <<<JSON
94
[
95
    {
96
        "name": "value\\nstuff",
97
        "number": 1.2,
98
        "bool": true,
99
        "null": null,
100
        "thing": {
101
            "hi": "there"
102
        },
103
        "array": [
104
            "a",
105
            "1"
106
        ]
107
    },
108
    {
109
        "other": "stuff"
110
    }
111
]
112
JSON
113
                ,
114
                [
115
                    [
116
                        'name'   => "value\nstuff",
117
                        'number' => 1.2,
118
                        'bool'   => true,
119
                        'null'   => null,
120
                        'thing'  => [
121
                            'hi' => 'there',
122
                        ],
123
                        'array'  => ['a', '1'],
124
                    ],
125
                    [
126
                        'other' => 'stuff',
127
                    ],
128
                ],
129
            ],
130
            [
131
                new JsonFormat([
132
                    JsonFormat::OPTION_FILE_TYPE => JsonFormat::JSON_FILE_TYPE_EACH_LINE,
133
                ]),
134
                <<<JSON
135
{"name": "value\\nline","number": 1.2,"bool": true,"null": null,"thing": {"hi": "there"},"array": ["a","1"]}
136
{"other": "stuff"}
137
JSON
138
                ,
139
                [
140
                    (object) [
141
                        'name'   => "value\nline",
142
                        'number' => 1.2,
143
                        'bool'   => true,
144
                        'null'   => null,
145
                        'thing'  => (object) [
146
                            'hi' => 'there',
147
                        ],
148
                        'array'  => ['a', '1'],
149
                    ],
150
                    (object) [
151
                        'other' => 'stuff',
152
                    ],
153
                ],
154
            ],
155
            [
156
                new JsonFormat([
157
                    JsonFormat::OPTION_FILE_TYPE          => JsonFormat::JSON_FILE_TYPE_EACH_LINE,
158
                    JsonFormat::OPTION_IGNORE_BLANK_LINES => true,
159
                ]),
160
                <<<JSON
161
162
{"name": "value\\nline","number": 1.2,"bool": true,"null": null,"thing": {"hi": "there"},"array": ["a","1"]}
163
164
165
166
{"other": "stuff"}
167
168
JSON
169
                ,
170
                [
171
                    (object) [
172
                        'name'   => "value\nline",
173
                        'number' => 1.2,
174
                        'bool'   => true,
175
                        'null'   => null,
176
                        'thing'  => (object) [
177
                            'hi' => 'there',
178
                        ],
179
                        'array'  => ['a', '1'],
180
                    ],
181
                    (object) [
182
                        'other' => 'stuff',
183
                    ],
184
                ],
185
            ],
186
            [
187
                new JsonFormat([
188
                    JsonFormat::OPTION_FILE_TYPE          => JsonFormat::JSON_FILE_TYPE_EACH_LINE,
189
                    JsonFormat::OPTION_IGNORE_BLANK_LINES => false,
190
                ]),
191
                <<<JSON
192
193
{"name": "value\\nline","number": 1.2,"bool": true,"null": null,"thing": {"hi": "there"},"array": ["a","1"]}
194
195
196
197
{"other": "stuff"}
198
199
JSON
200
                ,
201
                [
202
                    null,
203
                    (object) [
204
                        'name'   => "value\nline",
205
                        'number' => 1.2,
206
                        'bool'   => true,
207
                        'null'   => null,
208
                        'thing'  => (object) [
209
                            'hi' => 'there',
210
                        ],
211
                        'array'  => ['a', '1'],
212
                    ],
213
                    null,
214
                    null,
215
                    null,
216
                    (object) [
217
                        'other' => 'stuff',
218
                    ],
219
                    null,
220
                ],
221
            ],
222
        ];
223
    }
224
225
    /**
226
     * @dataProvider parseFailuresData
227
     *
228
     * @param JsonFormatInterface $format
229
     * @param string              $json
230
     * @param string              $exception
231
     * @param string|null         $regex
232
     */
233
    public function testParseFailures(JsonFormatInterface $format, $json, $exception, $regex = null)
234
    {
235
        $parser = new JsonParser($format);
236
237
        static::expectException($exception);
238
        if (!is_null($regex)) {
239
            static::expectExceptionMessageRegExp($regex);
240
        }
241
        $iterator = $parser->parse($this->createStream($json));
242
243
        iterator_to_array($iterator);
244
    }
245
246
    /**
247
     * @return array
248
     */
249
    public function parseFailuresData()
250
    {
251
        return [
252
            [
253
                new JsonFormat([
254
                    JsonFormat::OPTION_FILE_TYPE => JsonFormat::JSON_FILE_TYPE_SINGLE_BLOCK,
255
                ]),
256
                '{"not","an","array"}',
257
                RuntimeException::class,
258
                "/Expecting a json array to parse, unknown format detected/",
259
            ],
260
        ];
261
    }
262
}
263