Skip to content

Commit

Permalink
Merge pull request #115 from kitloong/feature/simplify
Browse files Browse the repository at this point in the history
Simplify
  • Loading branch information
kitloong authored Sep 5, 2022
2 parents b133cef + c4a3718 commit c1632cb
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 130 deletions.
38 changes: 19 additions & 19 deletions tests/Feature/FeatureTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function setUp(): void
{
parent::setUp();

$this->prepareStorage();
$this->prepareStoragePath();
}

protected function tearDown(): void
Expand Down Expand Up @@ -58,25 +58,25 @@ protected function loadDotenv()
$dotenv->load();
}

protected function prepareStorage()
protected function prepareStoragePath(): void
{
File::deleteDirectory(storage_path());
File::makeDirectory($this->storageMigrations(), 0775, true);
File::makeDirectory($this->storageFrom());
File::makeDirectory($this->storageSql());
File::makeDirectory($this->getStorageMigrationsPath(), 0775, true);
File::makeDirectory($this->getStorageFromPath());
File::makeDirectory($this->getStorageSqlPath());
}

protected function storageMigrations(string $path = ''): string
protected function getStorageMigrationsPath(string $path = ''): string
{
return storage_path('migrations') . ($path ? DIRECTORY_SEPARATOR . $path : $path);
}

protected function storageFrom(string $path = ''): string
protected function getStorageFromPath(string $path = ''): string
{
return storage_path('from') . ($path ? DIRECTORY_SEPARATOR . $path : $path);
}

protected function storageSql(string $path = ''): string
protected function getStorageSqlPath(string $path = ''): string
{
return storage_path('sql') . ($path ? DIRECTORY_SEPARATOR . $path : $path);
}
Expand All @@ -93,8 +93,8 @@ protected function migrateCollation(string $connection): void

