Skip to content

Commit a77e8a0

Browse files
committed
Added unit tests.
1 parent 6b9aeb2 commit a77e8a0

17 files changed

+1019
-0
lines changed

tests/Unit/BaseTestCase.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
abstract class BaseTestCase extends PHPUnit_Framework_TestCase
4+
{
5+
public function tearDown()
6+
{
7+
Mockery::close();
8+
}
9+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Illuminate\Container\Container;
4+
use Grimzy\LaravelSpatial\Connectors\ConnectionFactory;
5+
use Grimzy\LaravelSpatial\MysqlConnection;
6+
use Stubs\PDOStub;
7+
8+
class ConnectionFactoryBaseTest extends BaseTestCase
9+
{
10+
public function testMakeCallsCreateConnection()
11+
{
12+
$mysql_config = ['driver' => 'mysql', 'prefix' => 'prefix', 'database' => 'database', 'name' => 'foo'];
13+
$pdo = new PDOStub();
14+
15+
$factory = Mockery::mock(ConnectionFactory::class, [new Container()])->makePartial();
16+
$factory->shouldAllowMockingProtectedMethods();
17+
$conn = $factory->createConnection('mysql', $pdo, 'database', 'prefix', $mysql_config);
18+
19+
$this->assertInstanceOf(MysqlConnection::class, $conn);
20+
}
21+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php namespace Eloquent;
2+
3+
use BaseTestCase;
4+
use Grimzy\LaravelSpatial\Eloquent\Builder;
5+
use Grimzy\LaravelSpatial\Eloquent\SpatialTrait;
6+
use Grimzy\LaravelSpatial\MysqlConnection;
7+
use Grimzy\LaravelSpatial\Types\LineString;
8+
use Grimzy\LaravelSpatial\Types\Point;
9+
use Grimzy\LaravelSpatial\Types\Polygon;
10+
use Illuminate\Database\Eloquent\Model;
11+
use Illuminate\Database\Query\Builder as QueryBuilder;
12+
use Illuminate\Database\Query\Expression;
13+
use Mockery;
14+
15+
class BuilderTest extends BaseTestCase
16+
{
17+
protected $builder;
18+
19+
/**
20+
* @var \Mockery\MockInterface $queryBuilder
21+
*/
22+
protected $queryBuilder;
23+
24+
protected function setUp()
25+
{
26+
$connection = Mockery::mock(MysqlConnection::class)->makePartial();
27+
$this->queryBuilder = Mockery::mock(QueryBuilder::class, [$connection])->makePartial();
28+
29+
$this->queryBuilder
30+
->shouldReceive('from')
31+
->andReturn($this->queryBuilder);
32+
33+
$this->queryBuilder
34+
->shouldReceive('take')
35+
->with(1)
36+
->andReturn($this->queryBuilder);
37+
38+
$this->queryBuilder
39+
->shouldReceive('get')
40+
->andReturn([]);
41+
42+
$this->builder = new Builder($this->queryBuilder);
43+
$this->builder->setModel(new TestBuilderModel());
44+
}
45+
46+
public function testUpdate()
47+
{
48+
$this->queryBuilder
49+
->shouldReceive('raw')
50+
->with("ST_GeomFromText('POINT(2 1)')")
51+
->andReturn(new Expression("ST_GeomFromText('POINT(2 1)')"));
52+
53+
$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)]);
61+
62+
$builder->update(['point' => new Point(1, 3)]);
63+
}
64+
65+
// public function testUpdateLinestring()
66+
// {
67+
// $this->queryBuilder
68+
// ->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)')"));
71+
//
72+
// $this->queryBuilder
73+
// ->shouldReceive('update')
74+
// ->andReturn(1);
75+
//
76+
// $linestring = new LineString([new Point(0, 0), new Point(1, 1), new Point(2, 2)]);
77+
//
78+
// $builder = Mockery::mock(Builder::class, [$this->queryBuilder])->makePartial();
79+
// $builder->shouldAllowMockingProtectedMethods();
80+
// $builder
81+
// ->shouldReceive('addUpdatedAtColumn')
82+
// ->andReturn(['linestring' => $linestring]);
83+
//
84+
// $builder
85+
// ->shouldReceive('asWKT')->with($linestring)->once();
86+
//
87+
// $builder->update(['linestring' => $linestring]);
88+
// }
89+
}
90+
91+
class TestBuilderModel extends Model
92+
{
93+
use SpatialTrait;
94+
95+
protected $spatialFields = [
96+
'point' => Point::class,
97+
'linestring' => LineString::class,
98+
'polygon' => Polygon::class
99+
];
100+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
use Grimzy\LaravelSpatial\MysqlConnection;
4+
use Grimzy\LaravelSpatial\Types\Point;
5+
use Illuminate\Database\Eloquent\Model;
6+
use Mockery as m;
7+
8+
class SpatialTraitTest extends BaseTestCase
9+
{
10+
/**
11+
* @var TestModel
12+
*/
13+
protected $model;
14+
15+
/**
16+
* @var array
17+
*/
18+
protected $queries;
19+
20+
public function setUp()
21+
{
22+
$this->model = new TestModel();
23+
$this->queries = &$this->model->getConnection()->getPdo()->queries;
24+
}
25+
26+
public function tearDown()
27+
{
28+
$this->model->getConnection()->getPdo()->resetQueries();
29+
}
30+
31+
public function testInsertPointHasCorrectSql()
32+
{
33+
$this->model->point = new Point(1, 2);
34+
$this->model->save();
35+
36+
$this->assertContains("ST_GeomFromText('POINT(2 1)')", $this->queries[0]);
37+
}
38+
39+
public function testUpdatePointHasCorrectSql()
40+
{
41+
$this->model->exists = true;
42+
$this->model->point = new Point(2, 4);
43+
$this->model->save();
44+
45+
$this->assertContains("ST_GeomFromText('POINT(4 2)')", $this->queries[0]);
46+
}
47+
}
48+
49+
class TestModel extends Model
50+
{
51+
use \Grimzy\LaravelSpatial\Eloquent\SpatialTrait;
52+
53+
protected $postgisFields = [
54+
'point' => Point::class
55+
];
56+
57+
58+
public static $pdo;
59+
60+
public static function resolveConnection($connection = null)
61+
{
62+
if (is_null(static::$pdo)) {
63+
static::$pdo = m::mock('TestPDO')->makePartial();
64+
}
65+
66+
return new MysqlConnection(static::$pdo);
67+
}
68+
69+
public function testrelatedmodels()
70+
{
71+
return $this->hasMany(TestRelatedModel::class);
72+
}
73+
74+
public function testrelatedmodels2()
75+
{
76+
return $this->belongsToMany(TestRelatedModel::class);
77+
}
78+
79+
}
80+
81+
class TestRelatedModel extends TestModel
82+
{
83+
public function testmodel()
84+
{
85+
return $this->belongsTo(TestModel::class);
86+
}
87+
88+
public function testmodels()
89+
{
90+
return $this->belongsToMany(TestModel::class);
91+
}
92+
}
93+
94+
class TestPDO extends PDO
95+
{
96+
public $queries = [];
97+
public $counter = 1;
98+
99+
public function prepare($statement, $driver_options = null)
100+
{
101+
$this->queries[] = $statement;
102+
103+
$stmt = m::mock('PDOStatement');
104+
$stmt->shouldReceive('bindValue')->zeroOrMoreTimes();
105+
$stmt->shouldReceive('execute');
106+
$stmt->shouldReceive('fetchAll')->andReturn([['id' => 1, 'point' => 'POINT(1 2)']]);
107+
$stmt->shouldReceive('rowCount')->andReturn(1);
108+
109+
return $stmt;
110+
}
111+
112+
public function lastInsertId($name = null)
113+
{
114+
return $this->counter++;
115+
}
116+
117+
public function resetQueries()
118+
{
119+
$this->queries = [];
120+
}
121+
}

tests/Unit/MysqlConnectionTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Grimzy\LaravelSpatial\MysqlConnection;
4+
use Grimzy\LaravelSpatial\Schema\Builder;
5+
use Stubs\PDOStub;
6+
7+
class MysqlConnectionTest extends PHPUnit_Framework_TestCase
8+
{
9+
private $mysqlConnection;
10+
11+
protected function setUp()
12+
{
13+
$mysqlConfig = ['driver' => 'mysql', 'prefix' => 'prefix', 'database' => 'database', 'name' => 'foo'];
14+
$this->mysqlConnection = new MysqlConnection(new PDOStub(), 'database', 'prefix', $mysqlConfig);
15+
}
16+
17+
public function testGetSchemaBuilder()
18+
{
19+
$builder = $this->mysqlConnection->getSchemaBuilder();
20+
21+
$this->assertInstanceOf(Builder::class, $builder);
22+
}
23+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
namespace Schema;
3+
4+
use BaseTestCase;
5+
use Mockery;
6+
use Grimzy\LaravelSpatial\Schema\Blueprint;
7+
8+
class BlueprintTest extends BaseTestCase
9+
{
10+
protected $blueprint;
11+
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
16+
$this->blueprint = Mockery::mock(Blueprint::class)
17+
->makePartial()->shouldAllowMockingProtectedMethods();
18+
}
19+
20+
public function testGeometry()
21+
{
22+
$this->blueprint
23+
->shouldReceive('addCommand')
24+
->with('geometry', ['col', null, 2, true]);
25+
26+
$this->blueprint->geometry('col');
27+
}
28+
29+
public function testPoint()
30+
{
31+
$this->blueprint
32+
->shouldReceive('addCommand')
33+
->with('point', ['col', null, 2, true]);
34+
35+
$this->blueprint->point('col');
36+
}
37+
38+
public function testLinestring()
39+
{
40+
$this->blueprint
41+
->shouldReceive('addCommand')
42+
->with('linestring', ['col', null, 2, true]);
43+
44+
$this->blueprint->linestring('col');
45+
}
46+
47+
public function testPolygon()
48+
{
49+
$this->blueprint
50+
->shouldReceive('addCommand')
51+
->with('polygon', ['col', null, 2, true]);
52+
53+
$this->blueprint->polygon('col');
54+
}
55+
56+
public function testMultiPoint()
57+
{
58+
$this->blueprint
59+
->shouldReceive('addCommand')
60+
->with('multipoint', ['col', null, 2, true]);
61+
62+
$this->blueprint->multipoint('col');
63+
}
64+
65+
public function testMultiLineString()
66+
{
67+
$this->blueprint
68+
->shouldReceive('addCommand')
69+
->with('multilinestring', ['col', null, 2, true]);
70+
71+
$this->blueprint->multilinestring('col');
72+
}
73+
74+
public function testMulltiPolygon()
75+
{
76+
$this->blueprint
77+
->shouldReceive('addCommand')
78+
->with('multipolygon', ['col', null, 2, true]);
79+
80+
$this->blueprint->multipolygon('col');
81+
}
82+
83+
public function testGeometryCollection()
84+
{
85+
$this->blueprint
86+
->shouldReceive('addCommand')
87+
->with('geometrycollection', ['col', null, 2, true]);
88+
89+
$this->blueprint->geometrycollection('col');
90+
}
91+
92+
93+
}

tests/Unit/Schema/BuilderTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace Schema;
3+
4+
use BaseTestCase;
5+
use Grimzy\LaravelSpatial\MysqlConnection;
6+
use Grimzy\LaravelSpatial\Schema\Builder;
7+
use Grimzy\LaravelSpatial\Schema\Blueprint;
8+
use Mockery;
9+
10+
class BuilderTest extends BaseTestCase
11+
{
12+
public function testReturnsCorrectBlueprint()
13+
{
14+
$connection = Mockery::mock(MysqlConnection::class);
15+
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn(null);
16+
17+
$mock = Mockery::mock(Builder::class, [$connection]);
18+
$mock->makePartial()->shouldAllowMockingProtectedMethods();
19+
$blueprint = $mock->createBlueprint('test', function () {
20+
});
21+
22+
$this->assertInstanceOf(Blueprint::class, $blueprint);
23+
}
24+
}

0 commit comments

Comments
 (0)