|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use GeoJson\Geometry\GeometryCollection; |
|
4
|
|
|
use GeoJson\Geometry\LineString; |
|
5
|
|
|
use GeoJson\Geometry\MultiLineString; |
|
6
|
|
|
use GeoJson\Geometry\MultiPoint; |
|
7
|
|
|
use GeoJson\Geometry\MultiPolygon; |
|
8
|
|
|
use GeoJson\Geometry\Polygon; |
|
9
|
|
|
use GeoJson\Geometry\Point; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
11
|
|
|
use LaravelSpatial\Eloquent\Builder; |
|
12
|
|
|
use LaravelSpatial\Eloquent\SpatialTrait; |
|
13
|
|
|
use LaravelSpatial\Exceptions\SpatialFieldsNotDefinedException; |
|
14
|
|
|
use LaravelSpatial\MysqlConnection; |
|
15
|
|
|
// use Mockery; |
|
16
|
|
|
|
|
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(); |
|
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() |
|
188
|
|
|
{ |
|
189
|
|
|
$point1 = new Point([1, 2]); |
|
190
|
|
|
$linestring1 = new LineString([[2, 3], [3, 3]]); |
|
191
|
|
|
|
|
192
|
|
|
$this->assertFalse($this->model->exists); |
|
193
|
|
|
|
|
194
|
|
|
$this->model->geometrycollection = new GeometryCollection([$point1, $linestring1]); |
|
|
|
|
|
|
195
|
|
|
$this->model->save(); |
|
196
|
|
|
|
|
197
|
|
|
$this->assertStringStartsWith('insert', $this->queries[0]); |
|
198
|
|
|
$this->assertContains("insert into `test_models` (`geometrycollection`) values (ST_GeomFromText('GEOMETRYCOLLECTION (POINT (1 2), LINESTRING (2 3, 3 3))'))", $this->queries[0]); |
|
199
|
|
|
// TODO: assert bindings in query |
|
200
|
|
|
$this->assertTrue($this->model->exists); |
|
201
|
|
|
|
|
202
|
|
|
$this->model->geometrycollection = new GeometryCollection([$point1, $linestring1]); |
|
203
|
|
|
$this->model->save(); |
|
204
|
|
|
$this->assertStringStartsWith('update', $this->queries[1]); |
|
205
|
|
|
$this->assertContains("update `test_models` set `geometrycollection` = ST_GeomFromText('GEOMETRYCOLLECTION (POINT (1 2), LINESTRING (2 3, 3 3))') where `id` = ?", $this->queries[1]); |
|
206
|
|
|
// TODO: assert bindings in query |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
public function testSettingRawAttributes() |
|
210
|
|
|
{ |
|
211
|
|
|
$attributes['point'] = pack('H*', '0101000000000000000000f03f0000000000000040'); |
|
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); |
|
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); |
|
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() |
|
365
|
|
|
{ |
|
366
|
|
|
$query = TestModel::comparison('point', $this->buildTestPolygon(), 'within'); |
|
367
|
|
|
|
|
368
|
|
|
$this->assertInstanceOf(Builder::class, $query); |
|
369
|
|
|
$q = $query->getQuery(); |
|
370
|
|
|
$this->assertNotEmpty($q->wheres); |
|
371
|
|
|
$bindings = $q->getRawBindings()['where']; |
|
372
|
|
|
$this->assertNotEmpty($bindings); |
|
373
|
|
|
$this->assertContains('ST_Within(`point`, ST_GeomFromText(?))', $q->wheres[0]['sql']); |
|
374
|
|
|
$this->assertEquals('POLYGON ((1 1, 1 2, 1 2, 2 2, 2 2, 1 1))', $bindings[0]); |
|
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() |
|
443
|
|
|
{ |
|
444
|
|
|
$query = TestModel::intersects('point', $this->buildTestPolygon()); |
|
445
|
|
|
|
|
446
|
|
|
$this->assertInstanceOf(Builder::class, $query); |
|
447
|
|
|
$q = $query->getQuery(); |
|
448
|
|
|
$this->assertNotEmpty($q->wheres); |
|
449
|
|
|
$bindings = $q->getRawBindings()['where']; |
|
450
|
|
|
$this->assertNotEmpty($bindings); |
|
451
|
|
|
$this->assertContains('ST_Intersects(`point`, ST_GeomFromText(?))', $q->wheres[0]['sql']); |
|
452
|
|
|
$this->assertEquals('POLYGON ((1 1, 1 2, 1 2, 2 2, 2 2, 1 1))', $bindings[0]); |
|
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() |
|
469
|
|
|
{ |
|
470
|
|
|
$query = TestModel::doesTouch('point', $this->buildTestPolygon()); |
|
471
|
|
|
|
|
472
|
|
|
$this->assertInstanceOf(Builder::class, $query); |
|
473
|
|
|
$q = $query->getQuery(); |
|
474
|
|
|
$this->assertNotEmpty($q->wheres); |
|
475
|
|
|
$bindings = $q->getRawBindings()['where']; |
|
476
|
|
|
$this->assertNotEmpty($bindings); |
|
477
|
|
|
$this->assertContains('ST_Touches(`point`, ST_GeomFromText(?))', $q->wheres[0]['sql']); |
|
478
|
|
|
$this->assertEquals('POLYGON ((1 1, 1 2, 1 2, 2 2, 2 2, 1 1))', $bindings[0]); |
|
479
|
|
|
} |
|
480
|
|
|
} |
|
481
|
|
|
|
|
482
|
|
|
/** |
|
483
|
|
|
* Test Model |
|
484
|
|
|
* |
|
485
|
|
|
* @property $point |
|
486
|
|
|
* @property $linestring |
|
487
|
|
|
* @property $polygon |
|
488
|
|
|
* @property $multipoint |
|
489
|
|
|
* @property $multilinestring |
|
490
|
|
|
* @property $multipolygon |
|
491
|
|
|
* @property $geometrycollection |
|
492
|
|
|
*/ |
|
493
|
|
|
class TestModel extends Model |
|
494
|
|
|
{ |
|
495
|
|
|
use SpatialTrait; |
|
496
|
|
|
|
|
497
|
|
|
protected $spatialFields = [ |
|
498
|
|
|
'point', |
|
499
|
|
|
'linestring', |
|
500
|
|
|
'polygon', |
|
501
|
|
|
'multipoint', |
|
502
|
|
|
'multilinestring', |
|
503
|
|
|
'multipolygon', |
|
504
|
|
|
'geometrycollection', |
|
505
|
|
|
]; |
|
506
|
|
|
|
|
507
|
|
|
public $timestamps = false; |
|
508
|
|
|
|
|
509
|
|
|
public static $pdo; |
|
510
|
|
|
|
|
511
|
|
|
public static function resolveConnection($connection = null) |
|
512
|
|
|
{ |
|
513
|
|
|
if (is_null(static::$pdo)) { |
|
514
|
|
|
static::$pdo = Mockery::mock('TestPDO')->makePartial(); |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
|
|
return new MysqlConnection(static::$pdo); |
|
518
|
|
|
} |
|
519
|
|
|
|
|
520
|
|
|
public function testrelatedmodels() |
|
521
|
|
|
{ |
|
522
|
|
|
return $this->hasMany(TestRelatedModel::class); |
|
523
|
|
|
} |
|
524
|
|
|
|
|
525
|
|
|
public function testrelatedmodels2() |
|
526
|
|
|
{ |
|
527
|
|
|
return $this->belongsToMany(TestRelatedModel::class); |
|
528
|
|
|
} |
|
529
|
|
|
} |
|
530
|
|
|
|
|
531
|
|
|
class TestRelatedModel extends TestModel |
|
532
|
|
|
{ |
|
533
|
|
|
public function testmodel() |
|
534
|
|
|
{ |
|
535
|
|
|
return $this->belongsTo(TestModel::class); |
|
536
|
|
|
} |
|
537
|
|
|
|
|
538
|
|
|
public function testmodels() |
|
539
|
|
|
{ |
|
540
|
|
|
return $this->belongsToMany(TestModel::class); |
|
541
|
|
|
} |
|
542
|
|
|
} |
|
543
|
|
|
|
|
544
|
|
|
class TestNoSpatialModel extends Model |
|
545
|
|
|
{ |
|
546
|
|
|
use SpatialTrait; |
|
547
|
|
|
} |
|
548
|
|
|
|
|
549
|
|
|
class TestPDO extends PDO |
|
550
|
|
|
{ |
|
551
|
|
|
public $queries = []; |
|
552
|
|
|
|
|
553
|
|
|
public $counter = 1; |
|
554
|
|
|
|
|
555
|
|
|
public function prepare($statement, $driver_options = []) |
|
556
|
|
|
{ |
|
557
|
|
|
$this->queries[] = $statement; |
|
558
|
|
|
|
|
559
|
|
|
$stmt = Mockery::mock('PDOStatement'); |
|
560
|
|
|
$stmt->shouldReceive('bindValue')->zeroOrMoreTimes(); |
|
561
|
|
|
$stmt->shouldReceive('execute'); |
|
562
|
|
|
$stmt->shouldReceive('fetchAll')->andReturn([['id' => 1, 'point' => 'POINT(1 2)']]); |
|
563
|
|
|
$stmt->shouldReceive('rowCount')->andReturn(1); |
|
564
|
|
|
|
|
565
|
|
|
return $stmt; |
|
566
|
|
|
} |
|
567
|
|
|
|
|
568
|
|
|
public function lastInsertId($name = null) |
|
569
|
|
|
{ |
|
570
|
|
|
return $this->counter++; |
|
571
|
|
|
} |
|
572
|
|
|
|
|
573
|
|
|
public function resetQueries() |
|
574
|
|
|
{ |
|
575
|
|
|
$this->queries = []; |
|
576
|
|
|
} |
|
577
|
|
|
} |
|
578
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.