Passed
Push — master ( f71bba...a12491 )
by Maurício
01:50
created

ShapeFileTest::testMeasureShapeSaveLoad()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * phpMyAdmin ShapeFile library
4
 * <https://github.com/phpmyadmin/shapefile/>.
5
 *
6
 * Copyright 2006-2007 Ovidio <ovidio AT users.sourceforge.net>
7
 * Copyright 2016 - 2017 Michal Čihař <[email protected]>
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, you can download one from
20
 * https://www.gnu.org/copyleft/gpl.html.
21
 */
22
23
namespace ShapeFileTest;
24
25
use PhpMyAdmin\ShapeFile\ShapeFile;
26
use PhpMyAdmin\ShapeFile\ShapeRecord;
27
use PHPUnit\Framework\TestCase;
28
29
class ShapeFileTest extends TestCase
30
{
31
    /**
32
     * Tests loading of a file.
33
     *
34
     * @param string $filename Name of file
35
     * @param int    $records  Expected number of records
36
     * @param int    $parts    Expected number of parts in first record
37
     *
38
     *
39
     * @dataProvider provideFiles
40
     */
41
    public function testLoad($filename, $records, $parts)
42
    {
43
        $shp = new ShapeFile(1);
44
        $shp->loadFromFile($filename);
45
        $this->assertEquals('', $shp->lastError);
46
        $this->assertEquals($records, count($shp->records));
47
        if (! is_null($parts)) {
0 ignored issues
show
introduced by
The condition is_null($parts) is always false.
Loading history...
48
            $this->assertEquals($parts, count($shp->records[0]->SHPData['parts']));
49
        }
50
    }
51
52
    /**
53
     * Data provider for file loading tests.
54
     *
55
     * @return array
56
     */
57
    public function provideFiles()
58
    {
59
        return [
60
            [
61
                'data/capitals.*',
62
                652,
63
                null,
64
            ],
65
            [
66
                'data/mexico.*',
67
                32,
68
                3,
69
            ],
70
            [
71
                'data/Czech_Republic_AL2.*',
72
                1,
73
                1,
74
            ],
75
            [
76
                'data/w001n05f.*',
77
                16,
78
                1,
79
            ],
80
            [
81
                'data/bc_hospitals.*',
82
                44,
83
                null,
84
            ],
85
            [
86
                'data/multipoint.*',
87
                312,
88
                null,
89
            ],
90
        ];
91
    }
92
93
    /**
94
     * Test error handling in loader.
95
     *
96
     * @param string $filename name to load
97
     *
98
     *
99
     * @dataProvider provideErrorFiles
100
     */
101
    public function testLoadError($filename)
102
    {
103
        $shp = new ShapeFile(1);
104
        $shp->loadFromFile($filename);
105
        $this->assertNotEquals('', $shp->lastError);
106
    }
107
108
    /**
109
     * Data provider for file loading error tests.
110
     *
111
     * @return array
112
     */
113
    public function provideErrorFiles()
114
    {
115
        $result = [
116
            ['data/no-shp.*'],
117
            ['data/missing.*'],
118
            ['data/invalid-shp.*'],
119
        ];
120
121
        if (ShapeFile::supportsDbase()) {
122
            $result[] = ['data/no-dbf.*'];
123
            $result[] = ['data/invalid-dbf.*'];
124
        }
125
126
        return $result;
127
    }
128
129
    /**
130
     * Creates test data.
131
     */
132
    private function createTestData()
133
    {
134
        $shp = new ShapeFile(1);
135
136
        $record0 = new ShapeRecord(1);
137
        $record0->addPoint(['x' => 482131.764567, 'y' => 2143634.39608]);
138
139
        $record1 = new ShapeRecord(11);
140
        $record1->addPoint(['x' => 472131.764567, 'y' => 2143634.39608, 'z' => 220, 'm' => 120]);
141
142
        $record2 = new ShapeRecord(21);
143
        $record2->addPoint(['x' => 492131.764567, 'y' => 2143634.39608, 'z' => 150, 'm' => 80]);
144
145
        $record3 = new ShapeRecord(3);
146
        $record3->addPoint(['x' => 482131.764567, 'y' => 2143634.39608], 0);
147
        $record3->addPoint(['x' => 482132.764567, 'y' => 2143635.39608], 0);
148
        $record3->addPoint(['x' => 482131.764567, 'y' => 2143635.39608], 1);
149
        $record3->addPoint(['x' => 482132.764567, 'y' => 2143636.39608], 1);
150
151
        $shp->addRecord($record0);
152
        $shp->addRecord($record1);
153
        $shp->addRecord($record2);
154
        $shp->addRecord($record3);
155
156
        $shp->setDBFHeader(
157
            [
158
                [
159
                    'ID',
160
                    'N',
161
                    8,
162
                    0,
163
                ],
164
                [
165
                    'DESC',
166
                    'C',
167
                    50,
168
                    0,
169
                ],
170
            ]
171
        );
172
173
        $shp->records[0]->DBFData['ID'] = '1';
174
        $shp->records[0]->DBFData['DESC'] = 'AAAAAAAAA';
175
176
        $shp->records[1]->DBFData['ID'] = '2';
177
        $shp->records[1]->DBFData['DESC'] = 'BBBBBBBBBB';
178
179
        $shp->records[2]->DBFData['ID'] = '3';
180
        $shp->records[2]->DBFData['DESC'] = 'CCCCCCCCCCC';
181
182
        $shp->records[3]->DBFData['ID'] = '4';
183
        $shp->records[3]->DBFData['DESC'] = 'CCCCCCCCCCC';
184
185
        $shp->saveToFile('./data/test_shape.*');
186
    }
187
188
    /**
189
     * Tests creating file.
190
     */
191
    public function testCreate()
192
    {
193
        if (! ShapeFile::supportsDbase()) {
194
            $this->markTestSkipped('dbase extension missing');
195
        }
196
        $this->createTestData();
197
198
        $shp = new ShapeFile(1);
199
        $shp->loadFromFile('./data/test_shape.*');
200
        $this->assertEquals(4, count($shp->records));
201
    }
202
203
    /**
204
     * Tests removing record from a file.
205
     */
206
    public function testDelete()
207
    {
208
        if (! ShapeFile::supportsDbase()) {
209
            $this->markTestSkipped('dbase extension missing');
210
        }
211
        $this->createTestData();
212
213
        $shp = new ShapeFile(1);
214
        $shp->loadFromFile('./data/test_shape.*');
215
        $shp->deleteRecord(1);
216
        $shp->saveToFile();
217
        $this->assertEquals(3, count($shp->records));
218
219
        $shp = new ShapeFile(1);
220
        $shp->loadFromFile('./data/test_shape.*');
221
        $this->assertEquals(3, count($shp->records));
222
    }
223
224
    /**
225
     * Test adding record to a file.
226
     */
227
    public function testAdd()
228
    {
229
        if (! ShapeFile::supportsDbase()) {
230
            $this->markTestSkipped('dbase extension missing');
231
        }
232
        $this->createTestData();
233
234
        $shp = new ShapeFile(1);
235
        $shp->loadFromFile('./data/test_shape.*');
236
237
        $record0 = new ShapeRecord(1);
238
        $record0->addPoint(['x' => 482131.764567, 'y' => 2143634.39608]);
239
240
        $shp->addRecord($record0);
241
        $shp->records[4]->DBFData['ID'] = '4';
242
        $shp->records[4]->DBFData['DESC'] = 'CCCCCCCCCCC';
243
244
        $shp->saveToFile();
245
        $this->assertEquals(5, count($shp->records));
246
247
        $shp = new ShapeFile(1);
248
        $shp->loadFromFile('./data/test_shape.*');
249
        $this->assertEquals(5, count($shp->records));
250
    }
251
252
    /**
253
     * Tests saving without DBF.
254
     */
255
    public function testSaveNoDBF()
256
    {
257
        $shp = new ShapeFile(1);
258
        $shp->saveToFile('./data/test_nodbf.*');
259
        $this->assertFileNotExists('./data/test_nodbf.dbf');
260
    }
261
262
    /**
263
     * Test shape naming.
264
     */
265
    public function testShapeName()
266
    {
267
        $obj = new ShapeRecord(1);
268
        $this->assertEquals('Point', $obj->getShapeName());
269
        $obj = new Shapefile(1);
270
        $this->assertEquals('Point', $obj->getShapeName());
271
        $obj = new ShapeRecord(-1);
272
        $this->assertEquals('Shape -1', $obj->getShapeName());
273
    }
274
275
    /**
276
     * Test shapes save/load round robin.
277
     *
278
     * @param int   $type   Shape type
279
     * @param array $points Points
280
     *
281
     *
282
     * @dataProvider shapes
283
     */
284
    public function testShapeSaveLoad($type, $points)
285
    {
286
        $filename = "./data/test_shape-$type.*";
287
        $shp = new ShapeFile($type);
288
        $shp->setDBFHeader([
289
            [
290
                'ID',
291
                'N',
292
                19,
293
                0,
294
            ],
295
            [
296
                'DESC',
297
                'C',
298
                14,
299
                0,
300
            ],
301
        ]);
302
303
        $record0 = new ShapeRecord($type);
304
305
        foreach ($points as $point) {
306
            $record0->addPoint($point[0], $point[1]);
307
        }
308
309
        $shp->addRecord($record0);
310
311
        $shp->saveToFile($filename);
312
313
        $shp2 = new ShapeFile($type);
314
        $shp2->loadFromFile($filename);
315
316
        $this->assertEquals(
317
            count($shp->records),
318
            count($shp2->records)
319
        );
320
321
        $record = $shp->records[0];
322
        $record2 = $shp2->records[0];
323
324
        $items = [
325
            'numparts',
326
            'numpoints',
327
        ];
328
        foreach ($items as $item) {
329
            if (isset($record->SHPData[$item])) {
330
                $this->assertEquals(
331
                    $record->SHPData[$item],
332
                    $record2->SHPData[$item]
333
                );
334
            }
335
        }
336
337
        /* Test deletion works */
338
        $record->deletePoint();
339
    }
340
341
    /**
342
     * Test shapes save/load round robin with z coordinate.
343
     *
344
     * @param int   $type   Shape type
345
     * @param array $points Points
346
     *
347
     *
348
     * @dataProvider shapes
349
     */
350
    public function testZetShapeSaveLoad($type, $points)
351
    {
352
        $this->testShapeSaveLoad($type + 10, $points);
353
    }
354
355
    /**
356
     * Test shapes save/load round robin with measure.
357
     *
358
     * @param int   $type   Shape type
359
     * @param array $points Points
360
     *
361
     *
362
     * @dataProvider shapes
363
     */
364
    public function testMeasureShapeSaveLoad($type, $points)
365
    {
366
        $this->testShapeSaveLoad($type + 20, $points);
367
    }
368
369
    /**
370
     * Data provider for save/load testing.
371
     *
372
     * @return array
373
     */
374
    public function shapes()
375
    {
376
        return [
377
            [
378
                1,
379
                [
380
                    [
381
                        [
382
                            'x' => 10,
383
                            'y' => 20,
384
                        ], 0,
385
                    ],
386
                ],
387
            ],
388
            [
389
                3,
390
                [
391
                    [
392
                        [
393
                            'x' => 10,
394
                            'y' => 20,
395
                        ], 0,
396
                    ],
397
                    [
398
                        [
399
                            'x' => 20,
400
                            'y' => 20,
401
                        ], 0,
402
                    ],
403
                    [
404
                        [
405
                            'x' => 20,
406
                            'y' => 20,
407
                        ], 1,
408
                    ],
409
                    [
410
                        [
411
                            'x' => 20,
412
                            'y' => 10,
413
                        ], 1,
414
                    ],
415
                ],
416
            ],
417
            [
418
                5,
419
                [
420
                    [
421
                        [
422
                            'x' => 10,
423
                            'y' => 20,
424
                        ], 0,
425
                    ],
426
                    [
427
                        [
428
                            'x' => 20,
429
                            'y' => 20,
430
                        ], 0,
431
                    ],
432
                    [
433
                        [
434
                            'x' => 20,
435
                            'y' => 20,
436
                        ], 1,
437
                    ],
438
                    [
439
                        [
440
                            'x' => 20,
441
                            'y' => 10,
442
                        ], 1,
443
                    ],
444
                    [
445
                        [
446
                            'x' => 20,
447
                            'y' => 10,
448
                        ], 2,
449
                    ],
450
                    [
451
                        [
452
                            'x' => 10,
453
                            'y' => 20,
454
                        ], 2,
455
                    ],
456
                ],
457
            ],
458
            [
459
                8,
460
                [
461
                    [
462
                        [
463
                            'x' => 10,
464
                            'y' => 20,
465
                        ], 0,
466
                    ],
467
                    [
468
                        [
469
                            'x' => 20,
470
                            'y' => 20,
471
                        ], 0,
472
                    ],
473
                    [
474
                        [
475
                            'x' => 20,
476
                            'y' => 10,
477
                        ], 0,
478
                    ],
479
                ],
480
            ],
481
        ];
482
    }
483
484
    public function testSearch()
485
    {
486
        $shp = new ShapeFile(0);
487
        $shp->loadFromFile('data/capitals.*');
488
        /* Nonexisting entry or no dbase support */
489
        $this->assertEquals(
490
            -1,
491
            $shp->getIndexFromDBFData('CNTRY_NAME', 'nonexisting')
492
        );
493
        if (ShapeFile::supportsDbase()) {
494
            $this->assertEquals(
495
                218,
496
                $shp->getIndexFromDBFData('CNTRY_NAME', 'Czech Republic')
497
            );
498
        }
499
    }
500
}
501