Skip to content

Commit c1632cb

Browse files
authored
Merge pull request #115 from kitloong/feature/simplify
Simplify
2 parents b133cef + c4a3718 commit c1632cb

17 files changed

+129
-130
lines changed

tests/Feature/FeatureTestCase.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected function setUp(): void
3030
{
3131
parent::setUp();
3232

33-
$this->prepareStorage();
33+
$this->prepareStoragePath();
3434
}
3535

3636
protected function tearDown(): void
@@ -58,25 +58,25 @@ protected function loadDotenv()
5858
$dotenv->load();
5959
}
6060

61-
protected function prepareStorage()
61+
protected function prepareStoragePath(): void
6262
{
6363
File::deleteDirectory(storage_path());
64-
File::makeDirectory($this->storageMigrations(), 0775, true);
65-
File::makeDirectory($this->storageFrom());
66-
File::makeDirectory($this->storageSql());
64+
File::makeDirectory($this->getStorageMigrationsPath(), 0775, true);
65+
File::makeDirectory($this->getStorageFromPath());
66+
File::makeDirectory($this->getStorageSqlPath());
6767
}
6868

69-
protected function storageMigrations(string $path = ''): string
69+
protected function getStorageMigrationsPath(string $path = ''): string
7070
{
7171
return storage_path('migrations') . ($path ? DIRECTORY_SEPARATOR . $path : $path);
7272
}
7373

74-
protected function storageFrom(string $path = ''): string
74+
protected function getStorageFromPath(string $path = ''): string
7575
{
7676
return storage_path('from') . ($path ? DIRECTORY_SEPARATOR . $path : $path);
7777
}
7878

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

