Skip to content

Commit 694a1ab

Browse files
author
Andrey Helldar
committed
RC
1 parent c17b9f6 commit 694a1ab

File tree

8 files changed

+56
-49
lines changed

8 files changed

+56
-49
lines changed

.github/workflows/phpunit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
strategy:
99
fail-fast: true
1010
matrix:
11-
php: [ "7.4", "8.0" ]
11+
php: [ "7.2", "7.3", "7.4", "8.0" ]
1212
laravel: [ "7.0", "8.0" ]
1313

1414
name: Laravel ${{ matrix.laravel }}, PHP ${{ matrix.php }}

src/Console/Migrate.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Helldar\MigrateDB\Console;
44

55
use Helldar\MigrateDB\Exceptions\InvalidArgumentException;
6+
use Helldar\Support\Facades\Helpers\Arr as Arrayable;
67
use Illuminate\Console\Command;
78
use Illuminate\Database\Schema\Builder;
89
use Illuminate\Support\Arr;
@@ -25,9 +26,9 @@ public function handle()
2526
$this->cleanTargetDatabase();
2627
$this->runMigrations();
2728

28-
// $this->disableForeign();
29-
// $this->runTransfer();
30-
// $this->enableForeign();
29+
$this->disableForeign();
30+
$this->runTransfer();
31+
$this->enableForeign();
3132
}
3233

3334
protected function runTransfer(): void
@@ -49,7 +50,9 @@ protected function migrateTable(string $table, string $column): void
4950
->table($table)
5051
->orderBy($column)
5152
->chunk(1000, function (Collection $items) use ($table) {
52-
DB::connection($this->target())->table($table)->insert($items->toArray());
53+
$items = Arrayable::toArray($items);
54+
55+
return DB::connection($this->target())->table($table)->insert($items);
5356
});
5457
}
5558

tests/Concerns/Database.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Tests\Concerns;
44

5-
use Illuminate\Contracts\Console\Kernel;
65
use Tests\Services\MySqlConnection;
76

87
trait Database
@@ -25,8 +24,8 @@ protected function setDatabases($app): void
2524
protected function freshDatabase(): void
2625
{
2726
$this->createDatabases();
27+
2828
$this->cleanTestDatabase();
29-
$this->loadMigrations();
3029

3130
$this->fillTables();
3231
}
@@ -47,15 +46,6 @@ protected function createDatabase(string $name): void
4746

4847
protected function cleanTestDatabase(): void
4948
{
50-
$this->artisan('migrate:fresh', ['--database' => $this->source])->run();
51-
52-
$this->app[Kernel::class]->setArtisan(null);
53-
}
54-
55-
protected function loadMigrations(): void
56-
{
57-
$this->loadMigrationsFrom(
58-
__DIR__ . '/../fixtures/migrations'
59-
);
49+
$this->artisan('migrate')->run();
6050
}
6151
}

tests/Concerns/Migration.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,21 @@
44

55
use Illuminate\Database\Migrations\Migration as BaseMigration;
66
use Illuminate\Database\Schema\Blueprint;
7-
use Illuminate\Database\Schema\Builder;
87
use Illuminate\Support\Facades\Schema;
98

