Completed
Push — master ( 44fa0b...b8e178 )
by Michal
03:22
created

ShapeFileTest::testDelete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 17
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
namespace ShapeFileTest;
23
24
use ShapeFile\ShapeFile;
25
use ShapeFile\ShapeRecord;
26
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
        );
64
    }
65
66
    /**
67
     * Test error handling in loader
68
     *
69
     * @param string $filename name to load
70
     *
71
     * @return void
72
     *
73
     * @dataProvider provideErrorFiles
74
     */
75
    public function testLoadError($filename)
76
    {
77
        $shp = new ShapeFile(1);
78
        $shp->loadFromFile($filename);
79
        $this->assertNotEquals('', $shp->lastError);
80
    }
81
82
    /**
83
     * Data provider for file loading error tests.
84
     *
85
     * @return array
86
     */
87
    public function provideErrorFiles()
88
    {
89
        $result = array(
90
            array('data/no-shp.*'),
91
            array('data/missing.*'),
92
        );
93
94
        if (ShapeFile::supports_dbase()) {
95
            $result[] = array('data/no-dbf.*');
96
            $result[] = array('data/invalid-dbf.*');
97
        }
98
99
        return $result;
100
    }
101
102
    /**
103
     * Creates test data
104
     *
105
     * @return void
106
     */
107
    private function createTestData()
108
    {
109
        $shp = new ShapeFile(1);
110
111
        $record0 = new ShapeRecord(1);
112
        $record0->addPoint(array("x" => 482131.764567, "y" => 2143634.39608));
113
114
        $record1 = new ShapeRecord(11);
115
        $record1->addPoint(array("x" => 472131.764567, "y" => 2143634.39608, 'z' => 220, 'm' => 120));
116
117
        $record2 = new ShapeRecord(21);
118
        $record2->addPoint(array("x" => 492131.764567, "y" => 2143634.39608, 'z' => 150, 'm' => 80));
119
120
        $shp->addRecord($record0);
121
        $shp->addRecord($record1);
122
        $shp->addRecord($record2);
123
124
        $shp->setDBFHeader(
125
            array(
126
                array('ID', 'N', 8, 0),
127
                array('DESC', 'C', 50, 0)
128
            )
129
        );
130
131
        $shp->records[0]->DBFData['ID'] = '1';
132
        $shp->records[0]->DBFData['DESC'] = 'AAAAAAAAA';
133
134
        $shp->records[1]->DBFData['ID'] = '2';
135
        $shp->records[1]->DBFData['DESC'] = 'BBBBBBBBBB';
136
137
        $shp->records[2]->DBFData['ID'] = '3';
138
        $shp->records[2]->DBFData['DESC'] = 'CCCCCCCCCCC';
139
140
        $shp->saveToFile('./data/test_shape.*');
141
    }
142
143
    /**
144
     * Tests creating file
145
     *
146
     * @return void
147
     */
148
    public function testCreate()
149
    {
150
        if (!ShapeFile::supports_dbase()) {
151
            $this->markTestSkipped('dbase extension missing');
152
        }
153
        $this->createTestData();
154
155
        $shp = new ShapeFile(1);
156
        $shp->loadFromFile('./data/test_shape.*');
157
        $this->assertEquals(3, count($shp->records));
158
    }
159
160
    /**
161
     * Tests removing record from a file
162
     *
163
     * @return void
164
     */
165
    public function testDelete()
166
    {
167
        if (!ShapeFile::supports_dbase()) {
168
            $this->markTestSkipped('dbase extension missing');
169
        }
170
        $this->createTestData();
171
172
        $shp = new ShapeFile(1);
173
        $shp->loadFromFile('./data/test_shape.*');
174
        $shp->deleteRecord(1);
175
        $shp->saveToFile();
176
        $this->assertEquals(2, count($shp->records));
177
178
        $shp = new ShapeFile(1);
179
        $shp->loadFromFile('./data/test_shape.*');
180
        $this->assertEquals(2, count($shp->records));
181
    }
182
183
    /**
184
     * Test adding record to a file
185
     *
186
     * @return void
187
     */
188
    public function testAdd()
189
    {
190
        if (!ShapeFile::supports_dbase()) {
191
            $this->markTestSkipped('dbase extension missing');
192
        }
193
        $this->createTestData();
194
195
        $shp = new ShapeFile(1);
196
        $shp->loadFromFile('./data/test_shape.*');
197
198
        $record0 = new ShapeRecord(1);
199
        $record0->addPoint(array("x" => 482131.764567, "y" => 2143634.39608));
200
201
        $shp->addRecord($record0);
202
        $shp->records[3]->DBFData['ID'] = '4';
203
        $shp->records[3]->DBFData['DESC'] = 'CCCCCCCCCCC';
204
205
        $shp->saveToFile();
206
        $this->assertEquals(4, count($shp->records));
207
208
        $shp = new ShapeFile(1);
209
        $shp->loadFromFile('./data/test_shape.*');
210
        $this->assertEquals(4, count($shp->records));
211
    }
212
213
    /**
214
     * Test shape naming.
215
     *
216
     * @return void
217
     */
218
    public function testShapeName()
219
    {
220
        $obj = new ShapeRecord(1);
221
        $this->assertEquals('Point', $obj->getShapeName());
222
        $obj = new Shapefile(1);
223
        $this->assertEquals('Point', $obj->getShapeName());
224
        $obj = new ShapeRecord(-1);
225
        $this->assertEquals('Shape -1', $obj->getShapeName());
226
    }
227
}
228