-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathRegexListenerTest.php
368 lines (306 loc) · 11 KB
/
RegexListenerTest.php
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
declare(strict_types=1);
namespace JsonStreamingParser\Test;
use JsonStreamingParser\Listener\RegexListener;
use JsonStreamingParser\Parser;
use PHPUnit\Framework\TestCase;
error_reporting(E_ALL);
ini_set('display_errors', '1');
class RegexListenerTest extends TestCase
{
/**
* Test reading an attribute as well as a nested object
* with a regex symbol.
*/
public function testBasicRead(): void
{
$filename = __DIR__.'/data/example.json';
$testJSON = json_decode(file_get_contents($filename), true);
$tssnCall = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock()
;
$tssnCall->expects($this->once())
->method('__invoke')
->with($testJSON[0]['totally sweet scientific notation'])
;
$nested_objectCall = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock()
;
$nested_objectCall->expects($this->once())
->method('__invoke')
->with($testJSON[1]['nested object']['nested object?'])
;
$listener = new RegexListener(['/0/totally sweet scientific notation' => $tssnCall,
"/1/nested object/nested object\?" => $nested_objectCall, ]);
$parser = new Parser(fopen($filename, 'r'), $listener);
$parser->parse();
}
/**
* Test reading first and last elements in JSON.
*/
public function testReadFirstAndLast(): void
{
$filename = __DIR__.'/data/plain.json';
$testJSON = json_decode(file_get_contents($filename), true);
$firstCall = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock()
;
$firstCall->expects($this->once())
->method('__invoke')
->with($testJSON['key 1'])
;
$lastCall = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock()
;
$lastCall->expects($this->once())
->method('__invoke')
->with($testJSON['key 3'])
;
$listener = new RegexListener(['/key 1' => $firstCall,
'/key 3' => $lastCall, ]);
$parser = new Parser(fopen($filename, 'r'), $listener);
$parser->parse();
}
/**
* Test reading a nested array of simple data types (integers).
*/
public function testBasicReadArray(): void
{
$filename = __DIR__.'/data/example.json';
$testJSON = json_decode(file_get_contents($filename), true);
$arrayData = $testJSON[1]['nested object']['nested array'];
$stream = fopen($filename, 'r');
$this->assertNotNull($stream);
$arrayCall = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock()
;
$arrayList = array_map(function ($data) { return [$data]; },
$arrayData);
$arrayCall->expects($this->exactly(\count($arrayData)))
->method('__invoke')
->withConsecutive(...$arrayList)
;
$listener = new RegexListener(["/1/nested object/nested array/\d*" => $arrayCall]);
$parser = new Parser(fopen($filename, 'r'), $listener);
$parser->parse();
}
/**
* Test reading a nested array with objects.
*/
public function testBasicReadArrayElement(): void
{
$filename = __DIR__.'/data/dateRanges.json';
$testJSON = json_decode(file_get_contents($filename), true);
$arrayData = array_column($testJSON['data'][0]['rows'], 'startDate');
$arrayData2 = array_column($testJSON['data'][0]['rows'], 'endDate');
$arrayData3 = $testJSON['anotherPlace'];
$stream = fopen($filename, 'r');
$this->assertNotNull($stream);
$arrayCall = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock()
;
$arrayList = array_map(function ($data) { return [$data]; },
$arrayData);
$arrayCall->expects($this->exactly(\count($arrayData)))
->method('__invoke')
->withConsecutive(...$arrayList)
;
$arrayCall2 = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock()
;
$arrayList2 = array_map(function ($data) { return [$data]; },
$arrayData2);
$arrayCall2->expects($this->exactly(\count($arrayData2)))
->method('__invoke')
->withConsecutive(...$arrayList2)
;
$nested_objectCall = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock()
;
$nested_objectCall->expects($this->once())
->method('__invoke')
->with($arrayData3)
;
$listener = new RegexListener(["/data/0/rows/\d*/startDate" => $arrayCall,
"/data/0/rows/\d*/endDate" => $arrayCall2,
'/anotherPlace' => $nested_objectCall,
]);
$parser = new Parser(fopen($filename, 'r'), $listener);
$parser->parse();
}
/**
* Test reading root level nested objects.
*/
public function testBasicReadArrayObjects(): void
{
$filename = __DIR__.'/data/example.json';
$testJSON = json_decode(file_get_contents($filename), true);
$arrayCall = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock()
;
$arrayList = array_map(function ($data) { return [$data]; },
$testJSON);
$arrayCall->expects($this->exactly(\count($testJSON)))
->method('__invoke')
->withConsecutive(...$arrayList)
;
$listener = new RegexListener(["/\d*" => $arrayCall]);
$parser = new Parser(fopen($filename, 'r'), $listener);
$parser->parse();
}
/**
* Test large file list of ID's.
*/
public function testBasicReadArrayIDs(): void
{
$filename = __DIR__.'/data/ratherBig.json';
$testJSON = json_decode(file_get_contents($filename), true);
$arrayCall = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock()
;
$arrayList = array_map(function ($data) { return [$data]; },
array_column($testJSON['rows'], 'id'));
$arrayCall->expects($this->exactly(\count($testJSON['rows'])))
->method('__invoke')
->withConsecutive(...$arrayList)
;
$listener = new RegexListener(["/rows/\d*/id" => $arrayCall]);
$parser = new Parser(fopen($filename, 'r'), $listener);
$parser->parse();
}
/**
* Testing the capture group in the JSON path is passed to callback.
* As the example file has an 'endDate' element in various elements,
* the path of (.*)/endDate should result in both the element
* value as well as the base element name being passed into the callback.
*/
public function testCaptureGroup(): void
{
$filename = __DIR__.'/data/dateRanges.json';
$arrayCall = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock()
;
$arrayCall->expects($this->exactly(3))
->method('__invoke')
->withConsecutive(['2013-10-25', '/data/0/rows/0'],
['2013-10-27', '/data/0/rows/1'],
['2013-11-10', '/anotherPlace'])
;
$listener = new RegexListener(['(.*)/endDate' => $arrayCall]);
$parser = new Parser(fopen($filename, 'r'), $listener);
$parser->parse();
}
/**
* Testing ability to stop load.
*/
public function testBasicReadStop(): void
{
$filename = __DIR__.'/data/example.json';
$testJSON = json_decode(file_get_contents($filename), true);
$arrayCall = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock()
;
$arrayCall->expects($this->once())
->method('__invoke')
->with($testJSON[0]['name'])
;
$notCall = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock()
;
$notCall->expects($this->never())
->method('__invoke')
->with($testJSON[0]['zero character'])
;
$listener = new RegexListener();
$parser = new Parser(fopen($filename, 'r'), $listener);
$listener->setMatch(['/0/name' => $arrayCall,
'/0/integer' => function ($data) use ($parser): void {
$parser->stop();
},
'/0/zero character' => $notCall,
]);
$parser->parse();
}
/**
* Test reading a corrupt file.
* Extracts the type element of each of the features.
*/
public function testCorruptFile(): void
{
$filename = __DIR__.'/data/example.corrupted.json';
$arrayCall = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock()
;
$arrayCall->expects($this->exactly(2))
->method('__invoke')
->withConsecutive(['Point'], ['Polygon'])
;
$listener = new RegexListener(["/features/\d*/geometry/type" => $arrayCall]);
$parser = new Parser(fopen($filename, 'r'), $listener);
$parser->parse();
}
/**
* Test reading a simple array of integers.
*/
public function testBasicArrayRead(): void
{
$filename = __DIR__.'/data/simpleArray.json';
$testJSON = json_decode(file_get_contents($filename), true);
$value1Call = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock()
;
$value1Call->expects($this->once())
->method('__invoke')
->with($testJSON[0])
;
$value2Call = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock()
;
$value2Call->expects($this->once())
->method('__invoke')
->with($testJSON[1])
;
$listener = new RegexListener(['/0' => $value1Call,
'/1' => $value2Call, ]);
$parser = new Parser(fopen($filename, 'r'), $listener);
$parser->parse();
}
/**
* Test reading a simple array of integers with capture group.
*/
public function testBasicArrayReadCapture(): void
{
$filename = __DIR__.'/data/simpleArray.json';
$valueCall = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock()
;
$valueCall->expects($this->exactly(3))
->method('__invoke')
->withConsecutive(
['1', '/0'],
['2', '/1'],
['-1', '/2']
)
;
$listener = new RegexListener(["(/\d*)" => $valueCall]);
$parser = new Parser(fopen($filename, 'r'), $listener);
$parser->parse();
}
}