109
abstract class Migration extends BaseMigration
1110
{
12-
protected $connection = 'foo_db';
13-
1411
protected $table;
1512

1613
public function up()
1714
{
18-
$this->connection()->create($this->table, function (Blueprint $table) {
15+
Schema::create($this->table, function (Blueprint $table) {
1916
$table->string('value');
2017
});
2118
}
2219

2320
public function down()
2421
{
25-
$this->connection()->dropIfExists($this->table);
26-
}
27-
28-
protected function connection(): Builder
29-
{
30-
return Schema::connection($this->connection);
22+
Schema::dropIfExists($this->table);
3123
}
3224
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Tests\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
final class TestServiceProvider extends ServiceProvider
8+
{
9+
public function boot()
10+
{
11+
$this->loadMigrationsFrom(
12+
__DIR__ . '/../fixtures/migrations'
13+
);
14+
}
15+
}

tests/Services/MySqlConnection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ protected function compileCreateDatabase(string $name): string
9999
return sprintf(
100100
'create database %s default character set %s default collate %s',
101101
$this->wrapValue($name),
102-
$this->wrapValue(Arr::get($this->config, 'charset')),
103-
$this->wrapValue(Arr::get($this->config, 'collation'))
102+
$this->wrapValue(Arr::get($this->config(), 'charset')),
103+
$this->wrapValue(Arr::get($this->config(), 'collation'))
104104
);
105105
}
106106

tests/TestCase.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Orchestra\Testbench\TestCase as BaseTestCase;
77
use Tests\Concerns\Connections;
88
use Tests\Concerns\Database;
9+
use Tests\Providers\TestServiceProvider;
910

1011
abstract class TestCase extends BaseTestCase
1112
{
@@ -26,6 +27,9 @@ protected function getEnvironmentSetUp($app)
2627

2728
protected function getPackageProviders($app): array
2829
{
29-
return [ServiceProvider::class];
30+
return [
31+
ServiceProvider::class,
32+
TestServiceProvider::class,
33+
];
3034
}
3135
}

tests/Unit/MigrateTest.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Unit;
44

55
use Helldar\MigrateDB\Exceptions\InvalidArgumentException;
6+
use Helldar\Support\Facades\Helpers\Arr;
67
use Illuminate\Support\Facades\DB;
78
use InvalidArgumentException as BaseInvalidArgumentException;
89
use Tests\TestCase;
@@ -45,34 +46,34 @@ public function testCount()
4546

4647
public function testData()
4748
{
48-
$this->assertDatabaseHas($this->table_foo, ['value' => 'foo_q1'], $this->source);
49-
$this->assertDatabaseHas($this->table_foo, ['value' => 'foo_q2'], $this->source);
50-
$this->assertDatabaseHas($this->table_foo, ['value' => 'foo_q3'], $this->source);
49+
$this->assertDatabaseHas($this->table_foo, ['value' => 'foo_1'], $this->source);
50+
$this->assertDatabaseHas($this->table_foo, ['value' => 'foo_2'], $this->source);
51+
$this->assertDatabaseHas($this->table_foo, ['value' => 'foo_3'], $this->source);
5152

52-
$this->assertDatabaseHas($this->table_bar, ['value' => 'bar_q1'], $this->source);
53-
$this->assertDatabaseHas($this->table_bar, ['value' => 'bar_q2'], $this->source);
54-
$this->assertDatabaseHas($this->table_bar, ['value' => 'bar_q3'], $this->source);
53+
$this->assertDatabaseHas($this->table_bar, ['value' => 'bar_1'], $this->source);
54+
$this->assertDatabaseHas($this->table_bar, ['value' => 'bar_2'], $this->source);
55+
$this->assertDatabaseHas($this->table_bar, ['value' => 'bar_3'], $this->source);
5556

56-
$this->assertDatabaseHas($this->table_baz, ['value' => 'baz_q1'], $this->source);
57-
$this->assertDatabaseHas($this->table_baz, ['value' => 'baz_q2'], $this->source);
58-
$this->assertDatabaseHas($this->table_baz, ['value' => 'baz_q3'], $this->source);
57+
$this->assertDatabaseHas($this->table_baz, ['value' => 'baz_1'], $this->source);
58+
$this->assertDatabaseHas($this->table_baz, ['value' => 'baz_2'], $this->source);
59+
$this->assertDatabaseHas($this->table_baz, ['value' => 'baz_3'], $this->source);
5960

6061
$this->artisan('db:migrate', [
6162
'--schema-from' => $this->source,
6263
'--schema-to' => $this->target,
6364
])->assertExitCode(0)->run();
6465

65-
$this->assertDatabaseHas($this->table_foo, ['value' => 'foo_q1'], $this->target);
66-
$this->assertDatabaseHas($this->table_foo, ['value' => 'foo_q2'], $this->target);
67-
$this->assertDatabaseHas($this->table_foo, ['value' => 'foo_q3'], $this->target);
66+
$this->assertDatabaseHas($this->table_foo, ['value' => 'foo_1'], $this->target);
67+
$this->assertDatabaseHas($this->table_foo, ['value' => 'foo_2'], $this->target);
68+
$this->assertDatabaseHas($this->table_foo, ['value' => 'foo_3'], $this->target);
6869

69-
$this->assertDatabaseHas($this->table_bar, ['value' => 'bar_q1'], $this->target);
70-
$this->assertDatabaseHas($this->table_bar, ['value' => 'bar_q2'], $this->target);
71-
$this->assertDatabaseHas($this->table_bar, ['value' => 'bar_q3'], $this->target);
70+
$this->assertDatabaseHas($this->table_bar, ['value' => 'bar_1'], $this->target);
71+
$this->assertDatabaseHas($this->table_bar, ['value' => 'bar_2'], $this->target);
72+
$this->assertDatabaseHas($this->table_bar, ['value' => 'bar_3'], $this->target);
7273

73-
$this->assertDatabaseHas($this->table_baz, ['value' => 'baz_q1'], $this->target);
74-
$this->assertDatabaseHas($this->table_baz, ['value' => 'baz_q2'], $this->target);
75-
$this->assertDatabaseHas($this->table_baz, ['value' => 'baz_q3'], $this->target);
74+
$this->assertDatabaseHas($this->table_baz, ['value' => 'baz_1'], $this->target);
75+
$this->assertDatabaseHas($this->table_baz, ['value' => 'baz_2'], $this->target);
76+
$this->assertDatabaseHas($this->table_baz, ['value' => 'baz_3'], $this->target);
7677
}
7778

7879
public function testSame()
@@ -140,6 +141,8 @@ public function testFailedToConnectionName()
140141

141142
protected function tableData(string $connection, string $table): array
142143
{
143-
return DB::connection($connection)->table($table)->get()->toArray();
144+
$items = DB::connection($connection)->table($table)->get();
145+
146+
return Arr::toArray($items);
144147
}
145148
}

0 commit comments

Comments
 (0)