Skip to content

Commit 84e80bf

Browse files
authored
Merge pull request #16 from grimzy/improvement/tests-coverage-cleanup
Tests coverage cleanup
2 parents d9e9686 + bab2c2e commit 84e80bf

12 files changed

+275
-204
lines changed

src/Types/MultiLineString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct(array $linestrings)
2626
});
2727

2828
if (count($linestrings) !== count($validated)) {
29-
throw new InvalidArgumentException('$linestrings must be an array of Points');
29+
throw new InvalidArgumentException('$linestrings must be an array of LineString');
3030
}
3131

3232
$this->linestrings = $linestrings;

src/Types/MultiPoint.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ public static function fromString($wktArgument)
2121
$matches = [];
2222
preg_match_all('/\(\s*(\d+\s+\d+)\s*\)/', trim($wktArgument), $matches);
2323

24-
if (count($matches) < 2) {
25-
return new static([]);
26-
}
27-
2824
$points = array_map(function ($pair) {
2925
return Point::fromPair($pair);
3026
}, $matches[1]);

src/Types/MultiPolygon.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(array $polygons)
2222
});
2323

2424
if (count($polygons) !== count($validated)) {
25-
throw new InvalidArgumentException('$polygons must be an array of Points');
25+
throw new InvalidArgumentException('$polygons must be an array of Polygon');
2626
}
2727
$this->polygons = $polygons;
2828
}

src/Types/PointCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function insertPoint($index, Point $point)
6767
throw new InvalidArgumentException('$index is greater than the size of the array');
6868
}
6969

70-
array_splice($this->points, $offset, 0, [$point]);
70+
array_splice($this->points, $index, 0, [$point]);
7171
}
7272

7373
public function offsetExists($offset)

tests/Integration/Migrations/UpdateTables.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function up()
2121
$table->point('location')->change();
2222

2323
// The other field changes are just here to test if change works with them, we're not changing anything
24+
$table->geometry('geo')->default(null)->nullable()->change();
2425
$table->lineString('line')->default(null)->nullable()->change();
2526
$table->polygon('shape')->default(null)->nullable()->change();
2627
$table->multiPoint('multi_locations')->default(null)->nullable()->change();

tests/Integration/SpatialTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ protected function assertDatabaseHas($table, array $data, $connection = null)
6565
}
6666
}
6767

68+
protected function assertException($exceptionName)
69+
{
70+
if (method_exists(parent::class, 'expectException')) {
71+
parent::expectException($exceptionName);
72+
} else {
73+
$this->setExpectedException($exceptionName);
74+
}
75+
}
76+
6877
private function onMigrations(\Closure $closure, $reverse_sort = false)
6978
{
7079
$fileSystem = new Filesystem();
@@ -87,7 +96,7 @@ public function testSpatialFieldsNotDefinedException()
8796
$geo->geometry = new Point(1, 2);
8897
$geo->save();
8998

90-
$this->setExpectedException(\Grimzy\LaravelMysqlSpatial\Exceptions\SpatialFieldsNotDefinedException::class);
99+
$this->assertException(\Grimzy\LaravelMysqlSpatial\Exceptions\SpatialFieldsNotDefinedException::class);
91100
NoSpatialFieldsModel::all();
92101
}
93102

tests/Unit/BaseTestCase.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,13 @@ public function tearDown()
66
{
77
Mockery::close();
88
}
9+
10+
protected function assertException($exceptionName)
11+
{
12+
if (method_exists(parent::class, 'expectException')) {
13+
parent::expectException($exceptionName);
14+
} else {
15+
$this->setExpectedException($exceptionName);
16+
}
17+
}
918
}

tests/Unit/Types/GeometryCollectionTest.php

