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