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

Commit 00699b6

Browse files
Fixes migrations publishing.
1 parent 7774ed7 commit 00699b6

File tree

2 files changed

+84
-15
lines changed

2 files changed

+84
-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: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,33 @@
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\Carbon;
810

911
class ServiceProviderTest extends BaseTestCase
1012
{
13+
/** @var \Illuminate\Support\Carbon */
14+
static protected $now;
15+
16+
/** @var \Illuminate\Filesystem\Filesystem */
17+
protected mixed $filesystem;
18+
19+
public static function setUpBeforeClass(): void
20+
{
21+
parent::setUpBeforeClass();
22+
23+
static::$now = Carbon::create(2020, 1, 1, 19, 30);
24+
25+
Carbon::setTestNow(static::$now);
26+
}
27+
28+
protected function setUp(): void
29+
{
30+
parent::setUp();
31+
32+
$this->filesystem = $this->app->make(Filesystem::class);
33+
}
34+
1135
public function test_registers_package(): void
1236
{
1337
static::assertArrayHasKey(LaraconfigServiceProvider::class, $this->app->getLoadedProviders());
@@ -33,8 +57,46 @@ public function test_publishes_config(): void
3357
]
3458
)->execute();
3559

36-
$this->assertFileEquals(base_path('config/laraconfig.php'), __DIR__ . '/../config/laraconfig.php');
60+
static::assertFileEquals(base_path('config/laraconfig.php'), __DIR__ . '/../config/laraconfig.php');
61+
}
62+
63+
public function test_publishes_migrations(): void
64+
{
65+
$this->filesystem->ensureDirectoryExists(database_path('migrations'));
66+
67+
$this->artisan(
68+
'vendor:publish',
69+
[
70+
'--provider' => 'DarkGhostHunter\Laraconfig\LaraconfigServiceProvider',
71+
'--tag' => 'migrations',
72+
]
73+
)->run();
74+
75+
static::assertFileEquals(
76+
database_path('migrations/2020_01_01_193000_create_user_settings_table.php'),
77+
__DIR__ . '/../database/migrations/00_00_00_000000_create_user_settings_table.php'
78+
);
79+
80+
static::assertFileEquals(
81+
database_path('migrations/2020_01_01_193000_create_user_settings_metadata_table.php'),
82+
__DIR__ . '/../database/migrations/00_00_00_000000_create_user_settings_metadata_table.php'
83+
);
84+
}
85+
86+
protected function tearDown(): void
87+
{
88+
if ($this->filesystem->exists(base_path('config/laraconfig.php'))) {
89+
$this->filesystem->delete(base_path('config/laraconfig.php'));
90+
}
91+
92+
if ($this->filesystem->exists(database_path('00_00_00_000000_create_user_settings_metadata_table.php'))) {
93+
$this->filesystem->delete(database_path('00_00_00_000000_create_user_settings_metadata_table.php'));
94+
}
95+
96+
if ($this->filesystem->exists(database_path('00_00_00_000000_create_user_settings_metadata_table.php'))) {
97+
$this->filesystem->delete(database_path('00_00_00_000000_create_user_settings_metadata_table.php'));
98+
}
3799

38-
unlink(base_path('config/laraconfig.php'));
100+
parent::tearDown();
39101
}
40-
}
102+
}

0 commit comments

Comments
 (0)