Lines changed: 33 additions & 34 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
/**
@@ -43,22 +21,43 @@ public function testFromWKT()
4321

4422
public function testToWKT()
4523
{
46-
$this->assertEquals(
47-
'GEOMETRYCOLLECTION(LINESTRING(0 0,1 0,1 1,0 1,0 0),POINT(200 100))',
48-
$this->collection->toWKT()
49-
);
24+
$this->assertEquals('GEOMETRYCOLLECTION(LINESTRING(0 0,1 0,1 1,0 1,0 0),POINT(200 100))', $this->getGeometryCollection()->toWKT());
5025
}
5126

5227
public function testJsonSerialize()
5328
{
54-
$this->assertInstanceOf(
55-
\GeoJson\Geometry\GeometryCollection::class,
56-
$this->collection->jsonSerialize()
57-
);
29+
$this->assertInstanceOf(\GeoJson\Geometry\GeometryCollection::class, $this->getGeometryCollection()->jsonSerialize());
30+
31+
$this->assertSame('{"type":"GeometryCollection","geometries":[{"type":"LineString","coordinates":[[0,0],[1,0],[1,1],[0,1],[0,0]]},{"type":"Point","coordinates":[200,100]}]}', json_encode($this->getGeometryCollection()->jsonSerialize()));
32+
}
33+
34+
public function testInvalidArgumentExceptionNotArrayGeometries()
35+
{
36+
$this->assertException(InvalidArgumentException::class);
37+
$geometrycollection = new GeometryCollection([
38+
$this->getPoint(),
39+
1,
40+
]);
41+
}
42+
43+
private function getGeometryCollection()
44+
{
45+
return new GeometryCollection([$this->getLineString(), $this->getPoint()]);
46+
}
47+
48+
private function getLineString()
49+
{
50+
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+
]);
57+
}
5858

59-
$this->assertSame(
60-
'{"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())
62-
);
59+
private function getPoint()
60+
{
61+
return new Point(100, 200);
6362
}
6463
}

tests/Unit/Types/GeometryTest.php

Lines changed: 29 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Grimzy\LaravelMysqlSpatial\Exceptions\UnknownWKTTypeException;
34
use Grimzy\LaravelMysqlSpatial\Types\Geometry;
45
use Grimzy\LaravelMysqlSpatial\Types\GeometryCollection;
56
use Grimzy\LaravelMysqlSpatial\Types\LineString;
@@ -13,106 +14,42 @@ class GeometryTest extends BaseTestCase
1314
{
1415
public function testGetWKTArgument()
1516
{
16-
$this->assertEquals(
17-
'0 0',
18-
Geometry::getWKTArgument('POINT(0 0)')
19-
);
20-
$this->assertEquals(
21-
'0 0,1 1,1 2',
22-
Geometry::getWKTArgument('LINESTRING(0 0,1 1,1 2)')
23-
);
24-
$this->assertEquals(
25-
'(0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1)',
26-
Geometry::getWKTArgument('POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1))')
27-
);
28-
$this->assertEquals(
29-
'(0 0),(1 2)',
30-
Geometry::getWKTArgument('MULTIPOINT((0 0),(1 2))')
31-
);
32-
$this->assertEquals(
33-
'(0 0,1 1,1 2),(2 3,3 2,5 4)',
34-
Geometry::getWKTArgument('MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))')
35-
);
36-
$this->assertEquals(
37-
'((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1))',
38-
Geometry::getWKTArgument('MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))')
39-
);
40-
$this->assertEquals(
41-
'POINT(2 3),LINESTRING(2 3,3 4)',
42-
Geometry::getWKTArgument('GEOMETRYCOLLECTION(POINT(2 3),LINESTRING(2 3,3 4))')
43-
);
17+
$this->assertEquals('0 0', Geometry::getWKTArgument('POINT(0 0)'));
18+
$this->assertEquals('0 0,1 1,1 2', Geometry::getWKTArgument('LINESTRING(0 0,1 1,1 2)'));
19+
$this->assertEquals('(0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1)', Geometry::getWKTArgument('POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1))'));
20+
$this->assertEquals('(0 0),(1 2)', Geometry::getWKTArgument('MULTIPOINT((0 0),(1 2))'));
21+
$this->assertEquals('(0 0,1 1,1 2),(2 3,3 2,5 4)', Geometry::getWKTArgument('MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))'));
22+
$this->assertEquals('((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1))', Geometry::getWKTArgument('MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))'));
23+
$this->assertEquals('POINT(2 3),LINESTRING(2 3,3 4)', Geometry::getWKTArgument('GEOMETRYCOLLECTION(POINT(2 3),LINESTRING(2 3,3 4))'));
4424
}
4525

4626
public function testGetWKTClass()
4727
{
48-
$this->assertEquals(
49-
Point::class,
50-
Geometry::getWKTClass('POINT(0 0)')
51-
);
52-
$this->assertEquals(
53-
LineString::class,
54-
Geometry::getWKTClass('LINESTRING(0 0,1 1,1 2)')
55-
);
56-
$this->assertEquals(
57-
Polygon::class,
58-
Geometry::getWKTClass('POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1))')
59-
);
60-
$this->assertEquals(
61-
MultiPoint::class,
62-
Geometry::getWKTClass('MULTIPOINT((0 0),(1 2))')
63-
);
64-
$this->assertEquals(
65-
MultiLineString::class,
66-
Geometry::getWKTClass('MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))')
67-
);
68-
$this->assertEquals(
69-
MultiPolygon::class,
70-
Geometry::getWKTClass('MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))')
71-
);
72-
$this->assertEquals(
73-
GeometryCollection::class,
74-
Geometry::getWKTClass('GEOMETRYCOLLECTION(POINT(2 3),LINESTRING(2 3,3 4))')
75-
);
28+
$this->assertEquals(Point::class, Geometry::getWKTClass('POINT(0 0)'));
29+
$this->assertEquals(LineString::class, Geometry::getWKTClass('LINESTRING(0 0,1 1,1 2)'));
30+
$this->assertEquals(Polygon::class, Geometry::getWKTClass('POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1))'));
31+
$this->assertEquals(MultiPoint::class, Geometry::getWKTClass('MULTIPOINT((0 0),(1 2))'));
32+
$this->assertEquals(MultiLineString::class, Geometry::getWKTClass('MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))'));
33+
$this->assertEquals(MultiPolygon::class, Geometry::getWKTClass('MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))'));
34+
$this->assertEquals(GeometryCollection::class, Geometry::getWKTClass('GEOMETRYCOLLECTION(POINT(2 3),LINESTRING(2 3,3 4))'));
35+
$this->assertException(UnknownWKTTypeException::class);
36+
Geometry::getWKTClass('TRIANGLE((0 0, 0 9, 9 0, 0 0))');
7637
}
7738

7839
public function testGetWKBClass()
7940
{
80-
$this->assertInstanceOf(
81-
Point::class,
82-
Geometry::fromWKB('0101000000000000000000f03f0000000000000040')
83-
);
41+
$this->assertInstanceOf(Point::class, Geometry::fromWKB('0101000000000000000000f03f0000000000000040'));
8442

85-
$this->assertInstanceOf(
86-
LineString::class,
87-
Geometry::fromWKB('010200000002000000000000000000f03f000000000000004000000000000008400000000000001040')
88-
);
89-
$this->assertInstanceOf(
90-
Polygon::class,
91-
Geometry::fromWKB('01030000000100000004000000000000000000f03f00000000000000400000000000000840000000000000104000000000000014400000000000001840000000000000f03f0000000000000040')
92-
);
93-
$this->assertInstanceOf(
94-
MultiPoint::class,
95-
Geometry::fromWKB('0104000000020000000101000000000000000000f03f0000000000000040010100000000000000000008400000000000001040')
96-
);
97-
$this->assertInstanceOf(
98-
MultiLineString::class,
99-
Geometry::fromWKB('010500000001000000010200000002000000000000000000f03f000000000000004000000000000008400000000000001040')
100-
);
101-
$this->assertInstanceOf(
102-
MultiLineString::class,
103-
Geometry::fromWKB('010500000002000000010200000002000000000000000000f03f000000000000004000000000000008400000000000001040010200000002000000000000000000144000000000000018400000000000001c400000000000002040')
104-
);
105-
$this->assertInstanceOf(
106-
MultiPolygon::class,
107-
Geometry::fromWKB('01060000000200000001030000000100000004000000000000000000f03f00000000000000400000000000000840000000000000104000000000000014400000000000001840000000000000f03f000000000000004001030000000300000004000000000000000000f03f00000000000000400000000000000840000000000000104000000000000014400000000000001840000000000000f03f000000000000004004000000000000000000264000000000000028400000000000002a400000000000002c400000000000002e4000000000000030400000000000002640000000000000284004000000000000000000354000000000000036400000000000003740000000000000384000000000000039400000000000003a4000000000000035400000000000003640')
108-
);
109-
$this->assertInstanceOf(
110-
GeometryCollection::class,
111-
Geometry::fromWKB('0107000000010000000101000000000000000000f03f0000000000000040')
112-
);
113-
$this->assertInstanceOf(
114-
GeometryCollection::class,
115-
Geometry::fromWKB('0107000000020000000101000000000000000000f03f0000000000000040010200000002000000000000000000f03f000000000000004000000000000008400000000000001040')
116-
);
43+
$this->assertInstanceOf(LineString::class, Geometry::fromWKB('010200000002000000000000000000f03f000000000000004000000000000008400000000000001040'));
44+
$this->assertInstanceOf(Polygon::class, Geometry::fromWKB('01030000000100000004000000000000000000f03f00000000000000400000000000000840000000000000104000000000000014400000000000001840000000000000f03f0000000000000040'));
45+
$this->assertInstanceOf(MultiPoint::class, Geometry::fromWKB('0104000000020000000101000000000000000000f03f0000000000000040010100000000000000000008400000000000001040'));
46+
$this->assertInstanceOf(MultiLineString::class, Geometry::fromWKB('010500000001000000010200000002000000000000000000f03f000000000000004000000000000008400000000000001040'));
47+
$this->assertInstanceOf(MultiLineString::class, Geometry::fromWKB('010500000002000000010200000002000000000000000000f03f000000000000004000000000000008400000000000001040010200000002000000000000000000144000000000000018400000000000001c400000000000002040'));
48+
$this->assertInstanceOf(MultiPolygon::class, Geometry::fromWKB('01060000000200000001030000000100000004000000000000000000f03f00000000000000400000000000000840000000000000104000000000000014400000000000001840000000000000f03f000000000000004001030000000300000004000000000000000000f03f00000000000000400000000000000840000000000000104000000000000014400000000000001840000000000000f03f000000000000004004000000000000000000264000000000000028400000000000002a400000000000002c400000000000002e4000000000000030400000000000002640000000000000284004000000000000000000354000000000000036400000000000003740000000000000384000000000000039400000000000003a4000000000000035400000000000003640'));
49+
$this->assertInstanceOf(GeometryCollection::class, Geometry::fromWKB('0107000000010000000101000000000000000000f03f0000000000000040'));
50+
$this->assertInstanceOf(GeometryCollection::class, Geometry::fromWKB('0107000000020000000101000000000000000000f03f0000000000000040010200000002000000000000000000f03f000000000000004000000000000008400000000000001040'));
51+
52+
$prefix = "\0\0\0\0";
53+
$this->assertInstanceOf(Point::class, Geometry::fromWKB($prefix.'0101000000000000000000f03f0000000000000040'));
11754
}
11855
}

tests/Unit/Types/MultiLineStringTest.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ public function testFromWKT()
1616

1717
public function testToWKT()
1818
{
19-
$collection = new LineString(
20-
[
19+
$collection = new LineString([
2120
new Point(0, 0),
2221
new Point(0, 1),
2322
new Point(1, 1),
2423
new Point(1, 0),
2524
new Point(0, 0),
26-
]
27-
);
25+
]);
2826

2927
$multilinestring = new MultiLineString([$collection]);
3028

@@ -36,9 +34,21 @@ public function testJsonSerialize()
3634
$multilinestring = MultiLineString::fromWKT('MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))');
3735

3836
$this->assertInstanceOf(\GeoJson\Geometry\MultiLineString::class, $multilinestring->jsonSerialize());
39-
$this->assertSame(
40-
'{"type":"MultiLineString","coordinates":[[[0,0],[1,1],[1,2]],[[2,3],[3,2],[5,4]]]}',
41-
json_encode($multilinestring)
42-
);
37+
$this->assertSame('{"type":"MultiLineString","coordinates":[[[0,0],[1,1],[1,2]],[[2,3],[3,2],[5,4]]]}', json_encode($multilinestring));
38+
}
39+
40+
public function testInvalidArgumentExceptionAtLeastOneEntry()
41+
{
42+
$this->assertException(InvalidArgumentException::class);
43+
$multilinestring = new MultiLineString([]);
44+
}
45+
46+
public function testInvalidArgumentExceptionNotArrayOfLineString()
47+
{
48+
$this->assertException(InvalidArgumentException::class);
49+
$multilinestring = new MultiLineString([
50+
new LineString([new Point(0, 0), new Point(1, 1)]),
51+
new Point(0, 1),
52+
]);
4353
}
4454
}

0 commit comments

Comments
 (0)