Skip to content
This repository was archived by the owner on Feb 18, 2022. It is now read-only.

Commit c9684a1

Browse files
Merge pull request #9 from DarkGhostHunter/master
Fixes migration publishing
2 parents b9c4a53 + f9ea35f commit c9684a1

File tree

2 files changed

+61
-15
lines changed

2 files changed

+61
-15
lines changed

src/LaraconfigServiceProvider.php

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

55
use DarkGhostHunter\Laraconfig\Registrar\SettingRegistrar;
6+
use Generator;
67
use Illuminate\Filesystem\Filesystem;
78
use Illuminate\Support\Collection;
89
use Illuminate\Support\ServiceProvider;
@@ -19,10 +20,8 @@ class LaraconfigServiceProvider extends ServiceProvider
1920
* @var array|string[]
2021
*/
2122
protected const MIGRATION_FILES = [
22-
'CreateUserSettingsTable'
23-
=> __DIR__ . '/../database/migrations/00_00_00_000000_create_user_settings_table.php',
24-
'CreateUserSettingsMetadataTable'
25-
=> __DIR__ . '/../database/migrations/00_00_00_000000_create_user_settings_metadata_table.php',
23+
__DIR__ . '/../database/migrations/00_00_00_000000_create_user_settings_table.php',
24+
__DIR__ . '/../database/migrations/00_00_00_000000_create_user_settings_metadata_table.php',
2625
];
2726

2827
/**
@@ -61,13 +60,21 @@ public function boot(): void
6160

6261
$this->publishes([__DIR__.'/../config/laraconfig.php' => config_path('laraconfig.php')], 'config');
6362

64-
foreach (static::MIGRATION_FILES as $class => $file) {
65-
if (!class_exists($class)) {
66-
$this->publishes([
67-
$file => database_path('migrations/' . now()->format('Y_m_d_His') . Str::afterLast($file, '/'))
68-
], 'migrations');
69-
}
70-
}
63+
$this->publishes(iterator_to_array($this->migrationPathNames()), 'migrations');
7164
}
7265
}
73-
}
66+
67+
/**
68+
* Returns the migration file destination path name.
69+
*
70+
* @return \Generator
71+
*/
72+
protected function migrationPathNames(): Generator
73+
{
74+
foreach (static::MIGRATION_FILES as $file) {
75+
yield $file => $this->app->databasePath(
76+
'migrations/' . now()->format('Y_m_d_His') . Str::after($file, '00_00_00_000000')
77+
);
78+
}
79+
}
80+
}

tests/ServiceProviderTest.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,21 @@
55
use DarkGhostHunter\Laraconfig\Facades\Setting;
66
use DarkGhostHunter\Laraconfig\LaraconfigServiceProvider;
77
use DarkGhostHunter\Laraconfig\Registrar\SettingRegistrar;
8+
use Illuminate\Filesystem\Filesystem;
9+
use Illuminate\Support\Str;
810

911
class ServiceProviderTest extends BaseTestCase
1012
{
13+
/** @var \Illuminate\Filesystem\Filesystem */
14+
protected mixed $filesystem;
15+
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
$this->filesystem = $this->app->make(Filesystem::class);
21+
}
22+
1123
public function test_registers_package(): void
1224
{
1325
static::assertArrayHasKey(LaraconfigServiceProvider::class, $this->app->getLoadedProviders());
@@ -33,8 +45,35 @@ public function test_publishes_config(): void
3345
]
3446
)->execute();
3547

36-
$this->assertFileEquals(base_path('config/laraconfig.php'), __DIR__ . '/../config/laraconfig.php');
48+
static::assertFileEquals(base_path('config/laraconfig.php'), __DIR__ . '/../config/laraconfig.php');
49+
}
50+
51+
public function test_publishes_migrations(): void
52+
{
53+
$this->filesystem->ensureDirectoryExists(database_path('migrations'));
54+
55+
$this->artisan(
56+
'vendor:publish',
57+
[
58+
'--provider' => 'DarkGhostHunter\Laraconfig\LaraconfigServiceProvider',
59+
'--tag' => 'migrations',
60+
]
61+
)->run();
62+
63+
static::assertTrue(
64+
collect($this->filesystem->files($this->app->databasePath('migrations')))
65+
->contains(function (\SplFileInfo $file) {
66+
return Str::endsWith($file->getPathname(), '_create_user_settings_table.php')
67+
|| Str::endsWith($file->getPathname(), '_create_user_settings_metadata_table.php');
68+
})
69+
);
70+
}
71+
72+
protected function tearDown(): void
73+
{
74+
$this->filesystem->delete(base_path('config/laraconfig.php'));
75+
$this->filesystem->cleanDirectory(database_path('migrations'));
3776

38-
unlink(base_path('config/laraconfig.php'));
77+
parent::tearDown();
3978
}
40-
}
79+
}

0 commit comments

Comments
 (0)