Passed
Push — master ( 3bbbe2...c9cf55 )
by Michal
03:12
created

ShapeFileTest::provideErrorFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 14
rs 9.4285
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 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
 * http://www.gnu.org/copyleft/gpl.html.
21
 */
22
use ShapeFile\ShapeFile;
23
use ShapeFile\ShapeRecord;
24
25
class ShapeFileTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
26
{
27
    /**
28
     * Tests loading of a file
29
     *
30
     * @param string  $filename Name of file
31
     * @param integer $records  Expected number of records
32
     * @param integer $parts    Expected number of parts in first record
33
     *
34
     * @return void
35
     *
36
     * @dataProvider provideFiles
37
     */
38
    public function testLoad($filename, $records, $parts)
39
    {
40
        $shp = new ShapeFile(1);
41
        $shp->loadFromFile($filename);
42
        $this->assertEquals($records, count($shp->records));
43
        if (!is_null($parts)) {
44
            $this->assertEquals($parts, count($shp->records[0]->SHPData["parts"]));
45
        }
46
        $this->assertEquals('', $shp->lastError);
47
    }
48
49
    /**
50
     * Data provider for file loading tests.
51
     *
52
     * @return array
53
     */
54
    public function provideFiles()
55
    {
56
        return array(
57
            array('data/capitals.*', 652, null),
58
            array('data/mexico.*', 32, 3),
59
            array('data/Czech_Republic_AL2.*', 1, 1),
60
            array('data/w001n05f.*', 1, 1),
61
        );
62
    }
63
64
    /**
65
     * Test error handling in loader
66
     *
67
     * @param string $filename name to load
68
     *
69
     * @return void
70
     *
71
     * @dataProvider provideErrorFiles
72
     */
73
    public function testLoadError($filename)
74
    {
75
        $shp = new ShapeFile(1);
76
        $shp->loadFromFile($filename);
77
        $this->assertNotEquals('', $shp->lastError);
78
    }
79
80
    /**
81
     * Data provider for file loading error tests.
82
     *
83
     * @return array
84
     */
85
    public function provideErrorFiles()
86
    {
87
        $result = array(
88
            array('data/no-shp.*'),
89
            array('data/missing.*'),
90
        );
91
92
        if (ShapeFile::supports_dbase()) {
93
            $result[] = array('data/no-dbf.*');
94
            $result[] = array('data/invalid-dbf.*');
95
        }
96
97
        return $result;
98
    }
99
100
    /**
101
     * Creates test data
102
     *
103
     * @return void
104
     */
105
    private function createTestData()
106
    {
107
        $shp = new ShapeFile(1);
108
109
        $record0 = new ShapeRecord(1);
110
        $record0->addPoint(array("x" => 482131.764567, "y" => 2143634.39608));
111
112
        $record1 = new ShapeRecord(11);
113
        $record1->addPoint(array("x" => 472131.764567, "y" => 2143634.39608, 'z' => 220, 'm' => 120));
114
115
        $record2 = new ShapeRecord(21);
116
        $record2->addPoint(array("x" => 492131.764567, "y" => 2143634.39608, 'z' => 150, 'm' => 80));
117
118
        $shp->addRecord($record0);
119
        $shp->addRecord($record1);
120
        $shp->addRecord($record2);
121
122
        $shp->setDBFHeader(
123
            array(
124
                array('ID', 'N', 8, 0),
125
                array('DESC', 'C', 50, 0)
126
            )
127
        );
128
129
        $shp->records[0]->DBFData['ID'] = '1';
130
        $shp->records[0]->DBFData['DESC'] = 'AAAAAAAAA';
131
132
        $shp->records[1]->DBFData['ID'] = '2';
133
        $shp->records[1]->DBFData['DESC'] = 'BBBBBBBBBB';
134
135
        $shp->records[2]->DBFData['ID'] = '3';
136
        $shp->records[2]->DBFData['DESC'] = 'CCCCCCCCCCC';
137
138
        $shp->saveToFile('./data/test_shape.*');
139
    }
140
141
    /**
142
     * Tests creating file
143
     *
144
     * @return void
145
     */
146
    public function testCreate()
147
    {
148
        if (! ShapeFile::supports_dbase()) {
149
            $this->markTestSkipped('dbase extension missing');
150
        }
151
        $this->createTestData();
152
153
        $shp = new ShapeFile(1);
154
        $shp->loadFromFile('./data/test_shape.*');
155
        $this->assertEquals(3, count($shp->records));
156
    }
157
158
    /**
159
     * Tests removing record from a file
160
     *
161
     * @return void
162
     */
163
    public function testDelete()
164
    {
165
        if (! ShapeFile::supports_dbase()) {
166
            $this->markTestSkipped('dbase extension missing');
167
        }
168
        $this->createTestData();
169
170
        $shp = new ShapeFile(1);
171
        $shp->loadFromFile('./data/test_shape.*');
172
        $shp->deleteRecord(1);
173
        $shp->saveToFile();
174
        $this->assertEquals(2, count($shp->records));
175
176
        $shp = new ShapeFile(1);
177
        $shp->loadFromFile('./data/test_shape.*');
178
        $this->assertEquals(2, count($shp->records));
179
    }
180
181
    /**
182
     * Test adding record to a file
183
     *
184
     * @return void
185
     */
186
    public function testAdd()
187
    {
188
        if (! ShapeFile::supports_dbase()) {
189
            $this->markTestSkipped('dbase extension missing');
190
        }
191
        $this->createTestData();
192
193
        $shp = new ShapeFile(1);
194
        $shp->loadFromFile('./data/test_shape.*');
195
196
        $record0 = new ShapeRecord(1);
197
        $record0->addPoint(array("x" => 482131.764567, "y" => 2143634.39608));
198
199
        $shp->addRecord($record0);
200
        $shp->records[3]->DBFData['ID'] = '4';
201
        $shp->records[3]->DBFData['DESC'] = 'CCCCCCCCCCC';
202
203
        $shp->saveToFile();
204
        $this->assertEquals(4, count($shp->records));
205
206
        $shp = new ShapeFile(1);
207
        $shp->loadFromFile('./data/test_shape.*');
208
        $this->assertEquals(4, count($shp->records));
209
    }
210
}
211