Skip to content

Commit d179974

Browse files
committed
Added tests for grimzy#15
1 parent d1e8510 commit d179974

File tree

5 files changed

+183
-81
lines changed

5 files changed

+183
-81
lines changed

tests/Unit/Types/GeometryCollectionTest.php

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22

33
use Grimzy\LaravelMysqlSpatial\Types\GeometryCollection;
4+
use Grimzy\LaravelMysqlSpatial\Types\GeometryInterface;
45
use Grimzy\LaravelMysqlSpatial\Types\LineString;
56
use Grimzy\LaravelMysqlSpatial\Types\Point;
7+
use Grimzy\LaravelMysqlSpatial\Types\Polygon;
68

79
class GeometryCollectionTest extends BaseTestCase
810
{
@@ -40,6 +42,53 @@ public function testInvalidArgumentExceptionNotArrayGeometries()
4042
]);
4143
}
4244

45+
public function testToArray()
46+
{
47+
$geometryCollection = $this->getGeometryCollection();
48+
49+
$this->assertInternalType('array', $geometryCollection->toArray());
50+
}
51+
52+
public function testIteratorAggregate()
53+
{
54+
$geometryCollection = $this->getGeometryCollection();
55+
56+
foreach ($geometryCollection as $value) {
57+
$this->assertInstanceOf(GeometryInterface::class, $value);
58+
}
59+
}
60+
61+
public function testArrayAccess()
62+
{
63+
$linestring = $this->getLineString();
64+
$point = $this->getPoint();
65+
$geometryCollection = new GeometryCollection([$linestring, $point]);
66+
67+
// assert getting
68+
$this->assertEquals($linestring, $geometryCollection[0]);
69+
$this->assertEquals($point, $geometryCollection[1]);
70+
71+
// assert setting
72+
$polygon = $this->getPolygon();
73+
$geometryCollection[] = $polygon;
74+
$this->assertEquals($polygon, $geometryCollection[2]);
75+
76+
// assert unset
77+
unset($geometryCollection[0]);
78+
$this->assertNull($geometryCollection[0]);
79+
$this->assertEquals($point, $geometryCollection[1]);
80+
$this->assertEquals($polygon, $geometryCollection[2]);
81+
82+
// assert insert
83+
$point100 = new Point(100, 100);
84+
$geometryCollection[100] = $point100;
85+
$this->assertEquals($point100, $geometryCollection[100]);
86+
87+
// assert invalid
88+
$this->assertException(InvalidArgumentException::class);
89+
$geometryCollection[] = 1;
90+
}
91+
4392
private function getGeometryCollection()
4493
{
4594
return new GeometryCollection([$this->getLineString(), $this->getPoint()]);
@@ -48,16 +97,29 @@ private function getGeometryCollection()
4897
private function getLineString()
4998
{
5099
return new LineString([
51-
new Point(0, 0),
52-
new Point(0, 1),
53-
new Point(1, 1),
54-
new Point(1, 0),
55-
new Point(0, 0),
56-
]);
100+
new Point(0, 0),
101+
new Point(0, 1),
102+
new Point(1, 1),
103+
new Point(1, 0),
104+
new Point(0, 0),
105+
]);
57106
}
58107

59108
private function getPoint()
60109
{
61110
return new Point(100, 200);
62111
}
112+
113+
private function getPolygon()
114+
{
115+
return new Polygon([
116+
new LineString([
117+
new Point(0, 0),
118+
new Point(0, 1),
119+
new Point(1, 1),
120+
new Point(1, 0),
121+
new Point(0, 0),
122+
]),
123+
]);
124+
}
63125
}

tests/Unit/Types/GeometryTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,11 @@ public function testGetWKBClass()
5252
$prefix = "\0\0\0\0";
5353
$this->assertInstanceOf(Point::class, Geometry::fromWKB($prefix.'0101000000000000000000f03f0000000000000040'));
5454
}
55+
56+
public function testToJson()
57+
{
58+
$point = new Point(1, 1);
59+
60+
$this->assertSame('{"type":"Point","coordinates":[1,1]}', $point->toJson());
61+
}
5562
}

