Passed
Push — master ( 2377b8...7d8bb8 )
by Maurício
04:21 queued 15s
created

ShapeFileTest::testAllowsNoDbf()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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