Skip to content

Commit ccce934

Browse files
committed
Added tests for migrations. Abstracted createApplication process in integration tests. Removed utility class ClassFinder
1 parent 4db0a4a commit ccce934

File tree

6 files changed

+187
-263
lines changed

6 files changed

+187
-263
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
use Grimzy\LaravelMysqlSpatial\SpatialServiceProvider;
4+
use Illuminate\Support\Facades\DB;
5+
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
6+
7+
abstract class IntegrationBaseTestCase extends BaseTestCase
8+
{
9+
protected $after_fix = false;
10+
protected $migrations = [];
11+
12+
/**
13+
* Boots the application.
14+
*
15+
* @return \Illuminate\Foundation\Application
16+
*/
17+
public function createApplication()
18+
{
19+
$app = require __DIR__ . '/../../vendor/laravel/laravel/bootstrap/app.php';
20+
$app->register(SpatialServiceProvider::class);
21+
22+
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
23+
24+
/**
25+
* @param \Illuminate\Config\Repository $config
26+
*/
27+
$config = $app->config;
28+
$config->set('database.default', 'mysql');
29+
$config->set('database.connections.mysql.host', env('DB_HOST', '127.0.0.1'));
30+
$config->set('database.connections.mysql.database', 'spatial_test');
31+
$config->set('database.connections.mysql.username', 'root');
32+
$config->set('database.connections.mysql.password', '');
33+
$config->set('database.connections.mysql.modes', [
34+
'ONLY_FULL_GROUP_BY',
35+
'STRICT_TRANS_TABLES',
36+
'NO_ZERO_IN_DATE',
37+
'NO_ZERO_DATE',
38+
'ERROR_FOR_DIVISION_BY_ZERO',
39+
'NO_ENGINE_SUBSTITUTION',
40+
]);
41+
42+
return $app;
43+
}
44+
45+
/**
46+
* Setup DB before each test.
47+
*
48+
* @return void
49+
*/
50+
public function setUp()
51+
{
52+
parent::setUp();
53+
54+
$this->after_fix = $this->isMySQL8AfterFix();
55+
56+
$this->onMigrations(function ($migrationClass) {
57+
(new $migrationClass())->up();
58+
});
59+
60+
//\DB::listen(function($sql) {
61+
// var_dump($sql);
62+
//});
63+
}
64+
65+
public function tearDown()
66+
{
67+
$this->onMigrations(function ($migrationClass) {
68+
(new $migrationClass())->down();
69+
}, true);
70+
71+
parent::tearDown();
72+
}
73+
74+
// MySQL 8.0.4 fixed bug #26941370 and bug #88031
75+
private function isMySQL8AfterFix()
76+
{
77+
$results = DB::select(DB::raw('select version()'));
78+
$mysql_version = $results[0]->{'version()'};
79+
80+
return version_compare($mysql_version, '8.0.4', '>=');
81+
}
82+
83+
protected function assertDatabaseHas($table, array $data, $connection = null)
84+
{
85+
if (method_exists($this, 'seeInDatabase')) {
86+
$this->seeInDatabase($table, $data, $connection);
87+
} else {
88+
parent::assertDatabaseHas($table, $data, $connection);
89+
}
90+
}
91+
92+
protected function assertException($exceptionName)
93+
{
94+
if (method_exists(parent::class, 'expectException')) {
95+
parent::expectException($exceptionName);
96+
} else {
97+
$this->setExpectedException($exceptionName);
98+
}
99+
}
100+
101+
private function onMigrations(\Closure $closure, $reverse_sort = false)
102+
{
103+
$migrations = $this->migrations;
104+
$reverse_sort ? rsort($migrations, SORT_STRING) : sort($migrations, SORT_STRING);
105+
106+
foreach ($migrations as $migrationClass) {
107+
$closure($migrationClass);
108+
}
109+
110+
}
111+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\DB;
4+
5+
class MigrationTest extends IntegrationBaseTestCase
6+
{
7+
protected $migrations = [
8+
CreateLocationTable::class,
9+
UpdateLocationTable::class
10+
];
11+
12+
public function testTableWasCreatedWithRightTypes()
13+
{
14+
$result = DB::selectOne('SHOW CREATE TABLE geometry');
15+
16+
$expected = 'CREATE TABLE `geometry` (
17+
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
18+
`geo` geometry DEFAULT NULL,
19+
`location` point NOT NULL,
20+
`line` linestring DEFAULT NULL,
21+
`shape` polygon DEFAULT NULL,
22+
`multi_locations` multipoint DEFAULT NULL,
23+
`multi_lines` multilinestring DEFAULT NULL,
24+
`multi_shapes` multipolygon DEFAULT NULL,
25+
`multi_geometries` geomcollection DEFAULT NULL,
26+
`created_at` timestamp NULL DEFAULT NULL,
27+
`updated_at` timestamp NULL DEFAULT NULL,
28+
PRIMARY KEY (`id`),
29+
SPATIAL KEY `geometry_location_spatial` (`location`)
30+
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
31+
32+
$this->assertEquals('geometry', $result->Table);
33+
$this->assertEquals($expected, $result->{'Create Table'});
34+
}
35+
36+
public function testTableWasCreatedWithSrid()
37+
{
38+
$result = DB::selectOne('SHOW CREATE TABLE with_srid');
39+
40+
$expected = 'CREATE TABLE `with_srid` (
41+
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
42+
`geo` geometry /*!80003 SRID 3857 */ DEFAULT NULL,
43+
`location` point /*!80003 SRID 3857 */ DEFAULT NULL,
44+
`line` linestring /*!80003 SRID 3857 */ DEFAULT NULL,
45+
`shape` polygon /*!80003 SRID 3857 */ DEFAULT NULL,
46+
`multi_locations` multipoint /*!80003 SRID 3857 */ DEFAULT NULL,
47+
`multi_lines` multilinestring /*!80003 SRID 3857 */ DEFAULT NULL,
48+
`multi_shapes` multipolygon /*!80003 SRID 3857 */ DEFAULT NULL,
49+
`multi_geometries` geomcollection /*!80003 SRID 3857 */ DEFAULT NULL,
50+
PRIMARY KEY (`id`)
51+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci';
52+
53+
$this->assertEquals('with_srid', $result->Table);
54+
$this->assertEquals($expected, $result->{'Create Table'});
55+
}
56+
}

tests/Integration/Migrations/CreateTables.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

3+
use Grimzy\LaravelMysqlSpatial\Schema\Blueprint;
34
use Illuminate\Database\Migrations\Migration;
4-
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

77
class CreateLocationTable extends Migration
@@ -30,6 +30,18 @@ public function up()
3030
$table->increments('id');
3131
$table->geometry('geometry')->default(null)->nullable();
3232
});
33+
34+
Schema::create('with_srid', function (Blueprint $table) {
35+
$table->increments('id');
36+
$table->geometry('geo', 3857)->default(null)->nullable();
37+
$table->point('location', 3857)->default(null)->nullable();
38+
$table->lineString('line', 3857)->default(null)->nullable();
39+
$table->polygon('shape', 3857)->default(null)->nullable();
40+
$table->multiPoint('multi_locations', 3857)->default(null)->nullable();
41+
$table->multiLineString('multi_lines', 3857)->default(null)->nullable();
42+
$table->multiPolygon('multi_shapes', 3857)->default(null)->nullable();
43+
$table->geometryCollection('multi_geometries', 3857)->default(null)->nullable();
44+
});
3345
}
3446