tests/Unit/Types/MultiLineStringTest.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ public function testFromWKT()
1717
public function testToWKT()
1818
{
1919
$collection = new LineString([
20-
new Point(0, 0),
21-
new Point(0, 1),
22-
new Point(1, 1),
23-
new Point(1, 0),
24-
new Point(0, 0),
25-
]);
20+
new Point(0, 0),
21+
new Point(0, 1),
22+
new Point(1, 1),
23+
new Point(1, 0),
24+
new Point(0, 0),
25+
]);
2626

2727
$multilinestring = new MultiLineString([$collection]);
2828

@@ -51,4 +51,34 @@ public function testInvalidArgumentExceptionNotArrayOfLineString()
5151
new Point(0, 1),
5252
]);
5353
}
54+
55+
public function testArrayAccess()
56+
{
57+
$linestring0 = new LineString([
58+
new Point(0, 0),
59+
new Point(1, 1),
60+
]);
61+
$linestring1 = new LineString([
62+
new Point(1, 1),
63+
new Point(2, 2),
64+
]);
65+
66+
$multilinestring = new MultiLineString([$linestring0, $linestring1]);
67+
68+
// assert getting
69+
$this->assertEquals($linestring0, $multilinestring[0]);
70+
$this->assertEquals($linestring1, $multilinestring[1]);
71+
72+
// assert setting
73+
$linestring2 = new LineString([
74+
new Point(2, 2),
75+
new Point(3, 3),
76+
]);
77+
$multilinestring[] = $linestring2;
78+
$this->assertEquals($linestring2, $multilinestring[2]);
79+
80+
// assert invalid
81+
$this->assertException(InvalidArgumentException::class);
82+
$multilinestring[] = 1;
83+
}
5484
}

tests/Unit/Types/MultiPointTest.php

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,57 +29,6 @@ public function testGetPoints()
2929
$this->assertInstanceOf(Point::class, $multipoint->getPoints()[0]);
3030
}
3131

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-
{
41-
$multipoint = MultiPoint::fromWKT('MULTIPOINT((0 0),(1 0),(1 1))');
42-
43-
foreach ($multipoint as $value) {
44-
$this->assertInstanceOf(Point::class, $value);
45-
}
46-
}
47-
48-
public function testArrayAccess()
49-
{
50-
$point0 = new Point(0, 0);
51-
$point1 = new Point(1, 1);
52-
$multipoint = new MultiPoint([$point0, $point1]);
53-
54-
$this->assertEquals($point0, $multipoint[0]);
55-
$this->assertEquals($point1, $multipoint[1]);
56-
$point2 = new Point(2, 2);
57-
58-
$multipoint[] = $point2;
59-
$this->assertEquals($point2, $multipoint[2]);
60-
61-
unset($multipoint[0]);
62-
$this->assertNull($multipoint[0]);
63-
$this->assertEquals($point1, $multipoint[1]);
64-
$this->assertEquals($point2, $multipoint[2]);
65-
66-
$point100 = new Point(100, 100);
67-
$multipoint[100] = $point100;
68-
$this->assertEquals($point100, $multipoint[100]);
69-
70-
$this->assertException(InvalidArgumentException::class);
71-
$multipoint[] = 1;
72-
}
73-
74-
public function testToJson()
75-
{
76-
$collection = [new Point(0, 0), new Point(0, 1), new Point(1, 1)];
77-
78-
$multipoint = new MultiPoint($collection);
79-
80-
$this->assertSame('{"type":"MultiPoint","coordinates":[[0,0],[1,0],[1,1]]}', $multipoint->toJson());
81-
}
82-
8332
public function testJsonSerialize()
8433
{
8534
$collection = [new Point(0, 0), new Point(0, 1), new Point(1, 1)];
@@ -105,6 +54,26 @@ public function testInvalidArgumentExceptionNotArrayOfLineString()
10554
]);
10655
}
10756

