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

Commit

Permalink
Merge pull request #9 from DarkGhostHunter/master
Browse files Browse the repository at this point in the history
Fixes migration publishing
  • Loading branch information
DarkGhostHunter authored Jul 22, 2021
2 parents b9c4a53 + f9ea35f commit c9684a1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
31 changes: 19 additions & 12 deletions src/LaraconfigServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace DarkGhostHunter\Laraconfig;

use DarkGhostHunter\Laraconfig\Registrar\SettingRegistrar;
use Generator;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
Expand All @@ -19,10 +20,8 @@ class LaraconfigServiceProvider extends ServiceProvider
* @var array|string[]
*/
protected const MIGRATION_FILES = [
'CreateUserSettingsTable'
=> __DIR__ . '/../database/migrations/00_00_00_000000_create_user_settings_table.php',
'CreateUserSettingsMetadataTable'
=> __DIR__ . '/../database/migrations/00_00_00_000000_create_user_settings_metadata_table.php',
__DIR__ . '/../database/migrations/00_00_00_000000_create_user_settings_table.php',
__DIR__ . '/../database/migrations/00_00_00_000000_create_user_settings_metadata_table.php',
];

/**
Expand Down Expand Up @@ -61,13 +60,21 @@ public function boot(): void

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

foreach (static::MIGRATION_FILES as $class => $file) {
if (!class_exists($class)) {
$this->publishes([
$file => database_path('migrations/' . now()->format('Y_m_d_His') . Str::afterLast($file, '/'))
], 'migrations');
}
}
$this->publishes(iterator_to_array($this->migrationPathNames()), 'migrations');
}
}
}

/**
* Returns the migration file destination path name.
*
* @return \Generator
*/
protected function migrationPathNames(): Generator
{
foreach (static::MIGRATION_FILES as $file) {
yield $file => $this->app->databasePath(
'migrations/' . now()->format('Y_m_d_His') . Str::after($file, '00_00_00_000000')
);
}
}
}
45 changes: 42 additions & 3 deletions tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@
use DarkGhostHunter\Laraconfig\Facades\Setting;
use DarkGhostHunter\Laraconfig\LaraconfigServiceProvider;
use DarkGhostHunter\Laraconfig\Registrar\SettingRegistrar;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;

class ServiceProviderTest extends BaseTestCase
{
/** @var \Illuminate\Filesystem\Filesystem */
protected mixed $filesystem;

protected function setUp(): void
{
parent::setUp();

$this->filesystem = $this->app->make(Filesystem::class);
}

public function test_registers_package(): void
{
static::assertArrayHasKey(LaraconfigServiceProvider::class, $this->app->getLoadedProviders());
Expand All @@ -33,8 +45,35 @@ public function test_publishes_config(): void
]
)->execute();

$this->assertFileEquals(base_path('config/laraconfig.php'), __DIR__ . '/../config/laraconfig.php');
static::assertFileEquals(base_path('config/laraconfig.php'), __DIR__ . '/../config/laraconfig.php');
}

public function test_publishes_migrations(): void
{
$this->filesystem->ensureDirectoryExists(database_path('migrations'));

$this->artisan(
'vendor:publish',
[
'--provider' => 'DarkGhostHunter\Laraconfig\LaraconfigServiceProvider',
'--tag' => 'migrations',
]
)->run();

static::assertTrue(
collect($this->filesystem->files($this->app->databasePath('migrations')))
->contains(function (\SplFileInfo $file) {
return Str::endsWith($file->getPathname(), '_create_user_settings_table.php')
|| Str::endsWith($file->getPathname(), '_create_user_settings_metadata_table.php');
})
);
}

protected function tearDown(): void
{
$this->filesystem->delete(base_path('config/laraconfig.php'));
$this->filesystem->cleanDirectory(database_path('migrations'));

unlink(base_path('config/laraconfig.php'));
parent::tearDown();
}
}
}

0 comments on commit c9684a1

Please sign in to comment.