Total Complexity | 29 |
Total Lines | 462 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
17 | class SpatialTraitTest extends BaseTestCase |
||
18 | { |
||
19 | /** |
||
20 | * @var TestModel |
||
21 | */ |
||
22 | protected $model; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $queries; |
||
28 | |||
29 | public function setUp() |
||
30 | { |
||
31 | $this->model = new TestModel(); |
||
32 | $this->queries = &$this->model->getConnection()->getPdo()->queries; |
||
33 | } |
||
34 | |||
35 | public function tearDown() |
||
36 | { |
||
37 | $this->model->getConnection()->getPdo()->resetQueries(); |
||
1 ignored issue
–
show
|
|||
38 | } |
||
39 | |||
40 | public function testInsertUpdatePointHasCorrectSql() |
||
41 | { |
||
42 | $this->assertFalse($this->model->exists); |
||
43 | |||
44 | $this->model->point = new Point([1, 2]); |
||
45 | $this->model->save(); |
||
46 | |||
47 | $this->assertStringStartsWith('insert', $this->queries[0]); |
||
48 | $this->assertContains("insert into `test_models` (`point`) values (ST_GeomFromText('POINT (1 2)'))", $this->queries[0]); |
||
49 | // TODO: assert bindings in query |
||
50 | $this->assertTrue($this->model->exists); |
||
51 | |||
52 | $this->model->point = new Point([1, 2]); |
||
53 | $this->model->save(); |
||
54 | |||
55 | $this->assertStringStartsWith('update', $this->queries[1]); |
||
56 | $this->assertContains("update `test_models` set `point` = ST_GeomFromText('POINT (1 2)') where `id` = ?", $this->queries[1]); |
||
57 | // TODO: assert bindings in query |
||
58 | } |
||
59 | |||
60 | public function testInsertUpdateLineStringHasCorrectSql() |
||
61 | { |
||
62 | $point1 = new Point([1, 2]); |
||
63 | $point2 = new Point([2, 3]); |
||
64 | |||
65 | $this->assertFalse($this->model->exists); |
||
66 | |||
67 | $this->model->linestring = new LineString([$point1, $point2]); |
||
68 | $this->model->save(); |
||
69 | |||
70 | $this->assertStringStartsWith('insert', $this->queries[0]); |
||
71 | $this->assertContains("insert into `test_models` (`linestring`) values (ST_GeomFromText('LINESTRING (1 2, 2 3)'))", $this->queries[0]); |
||
72 | // TODO: assert bindings in query |
||
73 | $this->assertTrue($this->model->exists); |
||
74 | |||
75 | $this->model->linestring = new LineString([$point1, $point2]); |
||
76 | $this->model->save(); |
||
77 | |||
78 | $this->assertStringStartsWith('update', $this->queries[1]); |
||
79 | $this->assertContains("update `test_models` set `linestring` = ST_GeomFromText('LINESTRING (1 2, 2 3)') where `id` = ?", $this->queries[1]); |
||
80 | // TODO: assert bindings in query |
||
81 | } |
||
82 | |||
83 | public function testInsertUpdatePolygonHasCorrectSql() |
||
84 | { |
||
85 | $point1 = new Point([1, 2]); |
||
86 | $point2 = new Point([2, 3]); |
||
87 | $point3 = new Point([3, 2]); |
||
88 | $point4 = new Point([2, 1]); |
||
89 | |||
90 | $this->assertFalse($this->model->exists); |
||
91 | |||
92 | $this->model->polygon = new Polygon([[$point1, $point2, $point3, $point4, $point1]]); |
||
93 | $this->model->save(); |
||
94 | |||
95 | $this->assertStringStartsWith('insert', $this->queries[0]); |
||
96 | $this->assertContains("insert into `test_models` (`polygon`) values (ST_GeomFromText('POLYGON ((1 2, 2 3, 3 2, 2 1, 1 2))'))", $this->queries[0]); |
||
97 | // TODO: assert bindings in query |
||
98 | $this->assertTrue($this->model->exists); |
||
99 | |||
100 | $this->model->polygon = new Polygon([[$point1, $point2, $point3, $point4, $point1]]); |
||
101 | $this->model->save(); |
||
102 | $this->assertStringStartsWith('update', $this->queries[1]); |
||
103 | $this->assertContains("update `test_models` set `polygon` = ST_GeomFromText('POLYGON ((1 2, 2 3, 3 2, 2 1, 1 2))') where `id` = ?", $this->queries[1]); |
||
104 | // TODO: assert bindings in query |
||
105 | } |
||
106 | |||
107 | public function testInsertUpdateMultiPointHasCorrectSql() |
||
108 | { |
||
109 | $point1 = new Point([1, 2]); |
||
110 | $point2 = new Point([2, 3]); |
||
111 | |||
112 | $this->assertFalse($this->model->exists); |
||
113 | |||
114 | $this->model->multipoint = new MultiPoint([$point1, $point2]); |
||
115 | $this->model->save(); |
||
116 | |||
117 | $this->assertStringStartsWith('insert', $this->queries[0]); |
||
118 | $this->assertContains("insert into `test_models` (`multipoint`) values (ST_GeomFromText('MULTIPOINT ((1 2), (2 3))'))", $this->queries[0]); |
||
119 | // TODO: assert bindings in query |
||
120 | $this->assertTrue($this->model->exists); |
||
121 | |||
122 | $this->model->multipoint = new MultiPoint([$point1, $point2]); |
||
123 | $this->model->save(); |
||
124 | |||
125 | $this->assertStringStartsWith('update', $this->queries[1]); |
||
126 | $this->assertContains("update `test_models` set `multipoint` = ST_GeomFromText('MULTIPOINT ((1 2), (2 3))') where `id` = ?", $this->queries[1]); |
||
127 | // TODO: assert bindings in query |
||
128 | } |
||
129 | |||
130 | public function testInsertUpdateMultiLineStringHasCorrectSql() |
||
131 | { |
||
132 | $point1 = new Point([1, 2]); |
||
133 | $point2 = new Point([2, 3]); |
||
134 | $linestring1 = new LineString([$point1, $point2]); |
||
135 | $point3 = new Point([3, 2]); |
||
136 | $point4 = new Point([2, 1]); |
||
137 | $linestring2 = new LineString([$point3, $point4]); |
||
138 | |||
139 | $this->assertFalse($this->model->exists); |
||
140 | |||
141 | $this->model->multilinestring = new MultiLineString([$linestring1, $linestring2]); |
||
142 | $this->model->save(); |
||
143 | |||
144 | $this->assertStringStartsWith('insert', $this->queries[0]); |
||
145 | $this->assertContains("insert into `test_models` (`multilinestring`) values (ST_GeomFromText('MULTILINESTRING ((1 2, 2 3), (3 2, 2 1))'))", $this->queries[0]); |
||
146 | // TODO: assert bindings in query |
||
147 | $this->assertTrue($this->model->exists); |
||
148 | |||
149 | $this->model->multilinestring = new MultiLineString([$linestring1, $linestring2]); |
||
150 | $this->model->save(); |
||
151 | $this->assertStringStartsWith('update', $this->queries[1]); |
||
152 | $this->assertContains("update `test_models` set `multilinestring` = ST_GeomFromText('MULTILINESTRING ((1 2, 2 3), (3 2, 2 1))') where `id` = ?", $this->queries[1]); |
||
153 | // TODO: assert bindings in query |
||
154 | } |
||
155 | |||
156 | public function testInsertUpdateMultiPolygonHasCorrectSql() |
||
157 | { |
||
158 | $point1 = new Point([1, 2]); |
||
159 | $point2 = new Point([2, 3]); |
||
160 | $point3 = new Point([3, 2]); |
||
161 | $point4 = new Point([2, 1]); |
||
162 | $point5 = new Point([4, 5]); |
||
163 | $point6 = new Point([5, 6]); |
||
164 | $point7 = new Point([6, 5]); |
||
165 | $point8 = new Point([5, 4]); |
||
166 | |||
167 | $polygon1 = new Polygon([[$point1, $point2, $point3, $point4, $point1]]); |
||
168 | $polygon2 = new Polygon([[$point5, $point6, $point7, $point8, $point5]]); |
||
169 | |||
170 | $this->assertFalse($this->model->exists); |
||
171 | |||
172 | $this->model->multipolygon = new MultiPolygon([$polygon1, $polygon2]); |
||
173 | $this->model->save(); |
||
174 | |||
175 | $this->assertStringStartsWith('insert', $this->queries[0]); |
||
176 | $this->assertContains("insert into `test_models` (`multipolygon`) values (ST_GeomFromText('MULTIPOLYGON (((1 2, 2 3, 3 2, 2 1, 1 2)), ((4 5, 5 6, 6 5, 5 4, 4 5)))'))", $this->queries[0]); |
||
177 | // TODO: assert bindings in query |
||
178 | $this->assertTrue($this->model->exists); |
||
179 | |||
180 | $this->model->multipolygon = new MultiPolygon([$polygon1, $polygon2]); |
||
181 | $this->model->save(); |
||
182 | $this->assertStringStartsWith('update', $this->queries[1]); |
||
183 | $this->assertContains("update `test_models` set `multipolygon` = ST_GeomFromText('MULTIPOLYGON (((1 2, 2 3, 3 2, 2 1, 1 2)), ((4 5, 5 6, 6 5, 5 4, 4 5)))') where `id` = ?", $this->queries[1]); |
||
184 | // TODO: assert bindings in query |
||
185 | } |
||
186 | |||
187 | public function testInsertUpdateGeometryCollectionHasCorrectSql() |
||
206 | // TODO: assert bindings in query |
||
207 | } |
||
208 | |||
209 | public function testSettingRawAttributes() |
||
210 | { |
||
211 | $attributes['point'] = pack('H*', '0101000000000000000000f03f0000000000000040'); |
||
1 ignored issue
–
show
|
|||
212 | |||
213 | $this->model->setRawAttributes($attributes); |
||
214 | $this->assertInstanceOf(Point::class, ($this->model->point)); |
||
215 | } |
||
216 | |||
217 | public function testSpatialFieldsNotDefinedException() |
||
218 | { |
||
219 | $model = new TestNoSpatialModel(); |
||
220 | $this->assertException(SpatialFieldsNotDefinedException::class); |
||
221 | $model->getSpatialFields(); |
||
222 | } |
||
223 | |||
224 | public function testScopeDistance() |
||
225 | { |
||
226 | $point = new Point([1, 2]); |
||
227 | $query = TestModel::distance('point', $point, 10); |
||
228 | |||
229 | $this->assertInstanceOf(Builder::class, $query); |
||
230 | $q = $query->getQuery(); |
||
231 | $this->assertNotEmpty($q->wheres); |
||
232 | $bindings = $q->getRawBindings()['where']; |
||
233 | $this->assertNotEmpty($bindings); |
||
234 | $this->assertEquals('ST_Distance(point, ST_GeomFromText(?)) <= ?', $q->wheres[0]['sql']); |
||
235 | $this->assertEquals('POINT (1 2)', $bindings[0]); |
||
236 | $this->assertEquals(10, $bindings[1]); |
||
237 | } |
||
238 | |||
239 | public function testScopeDistanceExcludingSelf() |
||
240 | { |
||
241 | $point = new Point([1, 2]); |
||
242 | $query = TestModel::distanceExcludingSelf('point', $point, 10); |
||
243 | |||
244 | $this->assertInstanceOf(Builder::class, $query); |
||
245 | $q = $query->getQuery(); |
||
246 | $this->assertNotEmpty($q->wheres); |
||
247 | $bindings = $q->getRawBindings()['where']; |
||
248 | $this->assertNotEmpty($bindings); |
||
249 | $this->assertEquals('ST_Distance(point, ST_GeomFromText(?)) <= ?', $q->wheres[0]['sql']); |
||
250 | $this->assertEquals('ST_Distance(point, ST_GeomFromText(?)) != 0', $q->wheres[1]['sql']); |
||
251 | $this->assertEquals('POINT (1 2)', $bindings[0]); |
||
252 | $this->assertEquals(10, $bindings[1]); |
||
253 | $this->assertEquals('POINT (1 2)', $bindings[2]); |
||
254 | } |
||
255 | |||
256 | public function testScopeDistanceSphere() |
||
257 | { |
||
258 | $point = new Point([1, 2]); |
||
259 | $query = TestModel::distanceSphere('point', $point, 10); |
||
260 | |||
261 | $this->assertInstanceOf(Builder::class, $query); |
||
262 | $q = $query->getQuery(); |
||
263 | $this->assertNotEmpty($q->wheres); |
||
264 | $bindings = $q->getRawBindings()['where']; |
||
265 | $this->assertNotEmpty($bindings); |
||
266 | $this->assertEquals('ST_Distance_Sphere(point, ST_GeomFromText(?)) <= ?', $q->wheres[0]['sql']); |
||
267 | $this->assertEquals('POINT (1 2)', $bindings[0]); |
||
268 | $this->assertEquals(10, $bindings[1]); |
||
269 | } |
||
270 | |||
271 | public function testScopeDistanceSphereExcludingSelf() |
||
272 | { |
||
273 | $point = new Point([1, 2]); |
||
274 | $query = TestModel::distanceSphereExcludingSelf('point', $point, 10); |
||
275 | |||
276 | $this->assertInstanceOf(Builder::class, $query); |
||
277 | $q = $query->getQuery(); |
||
278 | $this->assertNotEmpty($q->wheres); |
||
279 | $bindings = $q->getRawBindings()['where']; |
||
280 | $this->assertNotEmpty($bindings); |
||
281 | $this->assertEquals('ST_Distance_Sphere(point, ST_GeomFromText(?)) <= ?', $q->wheres[0]['sql']); |
||
282 | $this->assertEquals('ST_Distance_Sphere(point, ST_GeomFromText(?)) != 0', $q->wheres[1]['sql']); |
||
283 | $this->assertEquals('POINT (1 2)', $bindings[0]); |
||
284 | $this->assertEquals(10, $bindings[1]); |
||
285 | $this->assertEquals('POINT (1 2)', $bindings[2]); |
||
286 | } |
||
287 | |||
288 | public function testScopeDistanceValue() |
||
289 | { |
||
290 | $point = new Point([1, 2]); |
||
291 | $query = TestModel::distanceValue('point', $point); |
||
292 | |||
293 | $this->assertInstanceOf(Builder::class, $query); |
||
294 | $q = $query->getQuery(); |
||
295 | $this->assertNotEmpty($q->columns); |
||
296 | $bindings = $q->getRawBindings()['select']; |
||
297 | $this->assertNotEmpty($bindings); |
||
298 | $this->assertEquals('*', $q->columns[0]); |
||
299 | $this->assertInstanceOf(\Illuminate\Database\Query\Expression::class, $q->columns[1]); |
||
300 | $this->assertEquals('ST_Distance(point, ST_GeomFromText(?)) as distance', $q->columns[1]->getValue()); |
||
301 | $this->assertEquals('POINT (1 2)', $bindings[0]); |
||
302 | } |
||
303 | |||
304 | public function testScopeDistanceValueWithSelect() |
||
305 | { |
||
306 | $point = new Point([1, 2]); |
||
307 | $query = TestModel::select('some_column')->distanceValue('point', $point); |
||
1 ignored issue
–
show
|
|||
308 | |||
309 | $this->assertInstanceOf(Builder::class, $query); |
||
310 | $q = $query->getQuery(); |
||
311 | $this->assertNotEmpty($q->columns); |
||
312 | $bindings = $q->getRawBindings()['select']; |
||
313 | $this->assertNotEmpty($bindings); |
||
314 | $this->assertEquals('some_column', $q->columns[0]); |
||
315 | $this->assertInstanceOf(\Illuminate\Database\Query\Expression::class, $q->columns[1]); |
||
316 | $this->assertEquals('ST_Distance(point, ST_GeomFromText(?)) as distance', $q->columns[1]->getValue()); |
||
317 | $this->assertEquals('POINT (1 2)', $bindings[0]); |
||
318 | } |
||
319 | |||
320 | public function testScopeDistanceSphereValue() |
||
321 | { |
||
322 | $point = new Point([1, 2]); |
||
323 | $query = TestModel::distanceSphereValue('point', $point); |
||
324 | |||
325 | $this->assertInstanceOf(Builder::class, $query); |
||
326 | $q = $query->getQuery(); |
||
327 | $this->assertNotEmpty($q->columns); |
||
328 | $bindings = $q->getRawBindings()['select']; |
||
329 | $this->assertNotEmpty($bindings); |
||
330 | $this->assertEquals('*', $q->columns[0]); |
||
331 | $this->assertInstanceOf(\Illuminate\Database\Query\Expression::class, $q->columns[1]); |
||
332 | $this->assertEquals('ST_Distance_Sphere(point, ST_GeomFromText(?)) as distance', $q->columns[1]->getValue()); |
||
333 | $this->assertEquals('POINT (1 2)', $bindings[0]); |
||
334 | } |
||
335 | |||
336 | public function testScopeDistanceSphereValueWithSelect() |
||
337 | { |
||
338 | $point = new Point([1, 2]); |
||
339 | $query = TestModel::select('some_column')->distanceSphereValue('point', $point); |
||
1 ignored issue
–
show
|
|||
340 | |||
341 | $this->assertInstanceOf(Builder::class, $query); |
||
342 | $q = $query->getQuery(); |
||
343 | $this->assertNotEmpty($q->columns); |
||
344 | $bindings = $q->getRawBindings()['select']; |
||
345 | $this->assertNotEmpty($bindings); |
||
346 | $this->assertEquals('some_column', $q->columns[0]); |
||
347 | $this->assertInstanceOf(\Illuminate\Database\Query\Expression::class, $q->columns[1]); |
||
348 | $this->assertEquals('ST_Distance_Sphere(point, ST_GeomFromText(?)) as distance', $q->columns[1]->getValue()); |
||
349 | $this->assertEquals('POINT (1 2)', $bindings[0]); |
||
350 | } |
||
351 | |||
352 | private function buildTestPolygon() |
||
353 | { |
||
354 | $point1 = new Point([1, 1]); |
||
355 | $point2 = new Point([1, 2]); |
||
356 | $point3 = new Point([1, 2]); |
||
357 | $point4 = new Point([2, 2]); |
||
358 | $point5 = new Point([2, 2]); |
||
359 | $point6 = new Point([1, 1]); |
||
360 | |||
361 | return new Polygon([[$point1, $point2, $point3, $point4, $point5, $point6]]); |
||
362 | } |
||
363 | |||
364 | public function testScopeComparison() |
||
375 | } |
||
376 | |||
377 | public function testScopeWithin() |
||
378 | { |
||
379 | $query = TestModel::within('point', $this->buildTestPolygon()); |
||
380 | |||
381 | $this->assertInstanceOf(Builder::class, $query); |
||
382 | $q = $query->getQuery(); |
||
383 | $this->assertNotEmpty($q->wheres); |
||
384 | $bindings = $q->getRawBindings()['where']; |
||
385 | $this->assertNotEmpty($bindings); |
||
386 | $this->assertContains('ST_Within(`point`, ST_GeomFromText(?))', $q->wheres[0]['sql']); |
||
387 | $this->assertEquals('POLYGON ((1 1, 1 2, 1 2, 2 2, 2 2, 1 1))', $bindings[0]); |
||
388 | } |
||
389 | |||
390 | public function testScopeCrosses() |
||
391 | { |
||
392 | $query = TestModel::crosses('point', $this->buildTestPolygon()); |
||
393 | |||
394 | $this->assertInstanceOf(Builder::class, $query); |
||
395 | $q = $query->getQuery(); |
||
396 | $this->assertNotEmpty($q->wheres); |
||
397 | $bindings = $q->getRawBindings()['where']; |
||
398 | $this->assertNotEmpty($bindings); |
||
399 | $this->assertContains('ST_Crosses(`point`, ST_GeomFromText(?))', $q->wheres[0]['sql']); |
||
400 | $this->assertEquals('POLYGON ((1 1, 1 2, 1 2, 2 2, 2 2, 1 1))', $bindings[0]); |
||
401 | } |
||
402 | |||
403 | public function testScopeContains() |
||
404 | { |
||
405 | $query = TestModel::contains('point', $this->buildTestPolygon()); |
||
406 | |||
407 | $this->assertInstanceOf(Builder::class, $query); |
||
408 | $q = $query->getQuery(); |
||
409 | $this->assertNotEmpty($q->wheres); |
||
410 | $bindings = $q->getRawBindings()['where']; |
||
411 | $this->assertNotEmpty($bindings); |
||
412 | $this->assertContains('ST_Contains(`point`, ST_GeomFromText(?))', $q->wheres[0]['sql']); |
||
413 | $this->assertEquals('POLYGON ((1 1, 1 2, 1 2, 2 2, 2 2, 1 1))', $bindings[0]); |
||
414 | } |
||
415 | |||
416 | public function testScopeDisjoint() |
||
417 | { |
||
418 | $query = TestModel::disjoint('point', $this->buildTestPolygon()); |
||
419 | |||
420 | $this->assertInstanceOf(Builder::class, $query); |
||
421 | $q = $query->getQuery(); |
||
422 | $this->assertNotEmpty($q->wheres); |
||
423 | $bindings = $q->getRawBindings()['where']; |
||
424 | $this->assertNotEmpty($bindings); |
||
425 | $this->assertContains('ST_Disjoint(`point`, ST_GeomFromText(?))', $q->wheres[0]['sql']); |
||
426 | $this->assertEquals('POLYGON ((1 1, 1 2, 1 2, 2 2, 2 2, 1 1))', $bindings[0]); |
||
427 | } |
||
428 | |||
429 | public function testScopeEquals() |
||
430 | { |
||
431 | $query = TestModel::equals('point', $this->buildTestPolygon()); |
||
432 | |||
433 | $this->assertInstanceOf(Builder::class, $query); |
||
434 | $q = $query->getQuery(); |
||
435 | $this->assertNotEmpty($q->wheres); |
||
436 | $bindings = $q->getRawBindings()['where']; |
||
437 | $this->assertNotEmpty($bindings); |
||
438 | $this->assertContains('ST_Equals(`point`, ST_GeomFromText(?))', $q->wheres[0]['sql']); |
||
439 | $this->assertEquals('POLYGON ((1 1, 1 2, 1 2, 2 2, 2 2, 1 1))', $bindings[0]); |
||
440 | } |
||
441 | |||
442 | public function testScopeIntersects() |
||
453 | } |
||
454 | |||
455 | public function testScopeOverlaps() |
||
456 | { |
||
457 | $query = TestModel::overlaps('point', $this->buildTestPolygon()); |
||
458 | |||
459 | $this->assertInstanceOf(Builder::class, $query); |
||
460 | $q = $query->getQuery(); |
||
461 | $this->assertNotEmpty($q->wheres); |
||
462 | $bindings = $q->getRawBindings()['where']; |
||
463 | $this->assertNotEmpty($bindings); |
||
464 | $this->assertContains('ST_Overlaps(`point`, ST_GeomFromText(?))', $q->wheres[0]['sql']); |
||
465 | $this->assertEquals('POLYGON ((1 1, 1 2, 1 2, 2 2, 2 2, 1 1))', $bindings[0]); |
||
466 | } |
||
467 | |||
468 | public function testScopeDoesTouch() |
||
479 | } |
||
480 | } |
||
481 | |||
482 | class TestModel extends Model |
||
483 | { |
||
484 | use SpatialTrait; |
||
1 ignored issue
–
show
|
|||
485 | |||
486 | protected $spatialFields = [ // TODO: only required when fetching, not saving |
||
487 | 'point', |
||
488 | 'linestring', |
||
489 | 'polygon', |
||
490 | 'multipoint', |
||
491 | 'multilinestring', |
||
492 | 'multipolygon', |
||
493 | 'geometrycollection', |
||
567 |