57+
public function testArrayAccess()
58+
{
59+
$point0 = new Point(0, 0);
60+
$point1 = new Point(1, 1);
61+
$multipoint = new MultiPoint([$point0, $point1]);
62+
63+
// assert getting
64+
$this->assertEquals($point0, $multipoint[0]);
65+
$this->assertEquals($point1, $multipoint[1]);
66+
67+
// assert setting
68+
$point2 = new Point(2, 2);
69+
$multipoint[] = $point2;
70+
$this->assertEquals($point2, $multipoint[2]);
71+
72+
// assert invalid
73+
$this->assertException(InvalidArgumentException::class);
74+
$multipoint[] = 1;
75+
}
76+
10877
public function testDeprecatedPrependPoint()
10978
{
11079
$point1 = new Point(1, 1);

tests/Unit/Types/MultiPolygonTest.php

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,27 @@ public function testInvalidArgumentExceptionNotArrayOfLineString()
4949
]);
5050
}
5151

52+
public function testArrayAccess()
53+
{
54+
$polygon0 = $this->getPolygon1();
55+
$polygon1 = $this->getPolygon2();
56+
57+
$multipolygon = new MultiPolygon([$polygon0, $polygon1]);
58+
59+
// assert getting
60+
$this->assertEquals($polygon0, $multipolygon[0]);
61+
$this->assertEquals($polygon1, $multipolygon[1]);
62+
63+
// assert setting
64+
$polygon2 = $this->getPolygon3();
65+
$multipolygon[] = $polygon2;
66+
$this->assertEquals($polygon2, $multipolygon[2]);
67+
68+
// assert invalid
69+
$this->assertException(InvalidArgumentException::class);
70+
$multipolygon[] = 1;
71+
}
72+
5273
private function getMultiPolygon()
5374
{
5475
return new MultiPolygon([$this->getPolygon1(), $this->getPolygon2()]);
@@ -57,34 +78,34 @@ private function getMultiPolygon()
5778
private function getLineString1()
5879
{
5980
return new LineString([
60-
new Point(0, 0),
61-
new Point(0, 1),
62-
new Point(1, 1),
63-
new Point(1, 0),
64-
new Point(0, 0),
65-
]);
81+
new Point(0, 0),
82+
new Point(0, 1),
83+
new Point(1, 1),
84+
new Point(1, 0),
85+
new Point(0, 0),
86+
]);
6687
}
6788

6889
private function getLineString2()
6990
{
7091
return new LineString([
71-
new Point(10, 10),
72-
new Point(10, 20),
73-
new Point(20, 20),
74-
new Point(20, 10),
75-
new Point(10, 10),
76-
]);
92+
new Point(10, 10),
93+
new Point(10, 20),
94+
new Point(20, 20),
95+
new Point(20, 10),
96+
new Point(10, 10),
97+
]);
7798
}
7899

79100
private function getLineString3()
80101
{
81102
return new LineString([
82-
new Point(100, 100),
83-
new Point(100, 200),
84-
new Point(200, 200),
85-
new Point(200, 100),
86-
new Point(100, 100),
87-
]);
103+
new Point(100, 100),
104+
new Point(100, 200),
105+
new Point(200, 200),
106+
new Point(200, 100),
107+
new Point(100, 100),
108+
]);
88109
}
89110

90111
private function getPolygon1()
@@ -96,4 +117,17 @@ private function getPolygon2()
96117
{
97118
return new Polygon([$this->getLineString3()]);
98119
}
120+
121+
private function getPolygon3()
122+
{
123+
return new Polygon([
124+
new LineString([
125+
new Point(10, 10),
126+
new Point(10, 20),
127+
new Point(20, 20),
128+
new Point(20, 10),
129+
new Point(10, 10),
130+
]),
131+
]);
132+
}
99133
}

0 commit comments

Comments
 (0)