3547
/**
@@ -41,5 +53,6 @@ public function down()
4153
{
4254
Schema::drop('geometry');
4355
Schema::drop('no_spatial_fields');
56+
Schema::drop('with_srid');
4457
}
4558
}

tests/Integration/Migrations/UpdateTables.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

3+
use Grimzy\LaravelMysqlSpatial\Schema\Blueprint;
34
use Illuminate\Database\Migrations\Migration;
4-
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

77
class UpdateLocationTable extends Migration

tests/Integration/SpatialTest.php

Lines changed: 5 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,18 @@
11
<?php
22

3-
use Grimzy\LaravelMysqlSpatial\SpatialServiceProvider;
43
use Grimzy\LaravelMysqlSpatial\Types\GeometryCollection;
54
use Grimzy\LaravelMysqlSpatial\Types\LineString;
65
use Grimzy\LaravelMysqlSpatial\Types\MultiPoint;
76
use Grimzy\LaravelMysqlSpatial\Types\MultiPolygon;
87
use Grimzy\LaravelMysqlSpatial\Types\Point;
98
use Grimzy\LaravelMysqlSpatial\Types\Polygon;
10-
use Illuminate\Filesystem\Filesystem;
11-
use Illuminate\Support\Facades\DB;
12-
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
139

14-
class SpatialTest extends BaseTestCase
10+
class SpatialTest extends IntegrationBaseTestCase
1511
{
16-
protected $after_fix = false;
17-
18-
/**
19-
* Boots the application.
20-
*
21-
* @return \Illuminate\Foundation\Application
22-
*/
23-
public function createApplication()
24-
{
25-
$app = require __DIR__.'/../../vendor/laravel/laravel/bootstrap/app.php';
26-
$app->register(SpatialServiceProvider::class);
27-
28-
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
29-
30-
$app['config']->set('database.default', 'mysql');
31-
$app['config']->set('database.connections.mysql.host', env('DB_HOST', '127.0.0.1'));
32-
$app['config']->set('database.connections.mysql.database', 'spatial_test');
33-
$app['config']->set('database.connections.mysql.username', 'root');
34-
$app['config']->set('database.connections.mysql.password', '');
35-
$app['config']->set('database.connections.mysql.modes', [
36-
'ONLY_FULL_GROUP_BY',
37-
'STRICT_TRANS_TABLES',
38-
'NO_ZERO_IN_DATE',
39-
'NO_ZERO_DATE',
40-
'ERROR_FOR_DIVISION_BY_ZERO',
41-
'NO_ENGINE_SUBSTITUTION',
42-
]);
43-
44-
return $app;
45-
}
46-
47-
/**
48-
* Setup DB before each test.
49-
*
50-
* @return void
51-
*/
52-
public function setUp()
53-
{
54-
parent::setUp();
55-
56-
$this->after_fix = $this->isMySQL8AfterFix();
57-
58-
$this->onMigrations(function ($migrationClass) {
59-
(new $migrationClass())->up();
60-
});
61-
62-
//\DB::listen(function($sql) {
63-
// var_dump($sql);
64-
//});
65-
}
66-
67-
public function tearDown()
68-
{
69-
$this->onMigrations(function ($migrationClass) {
70-
(new $migrationClass())->down();
71-
}, true);
72-
73-
parent::tearDown();
74-
}
75-
76-
// MySQL 8.0.4 fixed bug #26941370 and bug #88031
77-
private function isMySQL8AfterFix()
78-
{
79-
$results = DB::select(DB::raw('select version()'));
80-
$mysql_version = $results[0]->{'version()'};
81-
82-
return version_compare($mysql_version, '8.0.4', '>=');
83-
}
84-
85-
protected function assertDatabaseHas($table, array $data, $connection = null)
86-
{
87-
if (method_exists($this, 'seeInDatabase')) {
88-
$this->seeInDatabase($table, $data, $connection);
89-
} else {
90-
parent::assertDatabaseHas($table, $data, $connection);
91-
}
92-
}
93-
94-
protected function assertException($exceptionName)
95-
{
96-
if (method_exists(parent::class, 'expectException')) {
97-
parent::expectException($exceptionName);
98-
} else {
99-
$this->setExpectedException($exceptionName);
100-
}
101-
}
102-
103-
private function onMigrations(\Closure $closure, $reverse_sort = false)
104-
{
105-
$fileSystem = new Filesystem();
106-
$classFinder = new Tools\ClassFinder();
107-
108-
$migrations = $fileSystem->files(__DIR__.'/Migrations');
109-
$reverse_sort ? rsort($migrations, SORT_STRING) : sort($migrations, SORT_STRING);
110-
111-
foreach ($migrations as $file) {
112-
$fileSystem->requireOnce($file);
113-
$migrationClass = $classFinder->findClass($file);
114-
115-
$closure($migrationClass);
116-
}
117-
}
12+
protected $migrations = [
13+
CreateLocationTable::class,
14+
UpdateLocationTable::class
15+
];
11816

11917
public function testSpatialFieldsNotDefinedException()
12018
{

0 commit comments

Comments
 (0)