Skip to content

Commit 3d2ae93

Browse files
committed
Refactored and added some missing tests
(cherry picked from commit 7af87a7)
1 parent 6cf7401 commit 3d2ae93

File tree

5 files changed

+149
-72
lines changed

5 files changed

+149
-72
lines changed

tests/Unit/Types/GeometryCollectionTest.php

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,6 @@
66

77
class GeometryCollectionTest extends BaseTestCase
88
{
9-
/**
10-
* @var GeometryCollection
11-
*/
12-
private $collection;
13-
14-
protected function setUp()
15-
{
16-
$collection = new LineString(
17-
[
18-
new Point(0, 0),
19-
new Point(0, 1),
20-
new Point(1, 1),
21-
new Point(1, 0),
22-
new Point(0, 0),
23-
]
24-
);
25-
26-
$point = new Point(100, 200);
27-
28-
$this->collection = new GeometryCollection([$collection, $point]);
29-
}
30-
319
public function testFromWKT()
3210
{
3311
/**
@@ -45,20 +23,48 @@ public function testToWKT()
4523
{
4624
$this->assertEquals(
4725
'GEOMETRYCOLLECTION(LINESTRING(0 0,1 0,1 1,0 1,0 0),POINT(200 100))',
48-
$this->collection->toWKT()
26+
$this->getGeometryCollection()->toWKT()
4927
);
5028
}
5129

5230
public function testJsonSerialize()
5331
{
5432
$this->assertInstanceOf(
5533
\GeoJson\Geometry\GeometryCollection::class,
56-
$this->collection->jsonSerialize()
34+
$this->getGeometryCollection()->jsonSerialize()
5735
);
5836

5937
$this->assertSame(
6038
'{"type":"GeometryCollection","geometries":[{"type":"LineString","coordinates":[[0,0],[1,0],[1,1],[0,1],[0,0]]},{"type":"Point","coordinates":[200,100]}]}',
61-
json_encode($this->collection->jsonSerialize())
39+
json_encode($this->getGeometryCollection()->jsonSerialize())
40+
);
41+
}
42+
43+
public function testInvalidArgumentExceptionNotArrayGeometries() {
44+
$this->expectException(InvalidArgumentException::class);
45+
$geometrycollection = new GeometryCollection([
46+
$this->getPoint(),
47+
1
48+
]);
49+
}
50+
51+
private function getGeometryCollection() {
52+
return new GeometryCollection([$this->getLineString(), $this->getPoint()]);
53+
}
54+
55+
private function getLineString() {
56+
return new LineString(
57+
[
58+
new Point(0, 0),
59+
new Point(0, 1),
60+
new Point(1, 1),
61+
new Point(1, 0),
62+
new Point(0, 0),
63+
]
6264
);
6365
}
66+
67+
private function getPoint() {
68+
return new Point(100, 200);
69+
}
6470
}

tests/Unit/Types/GeometryTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,12 @@ public function testGetWKBClass()
117117
GeometryCollection::class,
118118
Geometry::fromWKB('0107000000020000000101000000000000000000f03f0000000000000040010200000002000000000000000000f03f000000000000004000000000000008400000000000001040')
119119
);
120+
121+
$prefix = "\0\0\0\0";
122+
$this->assertInstanceOf(
123+
Point::class,
124+
Geometry::fromWKB($prefix . '0101000000000000000000f03f0000000000000040')
125+
);
126+
120127
}
121128
}

tests/Unit/Types/MultiLineStringTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,17 @@ public function testJsonSerialize()
4141
json_encode($multilinestring)
4242
);
4343
}
44+
45+
public function testInvalidArgumentExceptionAtLeastOneEntry() {
46+
$this->expectException(InvalidArgumentException::class);
47+
$multilinestring = new MultiLineString([]);
48+
}
49+
50+
public function testInvalidArgumentExceptionNotArrayOfLineString() {
51+
$this->expectException(InvalidArgumentException::class);
52+
$multilinestring = new MultiLineString([
53+
new LineString([new Point(0, 0), new Point(1, 1)]),
54+
new Point(0,1)
55+
]);
56+
}
4457
}

tests/Unit/Types/MultiPointTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,32 @@ public function testToWKT()
2222
$this->assertEquals('MULTIPOINT((0 0),(1 0),(1 1))', $multipoint->toWKT());
2323
}
2424

25+
public function testGetPoints()
26+
{
27+
$multipoint = MultiPoint::fromWKT('MULTIPOINT((0 0),(1 0),(1 1))');
28+
29+
$this->assertInstanceOf(Point::class, $multipoint->getPoints()[0]);
30+
}
31+
32+
public function testToArray()
33+
{
34+
$multipoint = MultiPoint::fromWKT('MULTIPOINT((0 0),(1 0),(1 1))');
35+
36+
$this->assertInstanceOf(Point::class, $multipoint->toArray()[0]);
37+
}
38+
39+
public function testIteratorAggregate() {
40+
$multipoint = MultiPoint::fromWKT('MULTIPOINT((0 0),(1 0),(1 1))');
41+
42+
foreach($multipoint as $value) {
43+
$this->assertInstanceOf(Point::class, $value);
44+
}
45+
}
46+
47+
public function testArrayAccess() {
48+
49+
}
50+
2551
public function testJsonSerialize()
2652
{
2753
$collection = [new Point(0, 0), new Point(0, 1), new Point(1, 1)];
@@ -31,4 +57,17 @@ public function testJsonSerialize()
3157
$this->assertInstanceOf(\GeoJson\Geometry\MultiPoint::class, $multipoint->jsonSerialize());
3258
$this->assertSame('{"type":"MultiPoint","coordinates":[[0,0],[1,0],[1,1]]}', json_encode($multipoint));
3359
}
60+
61+
public function testInvalidArgumentExceptionAtLeastOneEntry() {
62+
$this->expectException(InvalidArgumentException::class);
63+
$multipoint = new MultiPoint([]);
64+
}
65+
66+
public function testInvalidArgumentExceptionNotArrayOfLineString() {
67+
$this->expectException(InvalidArgumentException::class);
68+
$multipoint = new MultiPoint([
69+
new Point(0, 0),
70+
1
71+
]);
72+
}
3473
}

tests/Unit/Types/MultiPolygonTest.php

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,6 @@
77

88
class MultiPolygonTest extends BaseTestCase
99
{
10-
/**
11-
* @var MultiPolygon
12-
*/
13-
private $multiPolygon;
14-
15-
protected function setUp()
16-
{
17-
$collection1 = new LineString(
18-
[
19-
new Point(0, 0),
20-
new Point(0, 1),
21-
new Point(1, 1),
22-
new Point(1, 0),
23-
new Point(0, 0),
24-
]
25-
);
26-
27-
$collection2 = new LineString(
28-
[
29-
new Point(10, 10),
30-
new Point(10, 20),
31-
new Point(20, 20),
32-
new Point(20, 10),
33-
new Point(10, 10),
34-
]
35-
);
36-
37-
$polygon1 = new Polygon([$collection1, $collection2]);
38-
39-
$collection3 = new LineString(
40-
[
41-
new Point(100, 100),
42-
new Point(100, 200),
43-
new Point(200, 200),
44-
new Point(200, 100),
45-
new Point(100, 100),
46-
]
47-
);
48-
49-
$polygon2 = new Polygon([$collection3]);
50-
51-
$this->multiPolygon = new MultiPolygon([$polygon1, $polygon2]);
52-
}
53-
5410
public function testFromWKT()
5511
{
5612
$polygon = MultiPolygon::fromWKT(
@@ -65,7 +21,7 @@ public function testToWKT()
6521
{
6622
$this->assertEquals(
6723
'MULTIPOLYGON(((0 0,1 0,1 1,0 1,0 0),(10 10,20 10,20 20,10 20,10 10)),((100 100,200 100,200 200,100 200,100 100)))',
68-
$this->multiPolygon->toWKT()
24+
$this->getMultiPolygon()->toWKT()
6925
);
7026
}
7127

@@ -89,10 +45,66 @@ public function testIssue12()
8945

9046
public function testJsonSerialize()
9147
{
92-
$this->assertInstanceOf(\GeoJson\Geometry\MultiPolygon::class, $this->multiPolygon->jsonSerialize());
48+
$this->assertInstanceOf(\GeoJson\Geometry\MultiPolygon::class, $this->getMultiPolygon()->jsonSerialize());
9349
$this->assertSame(
9450
'{"type":"MultiPolygon","coordinates":[[[[0,0],[1,0],[1,1],[0,1],[0,0]],[[10,10],[20,10],[20,20],[10,20],[10,10]]],[[[100,100],[200,100],[200,200],[100,200],[100,100]]]]}',
95-
json_encode($this->multiPolygon)
51+
json_encode($this->getMultiPolygon())
9652
);
9753
}
54+
55+
public function testInvalidArgumentExceptionNotArrayOfLineString() {
56+
$this->expectException(InvalidArgumentException::class);
57+
$multipolygon = new MultiPolygon([
58+
$this->getPolygon1(),
59+
$this->getLineString1()
60+
]);
61+
}
62+
63+
private function getMultiPolygon() {
64+
return new MultiPolygon([$this->getPolygon1(), $this->getPolygon2()]);
65+
}
66+
67+
private function getLineString1() {
68+
return new LineString(
69+
[
70+
new Point(0, 0),
71+
new Point(0, 1),
72+
new Point(1, 1),
73+
new Point(1, 0),
74+
new Point(0, 0),
75+
]
76+
);
77+
}
78+
79+
private function getLineString2() {
80+
return new LineString(
81+
[
82+
new Point(10, 10),
83+
new Point(10, 20),
84+
new Point(20, 20),
85+
new Point(20, 10),
86+
new Point(10, 10),
87+
]
88+
);
89+
}
90+
91+
private function getLineString3() {
92+
return new LineString(
93+
[
94+
new Point(100, 100),
95+
new Point(100, 200),
96+
new Point(200, 200),
97+
new Point(200, 100),
98+
new Point(100, 100),
99+
]
100+
);
101+
}
102+
103+
private function getPolygon1() {
104+
return new Polygon([$this->getLineString1(), $this->getLineString2()]);
105+
}
106+
107+
private function getPolygon2() {
108+
return new Polygon([$this->getLineString3()]);
109+
}
98110
}

0 commit comments

Comments
 (0)