99use Grimzy \LaravelSpatial \Types \Polygon ;
1010use Illuminate \Database \Eloquent \Model ;
1111use Illuminate \Database \Query \Builder as QueryBuilder ;
12- use Illuminate \Database \Query \Expression ;
12+ use Illuminate \Database \Query \Grammars \ MySqlGrammar ;
1313use Mockery ;
1414
1515class BuilderTest extends BaseTestCase
1616{
1717 protected $ builder ;
18-
19- /**
20- * @var \Mockery\MockInterface $queryBuilder
21- */
2218 protected $ queryBuilder ;
2319
2420 protected function setUp ()
2521 {
2622 $ connection = Mockery::mock (MysqlConnection::class)->makePartial ();
27- $ this ->queryBuilder = Mockery::mock (QueryBuilder::class, [$ connection ])->makePartial ();
23+ $ grammar = Mockery::mock (MySqlGrammar::class)->makePartial ();
24+ $ this ->queryBuilder = Mockery::mock (QueryBuilder::class, [$ connection , $ grammar ]);
2825
2926 $ this ->queryBuilder
3027 ->shouldReceive ('from ' )
28+ ->once ()
3129 ->andReturn ($ this ->queryBuilder );
3230
33- $ this ->queryBuilder
34- ->shouldReceive ('take ' )
35- ->with (1 )
36- ->andReturn ($ this ->queryBuilder );
37-
38- $ this ->queryBuilder
39- ->shouldReceive ('get ' )
40- ->andReturn ([]);
41-
4231 $ this ->builder = new Builder ($ this ->queryBuilder );
4332 $ this ->builder ->setModel (new TestBuilderModel ());
4433 }
4534
46- public function testUpdate ()
35+ public function testUpdatePoint ()
4736 {
4837 $ this ->queryBuilder
4938 ->shouldReceive ('raw ' )
5039 ->with ("GeomFromText('POINT(2 1)') " )
51- ->andReturn ( new Expression ( " GeomFromText('POINT(2 1)') " ) );
40+ ->once ( );
5241
5342 $ this ->queryBuilder
54- ->shouldReceive ('update ' );
55-
56- $ builder = Mockery::mock (Builder::class, [$ this ->queryBuilder ])->makePartial ();
57- $ builder ->shouldAllowMockingProtectedMethods ();
58- $ builder
59- ->shouldReceive ('addUpdatedAtColumn ' )
60- ->andReturn (['point ' => new Point (1 , 2 )]);
43+ ->shouldReceive ('update ' )
44+ ->once ();
6145
62- $ builder ->update (['point ' => new Point (1 , 3 )]);
46+ $ this -> builder ->update (['point ' => new Point (1 , 2 )]);
6347 }
6448
6549 public function testUpdateLinestring ()
6650 {
6751 $ this ->queryBuilder
6852 ->shouldReceive ('raw ' )
69- ->with ("ST_GeogFromText ('LINESTRING(0 0, 1 1, 2 2)') " )
70- ->andReturn ( new Expression ( " ST_GeogFromText('LINESTRING(0 0, 1 1, 2 2)') " ) );
53+ ->with ("GeomFromText ('LINESTRING(0 0,1 1,2 2)') " )
54+ ->once ( );
7155
7256 $ this ->queryBuilder
7357 ->shouldReceive ('update ' )
74- ->andReturn ( 1 );
58+ ->once ( );
7559
7660 $ linestring = new LineString ([new Point (0 , 0 ), new Point (1 , 1 ), new Point (2 , 2 )]);
7761
78- $ builder = Mockery::mock (Builder::class, [$ this ->queryBuilder ])->makePartial ();
79- $ builder ->shouldAllowMockingProtectedMethods ();
80- $ builder
81- ->shouldReceive ('addUpdatedAtColumn ' )
82- ->andReturn (['linestring ' => $ linestring ]);
62+ $ this ->builder ->update (['linestring ' => $ linestring ]);
63+ }
64+
65+ public function testUpdatePolygon ()
66+ {
67+ $ this ->queryBuilder
68+ ->shouldReceive ('raw ' )
69+ ->with ("GeomFromText('POLYGON((0 0,1 0),(1 0,1 1),(1 1,0 0))') " )
70+ ->once ();
71+
72+ $ this ->queryBuilder
73+ ->shouldReceive ('update ' )
74+ ->once ();
8375
84- $ builder
85- ->shouldReceive ('asWKT ' )->with ($ linestring )->once ();
76+ $ linestrings [] = new LineString ([new Point (0 , 0 ), new Point (0 , 1 )]);
77+ $ linestrings [] = new LineString ([new Point (0 , 1 ), new Point (1 , 1 )]);
78+ $ linestrings [] = new LineString ([new Point (1 , 1 ), new Point (0 , 0 )]);
79+ $ polygon = new Polygon ($ linestrings );
8680
87- $ builder ->update (['linestring ' => $ linestring ]);
81+ $ this -> builder ->update (['polygon ' => $ polygon ]);
8882 }
8983}
9084
9185class TestBuilderModel extends Model
9286{
9387 use SpatialTrait;
9488
95- protected $ spatialFields = [
96- 'point ' => Point::class,
97- 'linestring ' => LineString::class,
98- 'polygon ' => Polygon::class
99- ];
89+ public $ timestamps = false ;
90+ protected $ spatialFields = ['point ' , 'linestring ' , 'polygon ' ];
10091}
0 commit comments