|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -namespace Grimzy\LaravelMysqlSpatial\Tests\Unit\Eloquent; |
| 3 | +namespace Grimzy\LaravelMysqlSpatial\Tests\Integration; |
4 | 4 |
|
5 | 5 | use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;
|
6 | 6 | use Grimzy\LaravelMysqlSpatial\Exceptions\SpatialFieldsNotDefinedException;
|
7 |
| -use Grimzy\LaravelMysqlSpatial\Tests\Unit\BaseTestCase; |
| 7 | +use Grimzy\LaravelMysqlSpatial\Tests\Integration\Eloquent\TestModel; |
8 | 8 | use Grimzy\LaravelMysqlSpatial\Types\Point;
|
9 | 9 | use Illuminate\Database\Eloquent\Model;
|
| 10 | +use Illuminate\Database\Events\QueryExecuted; |
10 | 11 | use Illuminate\Support\Facades\DB;
|
11 | 12 | use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
12 | 13 |
|
13 |
| -class SpatialTraitTest extends BaseTestCase |
| 14 | +class SpatialTraitTest extends IntegrationBaseCase |
14 | 15 | {
|
15 | 16 | use MockeryPHPUnitIntegration;
|
16 | 17 |
|
17 | 18 | protected TestModel $model;
|
18 | 19 |
|
19 |
| - protected array $queries; |
| 20 | + protected array $queries = []; |
20 | 21 |
|
21 | 22 | public function setUp(): void
|
22 | 23 | {
|
23 | 24 | parent::setUp();
|
24 | 25 |
|
25 | 26 | $this->model = new TestModel();
|
26 |
| - $this->queries = &$this->model->getConnection()->getPdo()->queries; |
| 27 | + DB::listen(function (QueryExecuted $query) { |
| 28 | + $this->queries[] = $query->sql; |
| 29 | + }); |
27 | 30 | }
|
28 | 31 |
|
29 | 32 | public function tearDown(): void
|
30 | 33 | {
|
31 |
| - $this->model->getConnection()->getPdo()->resetQueries(); |
| 34 | + $this->queries = []; |
32 | 35 | }
|
33 | 36 |
|
34 | 37 | public function testInsertUpdatePointHasCorrectSql()
|
@@ -78,23 +81,30 @@ public function testInsertUpdatePolygonHasCorrectSql()
|
78 | 81 | {
|
79 | 82 | $point1 = new Point(1, 2);
|
80 | 83 | $point2 = new Point(2, 3);
|
81 |
| - $linestring1 = new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point1, $point2]); |
82 | 84 | $point3 = new Point(3, 2);
|
83 | 85 | $point4 = new Point(2, 1);
|
84 |
| - $linestring2 = new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point3, $point4]); |
| 86 | + |
| 87 | + $polygon = new \Grimzy\LaravelMysqlSpatial\Types\Polygon([ |
| 88 | + new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point1, $point2, $point3, $point4, $point1]), |
| 89 | + ]); |
85 | 90 |
|
86 | 91 | $this->assertFalse($this->model->exists);
|
87 | 92 |
|
88 |
| - $this->model->polygon = new \Grimzy\LaravelMysqlSpatial\Types\Polygon([$linestring1, $linestring2]); |
| 93 | + $this->model->polygon = $polygon; |
89 | 94 | $this->model->save();
|
90 | 95 |
|
91 | 96 | $this->assertStringStartsWith('insert', $this->queries[0]);
|
92 | 97 | $this->assertStringContainsString('insert into `test_models` (`polygon`) values (ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $this->queries[0]);
|
93 | 98 | // TODO: assert bindings in query
|
94 | 99 | $this->assertTrue($this->model->exists);
|
95 | 100 |
|
96 |
| - $this->model->polygon = new \Grimzy\LaravelMysqlSpatial\Types\Polygon([$linestring1, $linestring2]); |
| 101 | + $polygon = new \Grimzy\LaravelMysqlSpatial\Types\Polygon([ |
| 102 | + new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point1, $point2, $point3, $point4, $point1]), |
| 103 | + ]); |
| 104 | + |
| 105 | + $this->model->polygon = $polygon; |
97 | 106 | $this->model->save();
|
| 107 | + |
98 | 108 | $this->assertStringStartsWith('update', $this->queries[1]);
|
99 | 109 | $this->assertStringContainsString('update `test_models` set `polygon` = ST_GeomFromText(?, ?, \'axis-order=long-lat\') where `id` = ?', $this->queries[1]);
|
100 | 110 | // TODO: assert bindings in query
|
@@ -153,19 +163,19 @@ public function testInsertUpdateMultiPolygonHasCorrectSql()
|
153 | 163 | {
|
154 | 164 | $point1 = new Point(1, 2);
|
155 | 165 | $point2 = new Point(2, 3);
|
156 |
| - $linestring1 = new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point1, $point2]); |
157 | 166 | $point3 = new Point(3, 2);
|
158 | 167 | $point4 = new Point(2, 1);
|
159 |
| - $linestring2 = new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point3, $point4]); |
160 |
| - $polygon1 = new \Grimzy\LaravelMysqlSpatial\Types\Polygon([$linestring1, $linestring2]); |
| 168 | + $polygon1 = new \Grimzy\LaravelMysqlSpatial\Types\Polygon([ |
| 169 | + new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point1, $point2, $point3, $point1]), |
| 170 | + ]); |
161 | 171 |
|
162 | 172 | $point5 = new Point(4, 5);
|
163 | 173 | $point6 = new Point(5, 6);
|
164 |
| - $linestring3 = new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point5, $point6]); |
165 | 174 | $point7 = new Point(6, 5);
|
166 | 175 | $point8 = new Point(5, 4);
|
167 |
| - $linestring4 = new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point7, $point8]); |
168 |
| - $polygon2 = new \Grimzy\LaravelMysqlSpatial\Types\Polygon([$linestring3, $linestring4]); |
| 176 | + $polygon2 = new \Grimzy\LaravelMysqlSpatial\Types\Polygon([ |
| 177 | + new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point5, $point6, $point7, $point5]), |
| 178 | + ]); |
169 | 179 |
|
170 | 180 | $this->assertFalse($this->model->exists);
|
171 | 181 |
|
|
0 commit comments