9494
protected function migrateFromTemplate(string $connection, string $templatePath): void
9595
{
96-
File::copyDirectory($templatePath, $this->storageFrom());
97-
foreach (File::files($this->storageFrom()) as $file) {
96+
File::copyDirectory($templatePath, $this->getStorageFromPath());
97+
foreach (File::files($this->getStorageFromPath()) as $file) {
9898
$content = str_replace([
9999
'[db]',
100100
'_DB_'
@@ -103,14 +103,14 @@ protected function migrateFromTemplate(string $connection, string $templatePath)
103103
ucfirst("$connection")
104104
], $file->getContents());
105105

106-
file_put_contents($this->storageFrom($file->getBasename()), $content);
106+
file_put_contents($this->getStorageFromPath($file->getBasename()), $content);
107107
File::move(
108-
$this->storageFrom($file->getBasename()),
109-
$this->storageFrom(str_replace('_db_', "_${connection}_", $file->getBasename()))
108+
$this->getStorageFromPath($file->getBasename()),
109+
$this->getStorageFromPath(str_replace('_db_', "_${connection}_", $file->getBasename()))
110110
);
111111
}
112112

113-
$this->runMigrationsFrom($connection, $this->storageFrom());
113+
$this->runMigrationsFrom($connection, $this->getStorageFromPath());
114114
}
115115

116116
protected function runMigrationsFrom(string $connection, string $path): void
@@ -141,7 +141,7 @@ protected function generateMigrations(array $options = []): void
141141
$this->artisan(
142142
'migrate:generate',
143143
array_merge([
144-
'--path' => $this->storageMigrations(),
144+
'--path' => $this->getStorageMigrationsPath(),
145145
'--template-path' => base_path('resources/stub/migration.stub'),
146146
], $options)
147147
)
@@ -155,7 +155,7 @@ protected function generateMigrations(array $options = []): void
155155
protected function assertMigrations(): void
156156
{
157157
$migrations = [];
158-
foreach (File::files($this->storageMigrations()) as $migration) {
158+
foreach (File::files($this->getStorageMigrationsPath()) as $migration) {
159159
$migrations[] = $migration->getFilenameWithoutExtension();
160160
}
161161

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

172-
protected function truncateMigration()
172+
protected function truncateMigrationsTable()
173173
{
174174
DB::table('migrations')->truncate();
175175
}
@@ -181,7 +181,7 @@ protected function truncateMigration()
181181
*/
182182
protected function getTableNames(): array
183183
{
184-
return collect(DB::connection()->getDoctrineSchemaManager()->listTableNames())
184+
return collect(DB::getDoctrineSchemaManager()->listTableNames())
185185
->map(function ($table) {
186186
// The table name may contain quotes.
187187
// Always trim quotes before set into list.
@@ -200,7 +200,7 @@ protected function getTableNames(): array
200200
*/
201201
protected function getViewNames(): array
202202
{
203-
return collect(DB::connection()->getDoctrineSchemaManager()->listViews())
203+
return collect(DB::getDoctrineSchemaManager()->listViews())
204204
->map(function (View $view) {
205205
return $view->getName();
206206
})

tests/Feature/MariaDB/CommandTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public function testDown()
3030
{
3131
$this->migrateGeneral('mariadb');
3232

33-
$this->truncateMigration();
33+
$this->truncateMigrationsTable();
3434

3535
$this->generateMigrations();
3636

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

3939
$tables = $this->getTableNames();
4040
$views = $this->getViewNames();
@@ -61,23 +61,23 @@ private function verify(callable $migrateTemplates, callable $generateMigrations
6161
{
6262
$migrateTemplates();
6363

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

6767
$generateMigrations();
6868

6969
$this->assertMigrations();
7070

7171
$this->dropAllTables();
7272

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

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

7878
$this->assertFileEqualsIgnoringOrder(
79-
$this->storageSql('expected.sql'),
80-
$this->storageSql('actual.sql')
79+
$this->getStorageSqlPath('expected.sql'),
80+
$this->getStorageSqlPath('actual.sql')
8181
);
8282
}
8383
}

tests/Feature/MariaDB/MariaDBTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected function dumpSchemaAs(string $destination): void
5858

5959
protected function dropAllTables(): void
6060
{
61-
Schema::connection('mariadb')->dropAllViews();
62-
Schema::connection('mariadb')->dropAllTables();
61+
Schema::dropAllViews();
62+
Schema::dropAllTables();
6363
}
6464
}

tests/Feature/MySQL57/CommandTest.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public function testDown()
3535
{
3636
$this->migrateGeneral('mysql57');
3737

38-
$this->truncateMigration();
38+
$this->truncateMigrationsTable();
3939

4040
$this->generateMigrations();
4141

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

4444
$tables = $this->getTableNames();
4545
$views = $this->getViewNames();
@@ -79,11 +79,11 @@ public function testSquashDown()
7979
{
8080
$this->migrateGeneral('mysql57');
8181

82-
$this->truncateMigration();
82+
$this->truncateMigrationsTable();
8383

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

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

8888
$tables = $this->getTableNames();
8989
$views = $this->getViewNames();
@@ -97,7 +97,7 @@ public function testTables()
9797
{
9898
$this->migrateGeneral('mysql57');
9999

100-
$this->truncateMigration();
100+
$this->truncateMigrationsTable();
101101

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

110110
$this->dropAllTables();
111111

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

114114
$tables = $this->getTableNames();
115115
$views = $this->getViewNames();
@@ -127,7 +127,7 @@ public function testIgnore()
127127
{
128128
$this->migrateGeneral('mysql57');
129129

130-
$this->truncateMigration();
130+
$this->truncateMigrationsTable();
131131

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

@@ -148,7 +148,7 @@ public function testIgnore()
148148

149149
$this->dropAllTables();
150150

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

153153
$tables = $this->getTableNames();
154154
$views = $this->getViewNames();
@@ -161,7 +161,7 @@ public function testDefaultIndexNames()
161161
{
162162
$this->migrateGeneral('mysql57');
163163

164-
$this->truncateMigration();
164+
$this->truncateMigrationsTable();
165165

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

171171
$this->dropAllTables();
172172

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

175175
$indexes = app(MySQLSchema::class)
176176
->getTable('test_index_mysql57')
@@ -219,13 +219,13 @@ public function testDefaultFKNames()
219219
{
220220
$this->migrateGeneral('mysql57');
221221

222-
$this->truncateMigration();
222+
$this->truncateMigrationsTable();
223223

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

226226
$this->dropAllTables();
227227

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

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

249-
$this->rollbackMigrationsFrom('mysql57', $this->storageMigrations());
249+
$this->rollbackMigrationsFrom('mysql57', $this->getStorageMigrationsPath());
250250
}
251251

252252
public function testDate()
@@ -268,15 +268,15 @@ public function testTableFilenameAndViewFilename()
268268
{
269269
$this->migrateGeneral('mysql57');
270270

271-
$this->truncateMigration();
271+
$this->truncateMigrationsTable();
272272

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

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

@@ -288,12 +288,12 @@ public function testFKFilename()
288288
{
289289
$this->migrateGeneral('mysql57');
290290

291-
$this->truncateMigration();
291+
$this->truncateMigrationsTable();
292292

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

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

@@ -304,14 +304,14 @@ public function testSkipView()
304304
{
305305
$this->migrateGeneral('mysql57');
306306

307-
$this->truncateMigration();
307+
$this->truncateMigrationsTable();
308308

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

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

@@ -333,23 +333,23 @@ private function verify(callable $migrateTemplates, callable $generateMigrations
333333
{
334334
$migrateTemplates();
335335

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

339339
$generateMigrations();
340340

341341
$this->assertMigrations();
342342

343343
$this->dropAllTables();
344344

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

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

350350
$this->assertFileEqualsIgnoringOrder(
351-
$this->storageSql('expected.sql'),
352-
$this->storageSql('actual.sql')
351+
$this->getStorageSqlPath('expected.sql'),
352+
$this->getStorageSqlPath('actual.sql')
353353
);
354354
}
355355
}

0 commit comments

Comments
 (0)