Completed
Pull Request — master (#3)
by Harry
05:31
created

JsonParserTest::parseJsonData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 177
Code Lines 81

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 177
rs 8.2857
cc 1
eloc 81
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 View Code Duplication
    public function testParse(JsonFormatInterface $format, $json, array $expected)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testParseFailures(JsonFormatInterface $format, $json, $exception, $regex = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
234
    {
235
        $parser = new JsonParser($format);
236
237
        static::expectException($exception);
238
        if ($regex) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $regex of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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