protected function migrateFromTemplate(string $connection, string $templatePath): void
{
File::copyDirectory($templatePath, $this->storageFrom());
foreach (File::files($this->storageFrom()) as $file) {
File::copyDirectory($templatePath, $this->getStorageFromPath());
foreach (File::files($this->getStorageFromPath()) as $file) {
$content = str_replace([
'[db]',
'_DB_'
Expand All @@ -103,14 +103,14 @@ protected function migrateFromTemplate(string $connection, string $templatePath)
ucfirst("$connection")
], $file->getContents());

file_put_contents($this->storageFrom($file->getBasename()), $content);
file_put_contents($this->getStorageFromPath($file->getBasename()), $content);
File::move(
$this->storageFrom($file->getBasename()),
$this->storageFrom(str_replace('_db_', "_${connection}_", $file->getBasename()))
$this->getStorageFromPath($file->getBasename()),
$this->getStorageFromPath(str_replace('_db_', "_${connection}_", $file->getBasename()))
);
}

$this->runMigrationsFrom($connection, $this->storageFrom());
$this->runMigrationsFrom($connection, $this->getStorageFromPath());
}

protected function runMigrationsFrom(string $connection, string $path): void
Expand Down Expand Up @@ -141,7 +141,7 @@ protected function generateMigrations(array $options = []): void
$this->artisan(
'migrate:generate',
array_merge([
'--path' => $this->storageMigrations(),
'--path' => $this->getStorageMigrationsPath(),
'--template-path' => base_path('resources/stub/migration.stub'),
], $options)
)
Expand All @@ -155,7 +155,7 @@ protected function generateMigrations(array $options = []): void
protected function assertMigrations(): void
{
$migrations = [];
foreach (File::files($this->storageMigrations()) as $migration) {
foreach (File::files($this->getStorageMigrationsPath()) as $migration) {
$migrations[] = $migration->getFilenameWithoutExtension();
}

Expand All @@ -169,7 +169,7 @@ protected function assertMigrations(): void
$this->assertSame($migrations, $dbMigrations);
}

protected function truncateMigration()
protected function truncateMigrationsTable()
{
DB::table('migrations')->truncate();
}
Expand All @@ -181,7 +181,7 @@ protected function truncateMigration()
*/
protected function getTableNames(): array
{
return collect(DB::connection()->getDoctrineSchemaManager()->listTableNames())
return collect(DB::getDoctrineSchemaManager()->listTableNames())
->map(function ($table) {
// The table name may contain quotes.
// Always trim quotes before set into list.
Expand All @@ -200,7 +200,7 @@ protected function getTableNames(): array
*/
protected function getViewNames(): array
{
return collect(DB::connection()->getDoctrineSchemaManager()->listViews())
return collect(DB::getDoctrineSchemaManager()->listViews())
->map(function (View $view) {
return $view->getName();
})
Expand Down
18 changes: 9 additions & 9 deletions tests/Feature/MariaDB/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public function testDown()
{
$this->migrateGeneral('mariadb');

$this->truncateMigration();
$this->truncateMigrationsTable();

$this->generateMigrations();

$this->rollbackMigrationsFrom('mariadb', $this->storageMigrations());
$this->rollbackMigrationsFrom('mariadb', $this->getStorageMigrationsPath());

$tables = $this->getTableNames();
$views = $this->getViewNames();
Expand All @@ -61,23 +61,23 @@ private function verify(callable $migrateTemplates, callable $generateMigrations
{
$migrateTemplates();

$this->truncateMigration();
$this->dumpSchemaAs($this->storageSql('expected.sql'));
$this->truncateMigrationsTable();
$this->dumpSchemaAs($this->getStorageSqlPath('expected.sql'));

$generateMigrations();

$this->assertMigrations();

$this->dropAllTables();

$this->runMigrationsFrom('mariadb', $this->storageMigrations());
$this->runMigrationsFrom('mariadb', $this->getStorageMigrationsPath());

$this->truncateMigration();
$this->dumpSchemaAs($this->storageSql('actual.sql'));
$this->truncateMigrationsTable();
$this->dumpSchemaAs($this->getStorageSqlPath('actual.sql'));

$this->assertFileEqualsIgnoringOrder(
$this->storageSql('expected.sql'),
$this->storageSql('actual.sql')
$this->getStorageSqlPath('expected.sql'),
$this->getStorageSqlPath('actual.sql')
);
}
}
4 changes: 2 additions & 2 deletions tests/Feature/MariaDB/MariaDBTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function dumpSchemaAs(string $destination): void

protected function dropAllTables(): void
{
Schema::connection('mariadb')->dropAllViews();
Schema::connection('mariadb')->dropAllTables();
Schema::dropAllViews();
Schema::dropAllTables();
}
}
52 changes: 26 additions & 26 deletions tests/Feature/MySQL57/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public function testDown()
{
$this->migrateGeneral('mysql57');

$this->truncateMigration();
$this->truncateMigrationsTable();

$this->generateMigrations();

$this->rollbackMigrationsFrom('mysql57', $this->storageMigrations());
$this->rollbackMigrationsFrom('mysql57', $this->getStorageMigrationsPath());

$tables = $this->getTableNames();
$views = $this->getViewNames();
Expand Down Expand Up @@ -79,11 +79,11 @@ public function testSquashDown()
{
$this->migrateGeneral('mysql57');

$this->truncateMigration();
$this->truncateMigrationsTable();

$this->generateMigrations(['--squash' => true]);

$this->rollbackMigrationsFrom('mysql57', $this->storageMigrations());
$this->rollbackMigrationsFrom('mysql57', $this->getStorageMigrationsPath());

$tables = $this->getTableNames();
$views = $this->getViewNames();
Expand All @@ -97,7 +97,7 @@ public function testTables()
{
$this->migrateGeneral('mysql57');

$this->truncateMigration();
$this->truncateMigrationsTable();

$this->generateMigrations([
'--tables' => implode(',', [
Expand All @@ -109,7 +109,7 @@ public function testTables()

$this->dropAllTables();

$this->runMigrationsFrom('mysql57', $this->storageMigrations());
$this->runMigrationsFrom('mysql57', $this->getStorageMigrationsPath());

$tables = $this->getTableNames();
$views = $this->getViewNames();
Expand All @@ -127,7 +127,7 @@ public function testIgnore()
{
$this->migrateGeneral('mysql57');

$this->truncateMigration();
$this->truncateMigrationsTable();

$allAssets = count($this->getTableNames()) + count($this->getViewNames());

Expand All @@ -148,7 +148,7 @@ public function testIgnore()

$this->dropAllTables();

$this->runMigrationsFrom('mysql57', $this->storageMigrations());
$this->runMigrationsFrom('mysql57', $this->getStorageMigrationsPath());

$tables = $this->getTableNames();
$views = $this->getViewNames();
Expand All @@ -161,7 +161,7 @@ public function testDefaultIndexNames()
{
$this->migrateGeneral('mysql57');

$this->truncateMigration();
$this->truncateMigrationsTable();

$this->generateMigrations([
'--tables' => 'test_index_mysql57',
Expand All @@ -170,7 +170,7 @@ public function testDefaultIndexNames()

$this->dropAllTables();

$this->runMigrationsFrom('mysql57', $this->storageMigrations());
$this->runMigrationsFrom('mysql57', $this->getStorageMigrationsPath());

$indexes = app(MySQLSchema::class)
->getTable('test_index_mysql57')
Expand Down Expand Up @@ -219,13 +219,13 @@ public function testDefaultFKNames()
{
$this->migrateGeneral('mysql57');

$this->truncateMigration();
$this->truncateMigrationsTable();

$this->generateMigrations(['--default-fk-names' => true]);

$this->dropAllTables();

$this->runMigrationsFrom('mysql57', $this->storageMigrations());
$this->runMigrationsFrom('mysql57', $this->getStorageMigrationsPath());

$foreignKeys = app(MySQLSchema::class)->getTableForeignKeys('user_profile_mysql57');
$foreignKeyNames = $foreignKeys->map(function (ForeignKey $foreignKey) {
Expand All @@ -246,7 +246,7 @@ public function testDefaultFKNames()
$foreignKeyNames
);

$this->rollbackMigrationsFrom('mysql57', $this->storageMigrations());
$this->rollbackMigrationsFrom('mysql57', $this->getStorageMigrationsPath());
}

public function testDate()
Expand All @@ -268,15 +268,15 @@ public function testTableFilenameAndViewFilename()
{
$this->migrateGeneral('mysql57');

$this->truncateMigration();
$this->truncateMigrationsTable();

$this->generateMigrations([
'--table-filename' => '[datetime_prefix]_custom_[table]_table.php',
'--view-filename' => '[datetime_prefix]_custom_[table]_view.php',
]);

$migrations = [];
foreach (File::files($this->storageMigrations()) as $migration) {
foreach (File::files($this->getStorageMigrationsPath()) as $migration) {
$migrations[] = substr($migration->getFilenameWithoutExtension(), 18);
}

Expand All @@ -288,12 +288,12 @@ public function testFKFilename()
{
$this->migrateGeneral('mysql57');

$this->truncateMigration();
$this->truncateMigrationsTable();

$this->generateMigrations(['--fk-filename' => '[datetime_prefix]_custom_[table]_table.php']);

$migrations = [];
foreach (File::files($this->storageMigrations()) as $migration) {
foreach (File::files($this->getStorageMigrationsPath()) as $migration) {
$migrations[] = substr($migration->getFilenameWithoutExtension(), 18);
}

Expand All @@ -304,14 +304,14 @@ public function testSkipView()
{
$this->migrateGeneral('mysql57');

$this->truncateMigration();
$this->truncateMigrationsTable();

$this->generateMigrations([
'--skip-views' => true,
]);

$migrations = [];
foreach (File::files($this->storageMigrations()) as $migration) {
foreach (File::files($this->getStorageMigrationsPath()) as $migration) {
$migrations[] = substr($migration->getFilenameWithoutExtension(), 18);
}

Expand All @@ -333,23 +333,23 @@ private function verify(callable $migrateTemplates, callable $generateMigrations
{
$migrateTemplates();

$this->truncateMigration();
$this->dumpSchemaAs($this->storageSql('expected.sql'));
$this->truncateMigrationsTable();
$this->dumpSchemaAs($this->getStorageSqlPath('expected.sql'));

$generateMigrations();

$this->assertMigrations();

$this->dropAllTables();

$this->runMigrationsFrom('mysql57', $this->storageMigrations());
$this->runMigrationsFrom('mysql57', $this->getStorageMigrationsPath());

$this->truncateMigration();
$this->dumpSchemaAs($this->storageSql('actual.sql'));
$this->truncateMigrationsTable();
$this->dumpSchemaAs($this->getStorageSqlPath('actual.sql'));

$this->assertFileEqualsIgnoringOrder(
$this->storageSql('expected.sql'),
$this->storageSql('actual.sql')
$this->getStorageSqlPath('expected.sql'),
$this->getStorageSqlPath('actual.sql')
);
}
}
Loading

0 comments on commit c1632cb

Please sign in to comment.