Completed
Branch testing (7f3043)
by Hennik
05:13
created

BuilderTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 46
c 0
b 0
f 0
dl 0
loc 75
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 22 1
A testUpdatePolygon() 0 14 1
A testUpdatePoint() 0 14 1
A testUpdateLinestring() 0 14 1
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')
1 ignored issue
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'from'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
            ->/** @scrutinizer ignore-call */ 
31
              shouldReceive('from')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
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);
1 ignored issue
show
Bug introduced by
$this->queryBuilder of type Mockery\MockInterface is incompatible with the type Illuminate\Database\Query\Builder expected by parameter $query of LaravelSpatial\Eloquent\Builder::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
        $this->builder = new Builder(/** @scrutinizer ignore-type */ $this->queryBuilder);
Loading history...
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;
1 ignored issue
show
Bug introduced by
The trait LaravelSpatial\Eloquent\SpatialTrait requires the property $columns which is not provided by Eloquent\TestBuilderModel.
Loading history...
99
100
    public $timestamps = false;
101
    protected $spatialFields = ['point', 'linestring', 'polygon'];
102
}
103