1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Eloquent; |
4
|
|
|
|
5
|
|
|
use BaseTestCase; |
6
|
|
|
use GeoJson\Geometry\Point; |
7
|
|
|
use GeoJson\Geometry\LineString; |
8
|
|
|
use GeoJson\Geometry\Polygon; |
9
|
|
|
use LaravelSpatial\Eloquent\Builder; |
10
|
|
|
use LaravelSpatial\Eloquent\SpatialTrait; |
11
|
|
|
use LaravelSpatial\MysqlConnection; |
12
|
|
|
use Illuminate\Database\Eloquent\Model; |
13
|
|
|
use Illuminate\Database\Query\Builder as QueryBuilder; |
14
|
|
|
use Illuminate\Database\Query\Expression; |
15
|
|
|
use Illuminate\Database\Query\Grammars\MySqlGrammar; |
16
|
|
|
use Mockery; |
17
|
|
|
|
18
|
|
|
class BuilderTest extends BaseTestCase |
19
|
|
|
{ |
20
|
|
|
protected $builder; |
21
|
|
|
protected $queryBuilder; |
22
|
|
|
|
23
|
|
|
protected function setUp() |
24
|
|
|
{ |
25
|
|
|
$connection = Mockery::mock(MysqlConnection::class)->makePartial(); |
26
|
|
|
$grammar = Mockery::mock(MySqlGrammar::class)->makePartial(); |
27
|
|
|
$this->queryBuilder = Mockery::mock(QueryBuilder::class, [$connection, $grammar]); |
28
|
|
|
|
29
|
|
|
$this->queryBuilder |
30
|
|
|
->shouldReceive('from') |
31
|
|
|
->once() |
32
|
|
|
->andReturn($this->queryBuilder); |
33
|
|
|
|
34
|
|
|
$this->queryBuilder |
35
|
|
|
->shouldReceive('take') |
36
|
|
|
->with(1) |
37
|
|
|
->andReturn($this->queryBuilder); |
38
|
|
|
|
39
|
|
|
$this->queryBuilder |
40
|
|
|
->shouldReceive('get') |
41
|
|
|
->andReturn([]); |
42
|
|
|
|
43
|
|
|
$this->builder = new Builder($this->queryBuilder); |
44
|
|
|
$this->builder->setModel(new TestBuilderModel()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testUpdatePoint() |
48
|
|
|
{ |
49
|
|
|
$point = new Point([1, 2]); |
50
|
|
|
|
51
|
|
|
$this->queryBuilder |
52
|
|
|
->shouldReceive('raw') |
53
|
|
|
->with("ST_GeomFromText('POINT (1 2)')") |
54
|
|
|
->andReturn(new Expression("ST_GeomFromText('POINT (1 2)')")); |
55
|
|
|
|
56
|
|
|
$this->queryBuilder |
57
|
|
|
->shouldReceive('update') |
58
|
|
|
->andReturn(1); |
59
|
|
|
|
60
|
|
|
$this->builder->update(['point' => $point]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testUpdateLinestring() |
64
|
|
|
{ |
65
|
|
|
$linestring = new LineString([new Point([0, 0]), new Point([1, 1]), new Point([2, 2])]); |
66
|
|
|
|
67
|
|
|
$this->queryBuilder |
68
|
|
|
->shouldReceive('raw') |
69
|
|
|
->with("ST_GeomFromText('LINESTRING (0 0, 1 1, 2 2)')") |
70
|
|
|
->andReturn(new Expression("ST_GeomFromText('LINESTRING (0 0, 1 1, 2 2)')")); |
71
|
|
|
|
72
|
|
|
$this->queryBuilder |
73
|
|
|
->shouldReceive('update') |
74
|
|
|
->andReturn(1); |
75
|
|
|
|
76
|
|
|
$this->builder->update(['linestring' => $linestring]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testUpdatePolygon() |
80
|
|
|
{ |
81
|
|
|
$polygon = new Polygon([[new Point([0, 0]), new Point([0, 1]), new Point([1, 1]), new Point([0, 0])]]); |
82
|
|
|
|
83
|
|
|
$this->queryBuilder |
84
|
|
|
->shouldReceive('raw') |
85
|
|
|
->with("ST_GeomFromText('POLYGON ((0 0, 0 1, 1 1, 0 0))')") |
86
|
|
|
->andReturn(new Expression("ST_GeomFromText('POLYGON ((0 0, 0 1, 1 1, 0 0))')")); |
87
|
|
|
|
88
|
|
|
$this->queryBuilder |
89
|
|
|
->shouldReceive('update') |
90
|
|
|
->andReturn(1); |
91
|
|
|
|
92
|
|
|
$this->builder->update(['polygon' => $polygon]); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
class TestBuilderModel extends Model |
97
|
|
|
{ |
98
|
|
|
use SpatialTrait; |
99
|
|
|
|
100
|
|
|
public $timestamps = false; |
101
|
|
|
protected $spatialFields = ['point', 'linestring', 'polygon']; |
102
|
|
|
} |
103
|
|
|
|