From 3b130d3ffe7f15f04f146aa9ed58bd1846c241cf Mon Sep 17 00:00:00 2001 From: asciito Date: Fri, 29 Sep 2023 23:01:41 -0600 Subject: [PATCH 1/8] chore: refactor code * Remove spaghetti test * Remove unnecessary helper * Remove unnecessary files for testing * Refactor Package class to centralize the registration and exclusion of files/data for the package Signed-off-by: asciito --- src/Package/Concerns/HasConfig.php | 80 +++++++++---------- src/Package/Contracts/WithConfig.php | 18 ++--- src/Package/Package.php | 70 +++++++++++++--- src/helpers.php | 16 ---- tests/Feature/.gitkeep | 0 .../RegisterCommandsTest.php | 34 -------- .../RegisterConfigTest.php | 41 ---------- .../RegisterMigrationsTest.php | 49 ------------ .../Feature/Package/RegisterCommandsTest.php | 46 ----------- tests/Feature/Package/RegisterConfigTest.php | 41 ---------- .../Package/RegisterMigrationsTest.php | 45 ----------- .../Package/UnregisterCommandsTest.php | 35 -------- .../Feature/Package/UnregisterConfigTest.php | 40 ---------- .../Package/UnregisterMigrationTest.php | 42 ---------- tests/Feature/PackageRegisterConfigTest.php | 65 +++++++++++++++ tests/Feature/PackageUnregisterConfigTest.php | 66 +++++++++++++++ tests/Pest.php | 7 +- tests/Unit/HelpersTest.php | 13 +++ tests/Unit/TestHelpers.php | 19 ----- workbench/app/.gitkeep | 0 .../Extras/PackageTestCommandThree.php | 32 -------- .../Commands/Extras/PackageTestCommandTwo.php | 32 -------- .../Commands/PackageTestCommandFour.php | 32 -------- .../Commands/PackageTestCommandOne.php | 32 -------- .../Commands/ProjectTestCommandOne.php | 32 -------- .../Commands/ProjectTestCommandTwo.php | 32 -------- .../app/Nested/NestedServiceProvider.php | 18 ----- workbench/app/PackageServiceProvider.php | 20 +++++ workbench/app/ServiceProvider.php | 18 ----- workbench/config/extra/four.php | 5 ++ workbench/config/nested/nested-one.php | 5 -- workbench/config/nested/nested-two.php | 5 -- .../create_package_test_four_table.php | 31 ------- .../nested/create_nested_test_one_table.php | 31 ------- .../nested/create_nested_test_two_table.php | 31 ------- ...est.php => PackageServiceProviderTest.php} | 27 +++---- 36 files changed, 291 insertions(+), 819 deletions(-) delete mode 100644 tests/Feature/.gitkeep delete mode 100644 tests/Feature/NestedServiceProvider/RegisterCommandsTest.php delete mode 100644 tests/Feature/NestedServiceProvider/RegisterConfigTest.php delete mode 100644 tests/Feature/NestedServiceProvider/RegisterMigrationsTest.php delete mode 100644 tests/Feature/Package/RegisterCommandsTest.php delete mode 100644 tests/Feature/Package/RegisterConfigTest.php delete mode 100644 tests/Feature/Package/RegisterMigrationsTest.php delete mode 100644 tests/Feature/Package/UnregisterCommandsTest.php delete mode 100644 tests/Feature/Package/UnregisterConfigTest.php delete mode 100644 tests/Feature/Package/UnregisterMigrationTest.php create mode 100644 tests/Feature/PackageRegisterConfigTest.php create mode 100644 tests/Feature/PackageUnregisterConfigTest.php create mode 100644 tests/Unit/HelpersTest.php delete mode 100644 tests/Unit/TestHelpers.php delete mode 100644 workbench/app/.gitkeep delete mode 100644 workbench/app/Console/Commands/Extras/PackageTestCommandThree.php delete mode 100644 workbench/app/Console/Commands/Extras/PackageTestCommandTwo.php delete mode 100644 workbench/app/Console/Commands/PackageTestCommandFour.php delete mode 100644 workbench/app/Console/Commands/PackageTestCommandOne.php delete mode 100644 workbench/app/Nested/Console/Commands/ProjectTestCommandOne.php delete mode 100644 workbench/app/Nested/Console/Commands/ProjectTestCommandTwo.php delete mode 100644 workbench/app/Nested/NestedServiceProvider.php create mode 100644 workbench/app/PackageServiceProvider.php delete mode 100644 workbench/app/ServiceProvider.php create mode 100644 workbench/config/extra/four.php delete mode 100644 workbench/config/nested/nested-one.php delete mode 100644 workbench/config/nested/nested-two.php delete mode 100644 workbench/database/migrations/create_package_test_four_table.php delete mode 100644 workbench/database/migrations/nested/create_nested_test_one_table.php delete mode 100644 workbench/database/migrations/nested/create_nested_test_two_table.php rename workbench/tests/{ServiceProviderTest.php => PackageServiceProviderTest.php} (68%) diff --git a/src/Package/Concerns/HasConfig.php b/src/Package/Concerns/HasConfig.php index ed824db..98342f0 100644 --- a/src/Package/Concerns/HasConfig.php +++ b/src/Package/Concerns/HasConfig.php @@ -2,39 +2,32 @@ namespace Asciito\LaravelPackage\Package\Concerns; +use Illuminate\Support\Arr; use Illuminate\Support\Collection; trait HasConfig { protected string $configPath; - protected array $configFiles = []; - protected bool $preventLoadDefaultConfigFolder = false; protected bool $shouldIncludeConfigFromFolder = false; - protected array $excludedConfig = []; - - protected array $unpublishedConfig = []; - public function hasConfig(): bool { - return $this->shouldLoadDefaultConfigFolder() || filled($this->configFiles); - } - - private function shouldLoadDefaultConfigFolder(): bool - { - return ! $this->preventLoadDefaultConfigFolder && $this->shouldIncludeConfigFromFolder; + return $this->shouldLoadDefaultConfigFolder() + || $this->getRegister('config')->isNotEmpty(); } public function withConfig(string|array $config = [], bool $publish = true): static { + $this->ensureRegistersInitialize('config'); + if (filled($config)) { - $this->configFiles = collect($config) - ->mapWithKeys(fn (string $value) => [$value => $publish]) - ->merge($this->configFiles) - ->all(); + $this->register( + 'config', + Arr::mapWithKeys(Arr::wrap($config), fn (string $path): array => [$path => $publish]) + ); } $this->shouldIncludeConfigFromFolder = true; @@ -42,32 +35,35 @@ public function withConfig(string|array $config = [], bool $publish = true): sta return $this; } - public function preventDefaultConfig(): static + public function excludeConfig(string|array $path): static { - $this->preventLoadDefaultConfigFolder = true; + $this->exclude('config', Arr::wrap($path)); return $this; } public function getRegisteredConfig(): Collection { - $config = $this->loadConfigDefaultFolder(); + $files = $this->getDefaultConfigFiles(); - return collect($this->configFiles) - ->merge($config) - ->filter(function (bool $_, string $config) { - return ! in_array($config, $this->excludedConfig); - }) - ->keys(); + return $files + ->merge($this->getRegister('config')) + ->keys() + ->filter(fn (string $config) => ! in_array($config, $this->getExclude('config')->all())); } - private function loadConfigDefaultFolder(): array + + public function getPublishableConfig(): Collection { - if (! $this->shouldLoadDefaultConfigFolder()) { - return []; - } + $files = $this->getDefaultConfigFiles(); + + return $files->merge($this->getRegister('config')) + ->filter(function (bool $publish, string $path) { + $include = ! in_array($path, $this->getExclude('config')->all()); - return $this->loadFilesFrom($this->getConfigPath())->all(); + return $include && $publish; + }) + ->keys(); } public function getConfigPath(string $path = ''): string @@ -82,29 +78,25 @@ public function setConfigPath(string $path): static return $this; } - public function getPublishableConfig(): Collection + public function getDefaultConfigFiles(): Collection { - $config = $this->loadConfigDefaultFolder(); + if (! $this->shouldLoadDefaultConfigFolder()) { + return collect(); + } - return collect($this->configFiles) - ->merge($config) - ->filter(function (bool $publish, string $config) { - return $publish && ! in_array($config, [...$this->excludedConfig, ...$this->unpublishedConfig]); - }) - ->keys(); + return $this->getFilesFrom($this->configPath) + ->mapWithKeys(fn (string $path) => [$path => true]); } - public function unregisterConfig(string $path): static + public function preventDefaultConfig(): static { - $this->excludedConfig[] = absolute($path, $this->getConfigPath()); + $this->preventLoadDefaultConfigFolder = true; return $this; } - public function unpublishConfig(string $path): static + private function shouldLoadDefaultConfigFolder(): bool { - $this->unpublishedConfig[] = absolute($path, $this->getConfigPath()); - - return $this; + return ! $this->preventLoadDefaultConfigFolder && $this->shouldIncludeConfigFromFolder; } } diff --git a/src/Package/Contracts/WithConfig.php b/src/Package/Contracts/WithConfig.php index c190a75..d2fb335 100644 --- a/src/Package/Contracts/WithConfig.php +++ b/src/Package/Contracts/WithConfig.php @@ -29,12 +29,9 @@ public function hasConfig(): bool; public function withConfig(string|array $config = [], bool $publish = true): static; /** - * Prevent publishing the default config folder - * - * Calling this method ensures that the config files in the default folder - * wouldn't be loaded automatically. + * Exclude a config from being register */ - public function preventDefaultConfig(): static; + public function excludeConfig(string|array $path): static; /** * An array with the config file(s) registered @@ -59,12 +56,15 @@ public function setConfigPath(string $path): static; public function getConfigPath(string $path = ''): string; /** - * Un-register a previously registered config + * Get the files from the config path */ - public function unregisterConfig(string $path): static; + public function getDefaultConfigFiles(): Collection; /** - * Un-publish a previously published config + * Prevent publishing the default config folder + * + * Calling this method ensures that the config files in the default folder + * wouldn't be loaded automatically. */ - public function unpublishConfig(string $path): static; + public function preventDefaultConfig(): static; } diff --git a/src/Package/Package.php b/src/Package/Package.php index 40a2494..ff9655d 100644 --- a/src/Package/Package.php +++ b/src/Package/Package.php @@ -9,6 +9,7 @@ use Asciito\LaravelPackage\Package\Contracts\WithConfig; use Asciito\LaravelPackage\Package\Contracts\WithMigrations; use Illuminate\Contracts\Filesystem\FileNotFoundException; +use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Facades\File; use Symfony\Component\Finder\SplFileInfo; @@ -20,7 +21,7 @@ class Package implements WithCommands, WithConfig, WithMigrations /** * @var string The package name */ - protected string $name; + protected string $name = 'laravel-package'; /** * @var string Base path for the package @@ -32,6 +33,10 @@ class Package implements WithCommands, WithConfig, WithMigrations */ protected string $namespace; + public static array $register = []; + + public static array $excluded = []; + /** * Set the package name * @@ -106,15 +111,62 @@ public function setNamespace(string $namespace): static return $this; } - /** - * Get the files from the given path - * - * @param string|array $extensions The file extension(s) you want to include - */ - protected function loadFilesFrom(string $path, string|array $extensions = 'php'): Collection + public function ensureRegistersInitialize($component): void + { + $key = $this->prefixWithPackageName($component, '.'); + + data_fill(static::$excluded, $key, []); + data_fill(static::$register, $key, []); + } + + protected function getRegister(string $component): Collection { + $key = $this->prefixWithPackageName($component, '.'); + + return collect(data_get(static::$register, $key)); + } + + protected function register(string $component, mixed $data): static + { + $package = $this->name(); + + static::$register[$package][$component] = array_merge( + static::$register[$package][$component], + Arr::wrap($data), + ); + + return $this; + } + + + protected function getExclude(string $component): Collection + { + $key = $this->prefixWithPackageName($component, '.'); + + return collect(data_get(static::$excluded, $key)); + } + + protected function exclude(string $component, mixed $data): static + { + $package = $this->name(); + + static::$excluded[$package][$component] = array_merge( + static::$excluded[$package][$component], + Arr::wrap($data), + ); + + return $this; + } + + public function getFilesFrom(string $path): Collection + { + if (! File::exists($path)) { + return collect(); + } + return collect(File::files($path)) - ->filter(fn (SplFileInfo $file) => in_array($file->getExtension(), (array) $extensions)) - ->mapWithKeys(fn (SplFileInfo $file) => [(string) $file => true]); + ->filter(fn (SplFileInfo $file) => $file->getExtension() === 'php') + ->map(fn (SplFileInfo $file): string => $file); } + } diff --git a/src/helpers.php b/src/helpers.php index b42d10b..20ed1d0 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -11,19 +11,3 @@ function join_paths(string ...$path): string return rtrim(Arr::join($path, DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR); } } - -if (! function_exists('absolute')) { - /** - * Return the absolute path of the given path - * - * @returns false if the resulting path is not absolute - */ - function absolute(string $path, string $absolutePath = null): string - { - if (! realpath($path)) { - return join_paths(rtrim($absolutePath ?? '', DIRECTORY_SEPARATOR), trim($path, DIRECTORY_SEPARATOR)); - } - - return $path; - } -} diff --git a/tests/Feature/.gitkeep b/tests/Feature/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/tests/Feature/NestedServiceProvider/RegisterCommandsTest.php b/tests/Feature/NestedServiceProvider/RegisterCommandsTest.php deleted file mode 100644 index 8c8a7f3..0000000 --- a/tests/Feature/NestedServiceProvider/RegisterCommandsTest.php +++ /dev/null @@ -1,34 +0,0 @@ -setName('nested-service') - ->withCommands(); - } -} - -uses(NestedServiceProviderWithCommands::class); - -it('register commands', function () { - artisan('list') - ->expectsOutputToContain('project:one') - ->expectsOutputToContain('project:two') - ->assertSuccessful(); -}); - -it('run commands', function () { - artisan('project:one') - ->expectsOutput('Project command one') - ->assertSuccessful(); - - artisan('project:two') - ->expectsOutput('Project command two') - ->assertSuccessful(); -}); diff --git a/tests/Feature/NestedServiceProvider/RegisterConfigTest.php b/tests/Feature/NestedServiceProvider/RegisterConfigTest.php deleted file mode 100644 index 81e5285..0000000 --- a/tests/Feature/NestedServiceProvider/RegisterConfigTest.php +++ /dev/null @@ -1,41 +0,0 @@ -setName('nested-service') - ->setConfigPath($package->getBasePath('../../config/nested')) - ->withConfig(); - - expect($package) - ->getConfigPath() - ->toBe($package->getBasePath('../../config/nested')) - ->getPublishableConfig() - ->not()->toBeEmpty() - ->each - ->toMatch('/nested-\w+.php$/'); - } -} - -uses(NestedServiceProviderConfig::class); - -it('register config', function () { - expect(config('nested-one.key')) - ->toBe('nested-one') - ->and(config('nested-two.key')) - ->toBe('nested-two'); -}); - -it('publish config files', function () { - artisan('vendor:publish', ['--tag' => 'nested-service-config'])->run(); - - assertFileExists(config_path('nested-one.php')); - assertFileExists(config_path('nested-two.php')); -}); diff --git a/tests/Feature/NestedServiceProvider/RegisterMigrationsTest.php b/tests/Feature/NestedServiceProvider/RegisterMigrationsTest.php deleted file mode 100644 index 15aae6b..0000000 --- a/tests/Feature/NestedServiceProvider/RegisterMigrationsTest.php +++ /dev/null @@ -1,49 +0,0 @@ -setName('nested-service') - ->setMigrationPath($package->getBasePath('../../database/migrations/nested')) - ->withMigrations(); - - expect($package) - ->getMigrationPath() - ->toBe($package->getBasePath('../../database/migrations/nested')) - ->getPublishableMigrations() - ->not->toBeEmpty() - ->toHaveCount(2) - ->each - ->toStartWith($package->getMigrationPath()) - ->toMatch('/\/\w+.php$/'); - } -} - -uses(NestedServiceProviderWithMigrations::class); - -it('register migrations', function () { - artisan('migrate')->run(); - - assertdatabasecount('nested_test_one', 0); - assertdatabasecount('nested_test_one', 0); -}); - -it('publish migrations', function () { - artisan('vendor:publish', ['--tag' => 'nested-service-migrations'])->run(); - - assertFileExists(database_path('migrations/create_nested_test_one_table.php')); - assertFileExists(database_path('migrations/create_nested_test_two_table.php')); - - artisan('migrate')->assertSuccessful(); - - assertdatabasecount('nested_test_one', 0); - assertdatabasecount('nested_test_one', 0); -}); diff --git a/tests/Feature/Package/RegisterCommandsTest.php b/tests/Feature/Package/RegisterCommandsTest.php deleted file mode 100644 index e5d92b3..0000000 --- a/tests/Feature/Package/RegisterCommandsTest.php +++ /dev/null @@ -1,46 +0,0 @@ -withCommands(PackageTestCommandTwo::class) - ->withCommands([PackageTestCommandThree::class]); - } -} - -uses(PackageWithCommands::class); - -it('register commands', function () { - artisan('list') - ->expectsOutputToContain('package:one') - ->expectsOutputToContain('package:two') - ->expectsOutputToContain('package:three') - ->expectsOutputToContain('package:four') - ->assertSuccessful(); -}); - -it('run commands', function () { - artisan('package:one') - ->expectsOutput('Package command one') - ->assertSuccessful(); - - artisan('package:two') - ->expectsOutput('Package command two') - ->assertSuccessful(); - - artisan('package:three') - ->expectsOutput('Package command three') - ->assertSuccessful(); - - artisan('package:four') - ->expectsOutput('Package command four') - ->assertSuccessful(); -}); diff --git a/tests/Feature/Package/RegisterConfigTest.php b/tests/Feature/Package/RegisterConfigTest.php deleted file mode 100644 index af12be2..0000000 --- a/tests/Feature/Package/RegisterConfigTest.php +++ /dev/null @@ -1,41 +0,0 @@ -setName('register-configuration') - ->withConfig([ - $package->getConfigPath('one.php'), - $package->getConfigPath('two.php'), - ]) - ->withConfig($package->getConfigPath('extra/three.php'), false) - ->preventDefaultConfig(); - } -} - -uses(RegisterConfig::class); - -it('register config', function () { - expect(config('one.key')) - ->toBe('one') - ->and(config('two.key')) - ->toBe('two') - ->and(config('three.key')) - ->toBe('three'); -}); - -it('publish config files', function () { - artisan('vendor:publish', ['--tag' => 'register-configuration-config'])->run(); - - assertFileExists(config_path('one.php')); - assertFileExists(config_path('two.php')); - assertFileDoesNotExist(config_path('three.php')); -}); diff --git a/tests/Feature/Package/RegisterMigrationsTest.php b/tests/Feature/Package/RegisterMigrationsTest.php deleted file mode 100644 index 7f60291..0000000 --- a/tests/Feature/Package/RegisterMigrationsTest.php +++ /dev/null @@ -1,45 +0,0 @@ -setName('test-package-migration') - ->withMigrations(); - } -} - -uses(PackageWithMigrations::class); - -it('register migrations', function () { - artisan('migrate')->run(); - - assertdatabasecount('package_test_one', 0); - assertdatabasecount('package_test_two', 0); - assertdatabasecount('package_test_three', 0); - assertdatabasecount('package_test_four', 0); -}); - -it('published migrations', function () { - artisan('vendor:publish', ['--tag' => 'test-package-migration-migrations']) - ->assertSuccessful(); - - assertFileExists(database_path('migrations/create_package_test_one_table.php')); - assertFileExists(database_path('migrations/create_package_test_two_table.php')); - assertFileExists(database_path('migrations/create_package_test_three_table.php')); - assertFileExists(database_path('migrations/create_package_test_four_table.php')); - - artisan('migrate')->assertSuccessful(); - - assertdatabasecount('package_test_one', 0); - assertdatabasecount('package_test_two', 0); - assertdatabasecount('package_test_three', 0); - assertdatabasecount('package_test_four', 0); -}); diff --git a/tests/Feature/Package/UnregisterCommandsTest.php b/tests/Feature/Package/UnregisterCommandsTest.php deleted file mode 100644 index d80d7d9..0000000 --- a/tests/Feature/Package/UnregisterCommandsTest.php +++ /dev/null @@ -1,35 +0,0 @@ -withCommands(PackageTestCommandTwo::class) - ->unregisterCommand(PackageTestCommandOne::class); - } -} - -uses(UnregisterPackageCommand::class); - -it('register commands', function () { - artisan('list') - ->expectsOutputToContain('package:two') - ->doesntExpectOutputToContain('package:one') - ->assertSuccessful(); -}); - -it('run commands', function () { - artisan('package:two') - ->expectsOutput('Package command two') - ->assertSuccessful(); - - artisan('package:one')->run(); -})->throws(CommandNotFoundException::class); diff --git a/tests/Feature/Package/UnregisterConfigTest.php b/tests/Feature/Package/UnregisterConfigTest.php deleted file mode 100644 index b7b4df2..0000000 --- a/tests/Feature/Package/UnregisterConfigTest.php +++ /dev/null @@ -1,40 +0,0 @@ -setName('unregistered-unpublished-configuration') - ->withConfig($package->getConfigPath('extra/three.php')) - ->unregisterConfig('one.php') - ->unpublishConfig('three.php'); - } -} - -uses(UnregisterConfig::class); - -test('un-register config', function () { - artisan('vendor:publish', ['--tag' => 'unregistered-unpublished-configuration-config'])->run(); - - expect(config('one.key')) - ->toBeNull(); - - assertFileDoesNotExist(config_path('one.php')); -}); - -test('un-publish config', function () { - artisan('vendor:publish', ['--tag' => 'unregistered-unpublished-configuration-config'])->run(); - - assertFileDoesNotExist(config_path('three.key')); - - expect(config('three.key')) - ->toBe('three'); -}); diff --git a/tests/Feature/Package/UnregisterMigrationTest.php b/tests/Feature/Package/UnregisterMigrationTest.php deleted file mode 100644 index c2a52e4..0000000 --- a/tests/Feature/Package/UnregisterMigrationTest.php +++ /dev/null @@ -1,42 +0,0 @@ -setName('unregister-migration') - ->withMigrations() - ->unregisterMigration('create_package_test_one_table.php') - ->unpublishMigration('create_package_test_two_table.php'); - } -} - -uses(UnregisterMigrationTest::class); - -test('un-register migrations', function () { - artisan('migrate')->run(); - - // Will throw and error if the database not exist. - assertDatabaseCount('package_test_one', 0); -})->throws(QueryException::class); - -test('un-publish migration', function () { - artisan('vendor:publish', ['--tag' => 'unregister-migration-migrations'])->run(); - - assertFileDoesNotExist(database_path('migrations/create_package_test_one_table.php')); - assertFileDoesNotExist(database_path('migrations/create_package_test_two_table.php')); - - artisan('migrate')->run(); - - assertDatabaseCount('package_test_two', 0); -}); diff --git a/tests/Feature/PackageRegisterConfigTest.php b/tests/Feature/PackageRegisterConfigTest.php new file mode 100644 index 0000000..ebaf885 --- /dev/null +++ b/tests/Feature/PackageRegisterConfigTest.php @@ -0,0 +1,65 @@ +setName('package') + ->withConfig($package->getConfigPath('extra/three.php')) + ->withConfig([$package->getConfigPath('extra/four.php')]) + ->withConfig(); + } +} + +uses(PackageRegisterConfigTest::class); + +test('package has registered files from folder', function () { + expect($this->package) + ->getPublishableConfig() + ->toHaveCount(4) + ->getRegisteredConfig() + ->toHaveCount(4); +}); + +test('package register config files without publishing it', function () { + assertFileDoesNotExist(config_path('one.php')); + assertFileDoesNotExist(config_path('two.php')); + assertFileDoesNotExist(config_path('three.php')); + + expect(config()) + ->get('one.key') + ->toBe('one') + ->get('two.key') + ->toBe('two') + ->get('three.key') + ->toBe('three') + ->and(config_path('one.php')) + ->not->toBeFile() + ->and(config_path('two.php')) + ->not->toBeFile() + ->and(config_path('three.php')) + ->not->toBeFile(); +}); + +it('publish package config files', function () { + artisan('vendor:publish', ['--tag' => 'package-config'])->assertOk(); + + expect(config_path('one.php')) + ->toBeFile() + ->and(config_path('two.php')) + ->toBeFile() + ->and(config_path('three.php')) + ->toBeFile() + ->and(config()) + ->get('one.key') + ->toBe('one') + ->get('two.key') + ->toBe('two') + ->get('three.key') + ->toBe('three'); +}); diff --git a/tests/Feature/PackageUnregisterConfigTest.php b/tests/Feature/PackageUnregisterConfigTest.php new file mode 100644 index 0000000..90a7c8e --- /dev/null +++ b/tests/Feature/PackageUnregisterConfigTest.php @@ -0,0 +1,66 @@ +setName('unregister-package') + ->withConfig([ + $package->getConfigPath('one.php'), + $package->getConfigPath('two.php'), + ]) + ->withConfig($package->getConfigPath('extra/three.php'), false) + ->withConfig($package->getConfigPath('extra/four.php')) + ->excludeConfig([ + $package->getConfigPath('one.php'), + $package->getConfigPath('two.php'), + ]) + ->preventDefaultConfig(); + } +} + +uses(PackageUnRegisterConfigTest::class); + +test('package has register config files manually', function () { + expect($this->package) + ->getPublishableConfig() + ->toHaveCount(1) + ->getRegisteredConfig() + ->toHaveCount(2); +}); + +test('package has no default config files register', function () { + expect(config()) + ->get('one.key') + ->toBeNull() + ->get('two.key') + ->toBeNull() + ->get('three.key') + ->toBe('three') + ->get('four.key') + ->toBe('four'); +}); + +it('publish just one config file', function () { + assertFileDoesNotExist(config_path('four.php')); + + artisan('vendor:publish', ['--tag' => 'unregister-package-config']) + ->assertSuccessful(); + + expect(config()) + ->get('three.key') + ->toBe('three') + ->get('four.key') + ->toBe('four'); + + assertFileDoesNotExist(config_path('three.php')); + assertFileExists(config_path('four.php')); +}); + diff --git a/tests/Pest.php b/tests/Pest.php index f7f33aa..ca1e2bd 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -12,9 +12,12 @@ */ use Illuminate\Foundation\Testing\RefreshDatabase; -use Workbench\Tests\ServiceProviderTest; +use Workbench\Tests\PackageServiceProviderTest; -uses(ServiceProviderTest::class, RefreshDatabase::class)->in('Feature'); +uses( + PackageServiceProviderTest::class, + RefreshDatabase::class +)->in('Feature'); /* |-------------------------------------------------------------------------- diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php new file mode 100644 index 0000000..b71a4aa --- /dev/null +++ b/tests/Unit/HelpersTest.php @@ -0,0 +1,13 @@ + ['join', 'a', 'path', 'join/a/path'], + 'path two' => ['/this', 'is', '../weird/', '/this/is/../weird'], +]); + +it('join paths', function (string ...$data) { + expect(join_paths($data[0], $data[1], $data[2])) + ->toBe($data[3]); +})->with('paths'); diff --git a/tests/Unit/TestHelpers.php b/tests/Unit/TestHelpers.php deleted file mode 100644 index 7da1ccd..0000000 --- a/tests/Unit/TestHelpers.php +++ /dev/null @@ -1,19 +0,0 @@ -toBe($data[3]); -}) - ->with('paths'); - -it('return absolute path', function () { - $configFolder = join_paths(__DIR__, '..', '..', 'workbench', 'config'); - - expect(absolute('one.php', $configFolder)) - ->toBe(join_paths($configFolder, 'one.php')); -}); diff --git a/workbench/app/.gitkeep b/workbench/app/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/workbench/app/Console/Commands/Extras/PackageTestCommandThree.php b/workbench/app/Console/Commands/Extras/PackageTestCommandThree.php deleted file mode 100644 index 1c8f16b..0000000 --- a/workbench/app/Console/Commands/Extras/PackageTestCommandThree.php +++ /dev/null @@ -1,32 +0,0 @@ -info('Package command three'); - - return self::SUCCESS; - } -} diff --git a/workbench/app/Console/Commands/Extras/PackageTestCommandTwo.php b/workbench/app/Console/Commands/Extras/PackageTestCommandTwo.php deleted file mode 100644 index a622b87..0000000 --- a/workbench/app/Console/Commands/Extras/PackageTestCommandTwo.php +++ /dev/null @@ -1,32 +0,0 @@ -info('Package command two'); - - return self::SUCCESS; - } -} diff --git a/workbench/app/Console/Commands/PackageTestCommandFour.php b/workbench/app/Console/Commands/PackageTestCommandFour.php deleted file mode 100644 index 4933d02..0000000 --- a/workbench/app/Console/Commands/PackageTestCommandFour.php +++ /dev/null @@ -1,32 +0,0 @@ -info('Package command four'); - - return self::SUCCESS; - } -} diff --git a/workbench/app/Console/Commands/PackageTestCommandOne.php b/workbench/app/Console/Commands/PackageTestCommandOne.php deleted file mode 100644 index 797da49..0000000 --- a/workbench/app/Console/Commands/PackageTestCommandOne.php +++ /dev/null @@ -1,32 +0,0 @@ -info('Package command one'); - - return self::SUCCESS; - } -} diff --git a/workbench/app/Nested/Console/Commands/ProjectTestCommandOne.php b/workbench/app/Nested/Console/Commands/ProjectTestCommandOne.php deleted file mode 100644 index 04a5a61..0000000 --- a/workbench/app/Nested/Console/Commands/ProjectTestCommandOne.php +++ /dev/null @@ -1,32 +0,0 @@ -info('Project command one'); - - return self::SUCCESS; - } -} diff --git a/workbench/app/Nested/Console/Commands/ProjectTestCommandTwo.php b/workbench/app/Nested/Console/Commands/ProjectTestCommandTwo.php deleted file mode 100644 index 8a648d5..0000000 --- a/workbench/app/Nested/Console/Commands/ProjectTestCommandTwo.php +++ /dev/null @@ -1,32 +0,0 @@ -info('Project command two'); - - return self::SUCCESS; - } -} diff --git a/workbench/app/Nested/NestedServiceProvider.php b/workbench/app/Nested/NestedServiceProvider.php deleted file mode 100644 index ac8e6d0..0000000 --- a/workbench/app/Nested/NestedServiceProvider.php +++ /dev/null @@ -1,18 +0,0 @@ - null; - - $configClosure($package); - } -} diff --git a/workbench/app/PackageServiceProvider.php b/workbench/app/PackageServiceProvider.php new file mode 100644 index 0000000..0184d7f --- /dev/null +++ b/workbench/app/PackageServiceProvider.php @@ -0,0 +1,20 @@ + null; + + forward_static_call(static::$configureClosure, $package); + } +} diff --git a/workbench/app/ServiceProvider.php b/workbench/app/ServiceProvider.php deleted file mode 100644 index 8d34a98..0000000 --- a/workbench/app/ServiceProvider.php +++ /dev/null @@ -1,18 +0,0 @@ - null; - - $configClosure($package); - } -} diff --git a/workbench/config/extra/four.php b/workbench/config/extra/four.php new file mode 100644 index 0000000..5f34451 --- /dev/null +++ b/workbench/config/extra/four.php @@ -0,0 +1,5 @@ + 'four', +]; diff --git a/workbench/config/nested/nested-one.php b/workbench/config/nested/nested-one.php deleted file mode 100644 index e533c21..0000000 --- a/workbench/config/nested/nested-one.php +++ /dev/null @@ -1,5 +0,0 @@ - 'nested-one', -]; diff --git a/workbench/config/nested/nested-two.php b/workbench/config/nested/nested-two.php deleted file mode 100644 index 1a9b748..0000000 --- a/workbench/config/nested/nested-two.php +++ /dev/null @@ -1,5 +0,0 @@ - 'nested-two', -]; diff --git a/workbench/database/migrations/create_package_test_four_table.php b/workbench/database/migrations/create_package_test_four_table.php deleted file mode 100644 index 9faeba8..0000000 --- a/workbench/database/migrations/create_package_test_four_table.php +++ /dev/null @@ -1,31 +0,0 @@ -id(); - $table->timestamps(); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::dropIfExists('package_tests'); - } -}; diff --git a/workbench/database/migrations/nested/create_nested_test_one_table.php b/workbench/database/migrations/nested/create_nested_test_one_table.php deleted file mode 100644 index 40735c5..0000000 --- a/workbench/database/migrations/nested/create_nested_test_one_table.php +++ /dev/null @@ -1,31 +0,0 @@ -id(); - $table->timestamps(); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::dropIfExists('nested_tests'); - } -}; diff --git a/workbench/database/migrations/nested/create_nested_test_two_table.php b/workbench/database/migrations/nested/create_nested_test_two_table.php deleted file mode 100644 index 7e0b4db..0000000 --- a/workbench/database/migrations/nested/create_nested_test_two_table.php +++ /dev/null @@ -1,31 +0,0 @@ -id(); - $table->timestamps(); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::dropIfExists('nested_tests'); - } -}; diff --git a/workbench/tests/ServiceProviderTest.php b/workbench/tests/PackageServiceProviderTest.php similarity index 68% rename from workbench/tests/ServiceProviderTest.php rename to workbench/tests/PackageServiceProviderTest.php index 3d3e926..49574c2 100644 --- a/workbench/tests/ServiceProviderTest.php +++ b/workbench/tests/PackageServiceProviderTest.php @@ -7,25 +7,29 @@ use Illuminate\Support\Facades\File; use Symfony\Component\Finder\SplFileInfo; use Workbench\App\Nested\NestedServiceProvider; +use Workbench\App\PackageServiceProvider; use Workbench\App\ServiceProvider; use function Spatie\PestPluginTestTime\testTime; -abstract class ServiceProviderTest extends TestCase +abstract class PackageServiceProviderTest extends TestCase { + protected Package $package; + protected static array $configFilesNames = [ 'one.php', 'two.php', 'three.php', - 'nested-one.php', - 'nested-two.php', + 'four.php', ]; protected function setUp(): void { - ServiceProvider::$configurePackageUsing = fn (Package $package) => $this->configurePackage($package); + PackageServiceProvider::$configureClosure = function (Package $package) { + $this->package = $package; - NestedServiceProvider::$configureNestedUsing = fn (Package $package) => $this->configureNestedService($package); + $this->configurePackage($package); + }; testTime()->freeze('2023-01-01 00:00:00'); @@ -34,21 +38,12 @@ protected function setUp(): void $this->deletePublishable(); } - protected function configurePackage(Package $package): void - { - // - } - - protected function configureNestedService(Package $package): void - { - // - } + abstract protected function configurePackage(Package $package): void; protected function getPackageProviders($app): array { return [ - NestedServiceProvider::class, - ServiceProvider::class, + PackageServiceProvider::class, ]; } From 90704b1feef6b72916c21bb3f62bd54296b59b13 Mon Sep 17 00:00:00 2001 From: asciito Date: Sat, 30 Sep 2023 05:46:30 +0000 Subject: [PATCH 2/8] Fix styling --- src/Package/Concerns/HasConfig.php | 1 - src/Package/Package.php | 2 -- tests/Feature/PackageRegisterConfigTest.php | 35 ++++++++++--------- tests/Feature/PackageUnregisterConfigTest.php | 19 +++++----- tests/Unit/HelpersTest.php | 2 -- workbench/app/PackageServiceProvider.php | 2 +- .../tests/PackageServiceProviderTest.php | 2 -- 7 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/Package/Concerns/HasConfig.php b/src/Package/Concerns/HasConfig.php index 98342f0..d23ea0f 100644 --- a/src/Package/Concerns/HasConfig.php +++ b/src/Package/Concerns/HasConfig.php @@ -52,7 +52,6 @@ public function getRegisteredConfig(): Collection ->filter(fn (string $config) => ! in_array($config, $this->getExclude('config')->all())); } - public function getPublishableConfig(): Collection { $files = $this->getDefaultConfigFiles(); diff --git a/src/Package/Package.php b/src/Package/Package.php index ff9655d..fe1d524 100644 --- a/src/Package/Package.php +++ b/src/Package/Package.php @@ -138,7 +138,6 @@ protected function register(string $component, mixed $data): static return $this; } - protected function getExclude(string $component): Collection { $key = $this->prefixWithPackageName($component, '.'); @@ -168,5 +167,4 @@ public function getFilesFrom(string $path): Collection ->filter(fn (SplFileInfo $file) => $file->getExtension() === 'php') ->map(fn (SplFileInfo $file): string => $file); } - } diff --git a/tests/Feature/PackageRegisterConfigTest.php b/tests/Feature/PackageRegisterConfigTest.php index ebaf885..bfbb122 100644 --- a/tests/Feature/PackageRegisterConfigTest.php +++ b/tests/Feature/PackageRegisterConfigTest.php @@ -1,6 +1,7 @@ package) ->getPublishableConfig() - ->toHaveCount(4) + ->toHaveCount(4) ->getRegisteredConfig() - ->toHaveCount(4); + ->toHaveCount(4); }); test('package register config files without publishing it', function () { @@ -33,33 +34,33 @@ protected function configurePackage(Package $package): void expect(config()) ->get('one.key') - ->toBe('one') + ->toBe('one') ->get('two.key') - ->toBe('two') + ->toBe('two') ->get('three.key') - ->toBe('three') + ->toBe('three') ->and(config_path('one.php')) - ->not->toBeFile() + ->not->toBeFile() ->and(config_path('two.php')) - ->not->toBeFile() + ->not->toBeFile() ->and(config_path('three.php')) - ->not->toBeFile(); + ->not->toBeFile(); }); it('publish package config files', function () { artisan('vendor:publish', ['--tag' => 'package-config'])->assertOk(); expect(config_path('one.php')) - ->toBeFile() + ->toBeFile() ->and(config_path('two.php')) - ->toBeFile() + ->toBeFile() ->and(config_path('three.php')) - ->toBeFile() + ->toBeFile() ->and(config()) - ->get('one.key') - ->toBe('one') - ->get('two.key') - ->toBe('two') - ->get('three.key') - ->toBe('three'); + ->get('one.key') + ->toBe('one') + ->get('two.key') + ->toBe('two') + ->get('three.key') + ->toBe('three'); }); diff --git a/tests/Feature/PackageUnregisterConfigTest.php b/tests/Feature/PackageUnregisterConfigTest.php index 90a7c8e..c47019c 100644 --- a/tests/Feature/PackageUnregisterConfigTest.php +++ b/tests/Feature/PackageUnregisterConfigTest.php @@ -1,7 +1,7 @@ package) ->getPublishableConfig() - ->toHaveCount(1) + ->toHaveCount(1) ->getRegisteredConfig() - ->toHaveCount(2); + ->toHaveCount(2); }); test('package has no default config files register', function () { expect(config()) ->get('one.key') - ->toBeNull() + ->toBeNull() ->get('two.key') - ->toBeNull() + ->toBeNull() ->get('three.key') - ->toBe('three') + ->toBe('three') ->get('four.key') - ->toBe('four'); + ->toBe('four'); }); it('publish just one config file', function () { @@ -56,11 +56,10 @@ protected function configurePackage(Package $package): void expect(config()) ->get('three.key') - ->toBe('three') + ->toBe('three') ->get('four.key') - ->toBe('four'); + ->toBe('four'); assertFileDoesNotExist(config_path('three.php')); assertFileExists(config_path('four.php')); }); - diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php index b71a4aa..f962cd5 100644 --- a/tests/Unit/HelpersTest.php +++ b/tests/Unit/HelpersTest.php @@ -1,7 +1,5 @@ ['join', 'a', 'path', 'join/a/path'], 'path two' => ['/this', 'is', '../weird/', '/this/is/../weird'], diff --git a/workbench/app/PackageServiceProvider.php b/workbench/app/PackageServiceProvider.php index 0184d7f..2bd2a28 100644 --- a/workbench/app/PackageServiceProvider.php +++ b/workbench/app/PackageServiceProvider.php @@ -9,7 +9,7 @@ class PackageServiceProvider extends \Asciito\LaravelPackage\Package\PackageServ public static \Closure $configureClosure; /** - * @inheritDoc + * {@inheritDoc} */ protected function configurePackage(Package $package): void { diff --git a/workbench/tests/PackageServiceProviderTest.php b/workbench/tests/PackageServiceProviderTest.php index 49574c2..67570d1 100644 --- a/workbench/tests/PackageServiceProviderTest.php +++ b/workbench/tests/PackageServiceProviderTest.php @@ -6,9 +6,7 @@ use Asciito\LaravelPackage\Tests\TestCase; use Illuminate\Support\Facades\File; use Symfony\Component\Finder\SplFileInfo; -use Workbench\App\Nested\NestedServiceProvider; use Workbench\App\PackageServiceProvider; -use Workbench\App\ServiceProvider; use function Spatie\PestPluginTestTime\testTime; From ab7abe2b52fdef05e061570f81508b3f95a0e7b0 Mon Sep 17 00:00:00 2001 From: asciito Date: Mon, 2 Oct 2023 17:22:18 -0600 Subject: [PATCH 3/8] chore: refactor HelpersTest Remove unused imports Signed-off-by: asciito --- src/helpers.php | 3 +++ tests/Unit/HelpersTest.php | 15 ++++++++++----- workbench/tests/PackageServiceProviderTest.php | 2 ++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/helpers.php b/src/helpers.php index 20ed1d0..8beb5b1 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,6 +1,9 @@ ['join', 'a', 'path', 'join/a/path'], - 'path two' => ['/this', 'is', '../weird/', '/this/is/../weird'], + 'path one' => [ + 'values' => ['join', 'a', 'path'], + 'toBe' => 'join/a/path', + ], + 'path two' => [ + 'values' => ['/this', 'is', '../weird/'], + 'toBe' => '/this/is/../weird', + ], ]); -it('join paths', function (string ...$data) { - expect(join_paths($data[0], $data[1], $data[2])) - ->toBe($data[3]); +it('join paths', function (array $paths, string $result) { + expect(join_paths(...$paths))->toBe($result); })->with('paths'); diff --git a/workbench/tests/PackageServiceProviderTest.php b/workbench/tests/PackageServiceProviderTest.php index 67570d1..6976303 100644 --- a/workbench/tests/PackageServiceProviderTest.php +++ b/workbench/tests/PackageServiceProviderTest.php @@ -4,7 +4,9 @@ use Asciito\LaravelPackage\Package\Package; use Asciito\LaravelPackage\Tests\TestCase; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\File; +use Spatie\TestTime\TestTime; use Symfony\Component\Finder\SplFileInfo; use Workbench\App\PackageServiceProvider; From a7b6a48de4897654f40b3d5eeab792d09403e7fe Mon Sep 17 00:00:00 2001 From: asciito Date: Mon, 2 Oct 2023 17:25:50 -0600 Subject: [PATCH 4/8] chore: refactor config component This commit also improves the tests folder structure Signed-off-by: asciito --- src/Package/Concerns/HasConfig.php | 19 +++---- src/Package/PackageServiceProvider.php | 4 +- .../PackageRegisterConfigTest.php | 50 +++++++++++-------- .../PackageUnregisterConfigTest.php | 26 ++++++---- 4 files changed, 54 insertions(+), 45 deletions(-) rename tests/Feature/{ => Config}/PackageRegisterConfigTest.php (62%) rename tests/Feature/{ => Config}/PackageUnregisterConfigTest.php (78%) diff --git a/src/Package/Concerns/HasConfig.php b/src/Package/Concerns/HasConfig.php index d23ea0f..1a3e879 100644 --- a/src/Package/Concerns/HasConfig.php +++ b/src/Package/Concerns/HasConfig.php @@ -48,21 +48,22 @@ public function getRegisteredConfig(): Collection return $files ->merge($this->getRegister('config')) - ->keys() - ->filter(fn (string $config) => ! in_array($config, $this->getExclude('config')->all())); + ->except($this->getExclude('config')) + ->keys(); } + public function getPublishableConfig(): Collection { $files = $this->getDefaultConfigFiles(); - return $files->merge($this->getRegister('config')) - ->filter(function (bool $publish, string $path) { - $include = ! in_array($path, $this->getExclude('config')->all()); - - return $include && $publish; - }) - ->keys(); + return $files + ->merge($this->getRegister('config')) + ->filter() + ->except($this->getExclude('config')) + ->mapWithKeys(fn (bool $_, string $path) => [ + $path => config_path(basename($path)) + ]); } public function getConfigPath(string $path = ''): string diff --git a/src/Package/PackageServiceProvider.php b/src/Package/PackageServiceProvider.php index f0c32e9..70498a9 100644 --- a/src/Package/PackageServiceProvider.php +++ b/src/Package/PackageServiceProvider.php @@ -132,9 +132,7 @@ protected function publishesConfig(Package $package): void { if ($package->hasConfig()) { $this->publishes( - $this->package->getPublishableConfig() - ->mapWithKeys(fn (string $config) => [$config => config_path(basename($config))]) - ->all(), + $this->package->getPublishableConfig()->all(), $package->prefixWithPackageName('config'), ); } diff --git a/tests/Feature/PackageRegisterConfigTest.php b/tests/Feature/Config/PackageRegisterConfigTest.php similarity index 62% rename from tests/Feature/PackageRegisterConfigTest.php rename to tests/Feature/Config/PackageRegisterConfigTest.php index bfbb122..7d6cbe1 100644 --- a/tests/Feature/PackageRegisterConfigTest.php +++ b/tests/Feature/Config/PackageRegisterConfigTest.php @@ -3,7 +3,6 @@ use Asciito\LaravelPackage\Package\Package; use function Pest\Laravel\artisan; -use function PHPUnit\Framework\assertFileDoesNotExist; trait PackageRegisterConfigTest { @@ -22,29 +21,33 @@ protected function configurePackage(Package $package): void test('package has registered files from folder', function () { expect($this->package) ->getPublishableConfig() - ->toHaveCount(4) + ->toHaveCount(4) ->getRegisteredConfig() - ->toHaveCount(4); + ->toHaveCount(4); }); test('package register config files without publishing it', function () { - assertFileDoesNotExist(config_path('one.php')); - assertFileDoesNotExist(config_path('two.php')); - assertFileDoesNotExist(config_path('three.php')); + expect(config_path('one.php')) + ->not->toBeFile() + ->and(config_path('two.php')) + ->not->toBeFile() + + ->and(config_path('three.php')) + ->not->toBeFile() + + ->and(config_path('four.php')) + ->not->toBeFile() - expect(config()) + ->and(config()) ->get('one.key') - ->toBe('one') + ->toBe('one') ->get('two.key') - ->toBe('two') + ->toBe('two') ->get('three.key') - ->toBe('three') - ->and(config_path('one.php')) - ->not->toBeFile() - ->and(config_path('two.php')) - ->not->toBeFile() - ->and(config_path('three.php')) - ->not->toBeFile(); + ->toBe('three') + ->get('four.key') + ->toBe('four'); + }); it('publish package config files', function () { @@ -52,15 +55,18 @@ protected function configurePackage(Package $package): void expect(config_path('one.php')) ->toBeFile() - ->and(config_path('two.php')) + + ->and(config_path('two.php')) ->toBeFile() - ->and(config_path('three.php')) + + ->and(config_path('three.php')) ->toBeFile() - ->and(config()) + + ->and(config()) ->get('one.key') - ->toBe('one') + ->toBe('one') ->get('two.key') - ->toBe('two') + ->toBe('two') ->get('three.key') - ->toBe('three'); + ->toBe('three'); }); diff --git a/tests/Feature/PackageUnregisterConfigTest.php b/tests/Feature/Config/PackageUnregisterConfigTest.php similarity index 78% rename from tests/Feature/PackageUnregisterConfigTest.php rename to tests/Feature/Config/PackageUnregisterConfigTest.php index c47019c..88380c3 100644 --- a/tests/Feature/PackageUnregisterConfigTest.php +++ b/tests/Feature/Config/PackageUnregisterConfigTest.php @@ -31,35 +31,39 @@ protected function configurePackage(Package $package): void test('package has register config files manually', function () { expect($this->package) ->getPublishableConfig() - ->toHaveCount(1) + ->toHaveCount(1) ->getRegisteredConfig() - ->toHaveCount(2); + ->toHaveCount(2); }); test('package has no default config files register', function () { expect(config()) ->get('one.key') - ->toBeNull() + ->toBeNull() ->get('two.key') - ->toBeNull() + ->toBeNull() ->get('three.key') - ->toBe('three') + ->toBe('three') ->get('four.key') - ->toBe('four'); + ->toBe('four'); }); it('publish just one config file', function () { - assertFileDoesNotExist(config_path('four.php')); + expect(config_path('four.php')) + ->not->toBeFile(); artisan('vendor:publish', ['--tag' => 'unregister-package-config']) ->assertSuccessful(); expect(config()) ->get('three.key') - ->toBe('three') + ->toBe('three') ->get('four.key') - ->toBe('four'); + ->toBe('four') - assertFileDoesNotExist(config_path('three.php')); - assertFileExists(config_path('four.php')); + ->and(config_path('three.php')) + ->not->toBeFile() + + ->and(config_path('four.php')) + ->toBeFile(); }); From 0019d469f4f81fa2c01ad4f244f790777edbce65 Mon Sep 17 00:00:00 2001 From: asciito Date: Mon, 2 Oct 2023 17:26:43 -0600 Subject: [PATCH 5/8] chore: refactor migration component Reorder folder structure Improve publish and register migration file Signed-off-by: asciito --- src/Package/Concerns/HasMigrations.php | 104 ++++++++---------- src/Package/Contracts/WithMigrations.php | 29 ++--- src/Package/Package.php | 10 ++ src/Package/PackageServiceProvider.php | 10 +- .../PackageRegisterMigrationTest.php | 76 +++++++++++++ .../PackageUnregisterMigrationTest.php | 58 ++++++++++ .../create_package_test_one_table.php | 2 +- .../create_package_test_two_table.php | 2 +- .../extra/create_package_test_four_table.php | 31 ++++++ .../create_package_test_three_table.php | 2 +- 10 files changed, 239 insertions(+), 85 deletions(-) create mode 100644 tests/Feature/Migration/PackageRegisterMigrationTest.php create mode 100644 tests/Feature/Migration/PackageUnregisterMigrationTest.php create mode 100644 workbench/database/migrations/extra/create_package_test_four_table.php rename workbench/database/migrations/{ => extra}/create_package_test_three_table.php (91%) diff --git a/src/Package/Concerns/HasMigrations.php b/src/Package/Concerns/HasMigrations.php index 86f4eb6..3174ff4 100644 --- a/src/Package/Concerns/HasMigrations.php +++ b/src/Package/Concerns/HasMigrations.php @@ -2,117 +2,101 @@ namespace Asciito\LaravelPackage\Package\Concerns; +use Illuminate\Support\Arr; use Illuminate\Support\Collection; trait HasMigrations { - protected string $migrationsPath; - - protected array $migrations = []; + protected string $migrationPath; protected bool $preventLoadDefaultMigrationFolder = false; - protected bool $shouldIncludeMigrationsFromFolder = false; - - protected array $excludedMigrations = []; - - protected array $unpublishedMigrations = []; + protected bool $shouldIncludeMigrationFromFolder = false; public function hasMigrations(): bool { - return $this->shouldLoadDefaultMigrationsFolder() || filled($this->migrations); + return $this->shouldLoadDefaultMigrationFolder() + || $this->getRegister('migrations')->isNotEmpty(); } - private function shouldLoadDefaultMigrationsFolder(): bool + public function withMigration(string|array $migration = [], bool $publish = true): static { - return ! $this->preventLoadDefaultMigrationFolder && $this->shouldIncludeMigrationsFromFolder; - } + $this->ensureRegistersInitialize('migrations'); - public function withMigrations(string|array $migration = [], bool $publish = true): static - { if (filled($migration)) { - $this->migrations = collect($migration) - ->map(fn (string $migration) => absolute($migration)) - ->filter() - ->mapWithKeys(fn (string $value) => [$value => $publish]) - ->merge($this->migrations) - ->all(); + $this->register( + 'migrations', + Arr::mapWithKeys(Arr::wrap($migration), fn (string $path): array => [$path => $publish]) + ); } - $this->shouldIncludeMigrationsFromFolder = true; + $this->shouldIncludeMigrationFromFolder = true; return $this; } - public function preventDefaultMigrations(): static + public function excludeMigration(string|array $path): static { - $this->preventLoadDefaultMigrationFolder = true; + $this->exclude('migrations', Arr::wrap($path)); return $this; } - public function getRegisteredMigrations(): Collection + public function getRegisteredMigration(): Collection { - $migrations = $this->loadMigrationsDefaultFolder(); + $files = $this->getDefaultMigrationFiles(); - return collect($this->migrations) - ->merge($migrations) - ->filter(function (bool $_, string $migration) { - return ! in_array($migration, $this->excludedMigrations); - }) + return $files + ->merge($this->getRegister('migrations')) + ->except($this->getExclude('migrations')) ->keys(); } - private function loadMigrationsDefaultFolder(): array - { - if (! $this->shouldLoadDefaultMigrationsFolder()) { - return []; - } - return $this->loadFilesFrom($this->getMigrationPath())->all(); + public function getPublishableMigration(): Collection + { + $files = $this->getDefaultMigrationFiles(); + + return $files + ->merge($this->getRegister('migrations')) + ->filter() + ->except($this->getExclude('migrations')) + ->mapWithKeys(fn (bool $_, string $path) => [ + $path => database_path('migrations/'.basename($path)) + ]); } public function getMigrationPath(string $path = ''): string { - return join_paths($this->migrationsPath, $path); + return join_paths($this->migrationPath, $path); } - public function getPublishableMigrations(): Collection + public function setMigrationPath(string $path): static { - $migrations = $this->loadMigrationsDefaultFolder(); + $this->migrationPath = rtrim($path, DIRECTORY_SEPARATOR); - return collect($this->migrations) - ->merge($migrations) - ->filter(function (bool $publish, string $migration) { - return $publish && ! in_array($migration, [...$this->excludedMigrations, ...$this->unpublishedMigrations]); - }) - ->keys(); + return $this; } - public function setMigrationPath(string $path): static + public function getDefaultMigrationFiles(): Collection { - $this->migrationsPath = rtrim($path, DIRECTORY_SEPARATOR); + if (! $this->shouldLoadDefaultMigrationFolder()) { + return collect(); + } - return $this; + return $this->getFilesFrom($this->migrationPath) + ->mapWithKeys(fn (string $path) => [$path => true]); } - /** - * Un-register a previously registered migration - */ - public function unregisterMigration(string $path): static + public function preventDefaultMigration(): static { - $this->excludedMigrations[] = absolute($path, $this->getMigrationPath()); + $this->preventLoadDefaultMigrationFolder = true; return $this; } - /** - * Un-publish a previously published migration - */ - public function unpublishMigration(string $path): static + private function shouldLoadDefaultMigrationFolder(): bool { - $this->unpublishedMigrations[] = absolute($path, $this->getMigrationPath()); - - return $this; + return ! $this->preventLoadDefaultMigrationFolder && $this->shouldIncludeMigrationFromFolder; } } diff --git a/src/Package/Contracts/WithMigrations.php b/src/Package/Contracts/WithMigrations.php index 12a03b4..850cc32 100644 --- a/src/Package/Contracts/WithMigrations.php +++ b/src/Package/Contracts/WithMigrations.php @@ -23,47 +23,48 @@ public function hasMigrations(): bool; * By calling this method this will try to load your migration * file(s) from the default migration folder of your package. * + * @param string|string[] $migration The file(s) you want to register * @param bool $publish if the file(s) should be publishable, by default is true */ - public function withMigrations(string|array $migration = [], bool $publish = true): static; + public function withMigration(string|array $migration = [], bool $publish = true): static; /** - * Prevent publishing the default migrations - * - * Calling this method ensures that the migrations in the default folder - * wouldn't be loaded automatically. + * Exclude a migration from being register */ - public function preventDefaultMigrations(): static; + public function excludeMigration(string|array $path): static; /** * An array with the migration file(s) registered * * @return Collection The migration file(s) registered in the package */ - public function getRegisteredMigrations(): Collection; + public function getRegisteredMigration(): Collection; /** * Get the array with the publishable migration file(s) */ - public function getPublishableMigrations(): Collection; + public function getPublishableMigration(): Collection; /** - * Set the path to the migrations folder + * Set the path to the migration folder */ public function setMigrationPath(string $path): static; /** - * Get the path to the migrations folder + * Get the path to the migration folder */ public function getMigrationPath(string $path = ''): string; /** - * Un-register a previously registered migration + * Get the files from the migration path */ - public function unregisterMigration(string $path): static; + public function getDefaultMigrationFiles(): Collection; /** - * Un-publish a previously published migration + * Prevent publishing the default migration folder + * + * Calling this method ensures that the migration files in the default folder + * wouldn't be loaded automatically. */ - public function unpublishMigration(string $path): static; + public function preventDefaultMigration(): static; } diff --git a/src/Package/Package.php b/src/Package/Package.php index fe1d524..bf7ad56 100644 --- a/src/Package/Package.php +++ b/src/Package/Package.php @@ -10,8 +10,10 @@ use Asciito\LaravelPackage\Package\Contracts\WithMigrations; use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Support\Arr; +use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Illuminate\Support\Facades\File; +use Illuminate\Support\Str; use Symfony\Component\Finder\SplFileInfo; class Package implements WithCommands, WithConfig, WithMigrations @@ -167,4 +169,12 @@ public function getFilesFrom(string $path): Collection ->filter(fn (SplFileInfo $file) => $file->getExtension() === 'php') ->map(fn (SplFileInfo $file): string => $file); } + public function makeMigrationName(string $migration): string + { + if (Str::isMatch('/\d{4}_\d{2}_\d{2}_\d{6}/', $migration)) { + return $migration; + } + + return Carbon::now()->format('Y_m_d_His_').trim($migration, '_'); + } } diff --git a/src/Package/PackageServiceProvider.php b/src/Package/PackageServiceProvider.php index 70498a9..5466984 100644 --- a/src/Package/PackageServiceProvider.php +++ b/src/Package/PackageServiceProvider.php @@ -145,17 +145,11 @@ protected function publishesMigrations(Package $package): void { if ($package->hasMigrations()) { $this->publishes( - $package->getPublishableMigrations() - ->mapWithKeys(function (string $migration) { - $databaseMigration = database_path('migrations/'.basename($migration)); - - return [$migration => $databaseMigration]; - }) - ->all(), + $package->getPublishableMigration()->all(), $package->prefixWithPackageName('migrations') ); - $this->loadMigrationsFrom($package->getRegisteredMigrations()->all()); + $this->loadMigrationsFrom($package->getRegisteredMigration()->all()); } } diff --git a/tests/Feature/Migration/PackageRegisterMigrationTest.php b/tests/Feature/Migration/PackageRegisterMigrationTest.php new file mode 100644 index 0000000..19ed7de --- /dev/null +++ b/tests/Feature/Migration/PackageRegisterMigrationTest.php @@ -0,0 +1,76 @@ +setName('package') + ->withMigration($package->getMigrationPath('extra/create_package_test_three_table.php')) + ->withMigration([$package->getMigrationPath('extra/create_package_test_four_table.php')]); + } +} + +uses(PackageRegisterMigrationTest::class); + +test('package has registered files from folder', function () { + expect($this->package) + ->getPublishableMigration() + ->toHaveCount(4) + ->getRegisteredMigration() + ->toHaveCount(4); +}); + +test('package register migration files without publishing it', function () { + expect(database_path('migrations/create_package_test_one_table.php')) + ->not->toBeFile() + ->and(database_path('migrations/create_package_test_two_table.php')) + ->not->toBeFile() + ->and(database_path('migrations/create_package_test_three_table.php')) + ->not->toBeFile() + ->and(database_path('migrations/create_package_test_four_table.php')) + ->not->toBeFile(); + + artisan('migrate') + ->assertSuccessful(); + + assertDatabaseCount('package_test_one', 0); + assertDatabaseCount('package_test_two', 0); + assertDatabaseCount('package_test_three', 0); + assertDatabaseCount('package_test_four', 0); +}); + +it('publish package migration files', function () { + expect(database_path('migrations/create_package_test_one_table.php')) + ->not->toBeFile() + ->and(database_path('migrations/create_package_test_two_table.php')) + ->not->toBeFile() + ->and(database_path('migrations/create_package_test_three_table.php')) + ->not->toBeFile() + ->and(database_path('migrations/create_package_test_four_table.php')) + ->not->toBeFile(); + + artisan('vendor:publish', ['--tag' => 'package-migrations']); + + expect(database_path('migrations/create_package_test_one_table.php')) + ->toBeFile() + ->and(database_path('migrations/create_package_test_two_table.php')) + ->toBeFile() + ->and(database_path('migrations/create_package_test_three_table.php')) + ->toBeFile() + ->and(database_path('migrations/create_package_test_four_table.php')) + ->toBeFile(); + + artisan('migrate'); + + assertDatabaseCount('package_test_one', 0); + assertDatabaseCount('package_test_two', 0); + assertDatabaseCount('package_test_three', 0); + assertDatabaseCount('package_test_four', 0); +}); + + diff --git a/tests/Feature/Migration/PackageUnregisterMigrationTest.php b/tests/Feature/Migration/PackageUnregisterMigrationTest.php new file mode 100644 index 0000000..032fe81 --- /dev/null +++ b/tests/Feature/Migration/PackageUnregisterMigrationTest.php @@ -0,0 +1,58 @@ +setName('unregister-package') + ->withMigration([ + $package->getMigrationPath('extra/create_package_test_three_table.php'), + $package->getMigrationPath('extra/create_package_test_four_table.php'), + ]) + ->excludeMigration($package->getMigrationPath('extra/create_package_test_four_table.php')) + ->preventDefaultMigration(); + } +} + +uses(PackageUnRegistermigrationTest::class); + +test('package has register config files manually', function () { + expect($this->package) + ->getPublishableMigration() + ->toHaveCount(1) + ->getRegisteredMigration() + ->toHaveCount(1); +}); + +test('package has no default migration files register', function () { + expect(fn () => DB::table('package_test_one')->get()) + ->toThrow(QueryException::class) + ->and(fn () => DB::table('package_test_two')->get()) + ->toThrow(QueryException::class); +}); + +it('publish just one migration file', function () { + expect(database_path('migrations/create_package_test_three_table.php')) + ->not->toBeFile(); + + artisan('vendor:publish', ['--tag' => 'unregister-package-migrations']) + ->assertSuccessful(); + + expect(database_path('migrations/create_package_test_three_table.php')) + ->toBeFile() + ->and(database_path('migrations/create_package_test_four_table.php')) + ->not->toBeFile(); + + artisan('migrate')->assertSuccessful(); + + assertDatabaseCount('package_test_three', 0); +}); diff --git a/workbench/database/migrations/create_package_test_one_table.php b/workbench/database/migrations/create_package_test_one_table.php index 91400b9..d3fa8c0 100644 --- a/workbench/database/migrations/create_package_test_one_table.php +++ b/workbench/database/migrations/create_package_test_one_table.php @@ -26,6 +26,6 @@ public function up() */ public function down() { - Schema::dropIfExists('package_tests'); + Schema::dropIfExists('package_test_one'); } }; diff --git a/workbench/database/migrations/create_package_test_two_table.php b/workbench/database/migrations/create_package_test_two_table.php index 91fdb97..9b04a60 100644 --- a/workbench/database/migrations/create_package_test_two_table.php +++ b/workbench/database/migrations/create_package_test_two_table.php @@ -26,6 +26,6 @@ public function up() */ public function down() { - Schema::dropIfExists('package_tests'); + Schema::dropIfExists('package_test_two'); } }; diff --git a/workbench/database/migrations/extra/create_package_test_four_table.php b/workbench/database/migrations/extra/create_package_test_four_table.php new file mode 100644 index 0000000..e29fcc3 --- /dev/null +++ b/workbench/database/migrations/extra/create_package_test_four_table.php @@ -0,0 +1,31 @@ +id(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('package_test_four'); + } +}; diff --git a/workbench/database/migrations/create_package_test_three_table.php b/workbench/database/migrations/extra/create_package_test_three_table.php similarity index 91% rename from workbench/database/migrations/create_package_test_three_table.php rename to workbench/database/migrations/extra/create_package_test_three_table.php index 5c2d69e..3388aa5 100644 --- a/workbench/database/migrations/create_package_test_three_table.php +++ b/workbench/database/migrations/extra/create_package_test_three_table.php @@ -26,6 +26,6 @@ public function up() */ public function down() { - Schema::dropIfExists('package_tests'); + Schema::dropIfExists('package_test_three'); } }; From 77fa38736456367ca782e82445f05ccee0eda32e Mon Sep 17 00:00:00 2001 From: asciito Date: Mon, 2 Oct 2023 23:27:19 +0000 Subject: [PATCH 6/8] Fix styling --- src/Package/Concerns/HasConfig.php | 3 +- src/Package/Concerns/HasMigrations.php | 3 +- src/Package/Package.php | 1 + src/helpers.php | 3 -- .../Config/PackageRegisterConfigTest.php | 38 ++++++++----------- .../Config/PackageUnregisterConfigTest.php | 24 +++++------- .../PackageRegisterMigrationTest.php | 25 ++++++------ .../PackageUnregisterMigrationTest.php | 11 +++--- .../tests/PackageServiceProviderTest.php | 2 - 9 files changed, 46 insertions(+), 64 deletions(-) diff --git a/src/Package/Concerns/HasConfig.php b/src/Package/Concerns/HasConfig.php index 1a3e879..212ea7f 100644 --- a/src/Package/Concerns/HasConfig.php +++ b/src/Package/Concerns/HasConfig.php @@ -52,7 +52,6 @@ public function getRegisteredConfig(): Collection ->keys(); } - public function getPublishableConfig(): Collection { $files = $this->getDefaultConfigFiles(); @@ -62,7 +61,7 @@ public function getPublishableConfig(): Collection ->filter() ->except($this->getExclude('config')) ->mapWithKeys(fn (bool $_, string $path) => [ - $path => config_path(basename($path)) + $path => config_path(basename($path)), ]); } diff --git a/src/Package/Concerns/HasMigrations.php b/src/Package/Concerns/HasMigrations.php index 3174ff4..e5b4eee 100644 --- a/src/Package/Concerns/HasMigrations.php +++ b/src/Package/Concerns/HasMigrations.php @@ -52,7 +52,6 @@ public function getRegisteredMigration(): Collection ->keys(); } - public function getPublishableMigration(): Collection { $files = $this->getDefaultMigrationFiles(); @@ -62,7 +61,7 @@ public function getPublishableMigration(): Collection ->filter() ->except($this->getExclude('migrations')) ->mapWithKeys(fn (bool $_, string $path) => [ - $path => database_path('migrations/'.basename($path)) + $path => database_path('migrations/'.basename($path)), ]); } diff --git a/src/Package/Package.php b/src/Package/Package.php index bf7ad56..2799415 100644 --- a/src/Package/Package.php +++ b/src/Package/Package.php @@ -169,6 +169,7 @@ public function getFilesFrom(string $path): Collection ->filter(fn (SplFileInfo $file) => $file->getExtension() === 'php') ->map(fn (SplFileInfo $file): string => $file); } + public function makeMigrationName(string $migration): string { if (Str::isMatch('/\d{4}_\d{2}_\d{2}_\d{6}/', $migration)) { diff --git a/src/helpers.php b/src/helpers.php index 8beb5b1..20ed1d0 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,9 +1,6 @@ package) ->getPublishableConfig() - ->toHaveCount(4) + ->toHaveCount(4) ->getRegisteredConfig() - ->toHaveCount(4); + ->toHaveCount(4); }); test('package register config files without publishing it', function () { expect(config_path('one.php')) ->not->toBeFile() - ->and(config_path('two.php')) + ->and(config_path('two.php')) ->not->toBeFile() - - ->and(config_path('three.php')) + ->and(config_path('three.php')) ->not->toBeFile() - - ->and(config_path('four.php')) + ->and(config_path('four.php')) ->not->toBeFile() - - ->and(config()) + ->and(config()) ->get('one.key') - ->toBe('one') + ->toBe('one') ->get('two.key') - ->toBe('two') + ->toBe('two') ->get('three.key') - ->toBe('three') + ->toBe('three') ->get('four.key') - ->toBe('four'); + ->toBe('four'); }); @@ -55,18 +52,15 @@ protected function configurePackage(Package $package): void expect(config_path('one.php')) ->toBeFile() - - ->and(config_path('two.php')) + ->and(config_path('two.php')) ->toBeFile() - - ->and(config_path('three.php')) + ->and(config_path('three.php')) ->toBeFile() - - ->and(config()) + ->and(config()) ->get('one.key') - ->toBe('one') + ->toBe('one') ->get('two.key') - ->toBe('two') + ->toBe('two') ->get('three.key') - ->toBe('three'); + ->toBe('three'); }); diff --git a/tests/Feature/Config/PackageUnregisterConfigTest.php b/tests/Feature/Config/PackageUnregisterConfigTest.php index 88380c3..00ce3eb 100644 --- a/tests/Feature/Config/PackageUnregisterConfigTest.php +++ b/tests/Feature/Config/PackageUnregisterConfigTest.php @@ -3,8 +3,6 @@ use Asciito\LaravelPackage\Package\Package; use function Pest\Laravel\artisan; -use function PHPUnit\Framework\assertFileDoesNotExist; -use function PHPUnit\Framework\assertFileExists; trait PackageUnRegisterConfigTest { @@ -31,21 +29,21 @@ protected function configurePackage(Package $package): void test('package has register config files manually', function () { expect($this->package) ->getPublishableConfig() - ->toHaveCount(1) + ->toHaveCount(1) ->getRegisteredConfig() - ->toHaveCount(2); + ->toHaveCount(2); }); test('package has no default config files register', function () { expect(config()) ->get('one.key') - ->toBeNull() + ->toBeNull() ->get('two.key') - ->toBeNull() + ->toBeNull() ->get('three.key') - ->toBe('three') + ->toBe('three') ->get('four.key') - ->toBe('four'); + ->toBe('four'); }); it('publish just one config file', function () { @@ -57,13 +55,11 @@ protected function configurePackage(Package $package): void expect(config()) ->get('three.key') - ->toBe('three') + ->toBe('three') ->get('four.key') - ->toBe('four') - - ->and(config_path('three.php')) + ->toBe('four') + ->and(config_path('three.php')) ->not->toBeFile() - - ->and(config_path('four.php')) + ->and(config_path('four.php')) ->toBeFile(); }); diff --git a/tests/Feature/Migration/PackageRegisterMigrationTest.php b/tests/Feature/Migration/PackageRegisterMigrationTest.php index 19ed7de..6ce8577 100644 --- a/tests/Feature/Migration/PackageRegisterMigrationTest.php +++ b/tests/Feature/Migration/PackageRegisterMigrationTest.php @@ -1,6 +1,7 @@ package) ->getPublishableMigration() - ->toHaveCount(4) + ->toHaveCount(4) ->getRegisteredMigration() - ->toHaveCount(4); + ->toHaveCount(4); }); test('package register migration files without publishing it', function () { expect(database_path('migrations/create_package_test_one_table.php')) ->not->toBeFile() - ->and(database_path('migrations/create_package_test_two_table.php')) + ->and(database_path('migrations/create_package_test_two_table.php')) ->not->toBeFile() - ->and(database_path('migrations/create_package_test_three_table.php')) + ->and(database_path('migrations/create_package_test_three_table.php')) ->not->toBeFile() - ->and(database_path('migrations/create_package_test_four_table.php')) + ->and(database_path('migrations/create_package_test_four_table.php')) ->not->toBeFile(); artisan('migrate') @@ -47,22 +48,22 @@ protected function configurePackage(Package $package): void it('publish package migration files', function () { expect(database_path('migrations/create_package_test_one_table.php')) ->not->toBeFile() - ->and(database_path('migrations/create_package_test_two_table.php')) + ->and(database_path('migrations/create_package_test_two_table.php')) ->not->toBeFile() - ->and(database_path('migrations/create_package_test_three_table.php')) + ->and(database_path('migrations/create_package_test_three_table.php')) ->not->toBeFile() - ->and(database_path('migrations/create_package_test_four_table.php')) + ->and(database_path('migrations/create_package_test_four_table.php')) ->not->toBeFile(); artisan('vendor:publish', ['--tag' => 'package-migrations']); expect(database_path('migrations/create_package_test_one_table.php')) ->toBeFile() - ->and(database_path('migrations/create_package_test_two_table.php')) + ->and(database_path('migrations/create_package_test_two_table.php')) ->toBeFile() - ->and(database_path('migrations/create_package_test_three_table.php')) + ->and(database_path('migrations/create_package_test_three_table.php')) ->toBeFile() - ->and(database_path('migrations/create_package_test_four_table.php')) + ->and(database_path('migrations/create_package_test_four_table.php')) ->toBeFile(); artisan('migrate'); @@ -72,5 +73,3 @@ protected function configurePackage(Package $package): void assertDatabaseCount('package_test_three', 0); assertDatabaseCount('package_test_four', 0); }); - - diff --git a/tests/Feature/Migration/PackageUnregisterMigrationTest.php b/tests/Feature/Migration/PackageUnregisterMigrationTest.php index 032fe81..d825634 100644 --- a/tests/Feature/Migration/PackageUnregisterMigrationTest.php +++ b/tests/Feature/Migration/PackageUnregisterMigrationTest.php @@ -1,10 +1,9 @@ package) ->getPublishableMigration() - ->toHaveCount(1) + ->toHaveCount(1) ->getRegisteredMigration() - ->toHaveCount(1); + ->toHaveCount(1); }); test('package has no default migration files register', function () { expect(fn () => DB::table('package_test_one')->get()) ->toThrow(QueryException::class) - ->and(fn () => DB::table('package_test_two')->get()) + ->and(fn () => DB::table('package_test_two')->get()) ->toThrow(QueryException::class); }); @@ -49,7 +48,7 @@ protected function configurePackage(Package $package): void expect(database_path('migrations/create_package_test_three_table.php')) ->toBeFile() - ->and(database_path('migrations/create_package_test_four_table.php')) + ->and(database_path('migrations/create_package_test_four_table.php')) ->not->toBeFile(); artisan('migrate')->assertSuccessful(); diff --git a/workbench/tests/PackageServiceProviderTest.php b/workbench/tests/PackageServiceProviderTest.php index 6976303..67570d1 100644 --- a/workbench/tests/PackageServiceProviderTest.php +++ b/workbench/tests/PackageServiceProviderTest.php @@ -4,9 +4,7 @@ use Asciito\LaravelPackage\Package\Package; use Asciito\LaravelPackage\Tests\TestCase; -use Illuminate\Support\Carbon; use Illuminate\Support\Facades\File; -use Spatie\TestTime\TestTime; use Symfony\Component\Finder\SplFileInfo; use Workbench\App\PackageServiceProvider; From 18e784e9f4cf5efe9a166086b0baef1f800cfbea Mon Sep 17 00:00:00 2001 From: asciito Date: Mon, 2 Oct 2023 19:21:07 -0600 Subject: [PATCH 7/8] chore: refactor migration and command component Reorder folder structure Improve publish and register migration file Signed-off-by: asciito --- src/Package/Concerns/HasCommand.php | 83 ++++++++++++++++++ src/Package/Concerns/HasCommands.php | 86 ------------------- .../{HasMigrations.php => HasMigration.php} | 2 +- src/Package/Contracts/WithCommand.php | 54 ++++++++++++ src/Package/Contracts/WithCommands.php | 45 ---------- .../{WithMigrations.php => WithMigration.php} | 2 +- src/Package/Package.php | 12 +-- src/Package/PackageServiceProvider.php | 4 +- .../Command/PackageregisterCommandTest.php | 51 +++++++++++ .../Command/PackageuUnregisterCommandTest.php | 58 +++++++++++++ .../Console/Commands/PackageCommandOne.php | 32 +++++++ .../Console/Commands/PackageCommandTwo.php | 32 +++++++ .../Commands/extra/PackageCommandFour.php | 32 +++++++ .../Commands/extra/PackageCommandThree.php | 32 +++++++ 14 files changed, 384 insertions(+), 141 deletions(-) create mode 100644 src/Package/Concerns/HasCommand.php delete mode 100644 src/Package/Concerns/HasCommands.php rename src/Package/Concerns/{HasMigrations.php => HasMigration.php} (99%) create mode 100644 src/Package/Contracts/WithCommand.php delete mode 100644 src/Package/Contracts/WithCommands.php rename src/Package/Contracts/{WithMigrations.php => WithMigration.php} (98%) create mode 100644 tests/Feature/Command/PackageregisterCommandTest.php create mode 100644 tests/Feature/Command/PackageuUnregisterCommandTest.php create mode 100644 workbench/app/Console/Commands/PackageCommandOne.php create mode 100644 workbench/app/Console/Commands/PackageCommandTwo.php create mode 100644 workbench/app/Console/Commands/extra/PackageCommandFour.php create mode 100644 workbench/app/Console/Commands/extra/PackageCommandThree.php diff --git a/src/Package/Concerns/HasCommand.php b/src/Package/Concerns/HasCommand.php new file mode 100644 index 0000000..b0c706f --- /dev/null +++ b/src/Package/Concerns/HasCommand.php @@ -0,0 +1,83 @@ +shouldLoadDefaultCommandFolder() + || $this->getRegister('command')->isNotEmpty(); + } + + public function withCommand(string|array $command = []): static + { + $this->ensureRegistersInitialize('command'); + + if (filled($command)) { + $this->register( + 'command', + $command, + ); + } + + $this->shouldIncludeCommandFromFolder = true; + + return $this; + } + + public function excludeCommand(string|array $path): static + { + $this->exclude('command', $path); + + return $this; + } + + public function getRegisteredCommand(): Collection + { + $files = $this->getDefaultCommandFiles(); + + return $files + ->merge($this->getRegister('command')) + ->filter(fn (string $path) => ! in_array($path, $this->getExclude('command')->all())); + } + + public function getDefaultCommandFiles(): Collection + { + if (! $this->shouldLoadDefaultCommandFolder()) { + return collect(); + } + + return $this->getFilesFrom($this->getBasePath('console/commands')) + ->map(fn (string $path): string => Str::of($path) + ->basename('.php') + ->prepend( + '\\', + $this->getNamespace(), + '\\Console\\Commands\\', + ) + , + ); + + } + + public function preventDefaultCommand(): static + { + $this->preventLoadDefaultCommandFolder = true; + + return $this; + } + + private function shouldLoadDefaultCommandFolder(): bool + { + return ! $this->preventLoadDefaultCommandFolder + && $this->shouldIncludeCommandFromFolder; + } +} diff --git a/src/Package/Concerns/HasCommands.php b/src/Package/Concerns/HasCommands.php deleted file mode 100644 index b01a4e6..0000000 --- a/src/Package/Concerns/HasCommands.php +++ /dev/null @@ -1,86 +0,0 @@ -shouldLoadDefaultCommandsFolder() || filled($this->commands); - } - - private function shouldLoadDefaultCommandsFolder(): bool - { - return ! $this->preventLoadDefaultCommandsFolder && $this->shouldLoadCommandsFromDefaultFolder; - } - - public function preventDefaultCommands(): static - { - $this->preventLoadDefaultCommandsFolder = true; - - return $this; - } - - public function withCommands(string|array ...$command): static - { - if (filled($command)) { - $this->commands = collect($command) - ->flatten() - ->merge($this->commands) - ->all(); - } - - $this->shouldLoadCommandsFromDefaultFolder = true; - - return $this; - } - - public function getRegisteredCommands(): Collection - { - $commands = []; - - if ($this->shouldLoadDefaultCommandsFolder()) { - $commands = $this->loadCommandsFromDefaultFolder(); - } - - return collect($this->commands) - ->merge($commands) - ->filter(fn (string $command) => ! in_array($command, $this->excludedCommands)); - } - - public function loadCommandsFromDefaultFolder() - { - return collect(File::files($this->getBasePath('Console/Commands'))) - ->filter(fn (SplFileInfo $file) => $file->getExtension() === 'php') - ->map(fn (SplFileInfo $file) => str($file) - ->after('Console/') - ->replace('/', '\\') - ->remove('.php') - ->prepend($this->getNamespace().'\\Console\\') - ->toString() - ) - ->all(); - } - - /** - * Un-register a previously registered command - */ - public function unregisterCommand(string $fqcn): static - { - $this->excludedCommands[] = $fqcn; - - return $this; - } -} diff --git a/src/Package/Concerns/HasMigrations.php b/src/Package/Concerns/HasMigration.php similarity index 99% rename from src/Package/Concerns/HasMigrations.php rename to src/Package/Concerns/HasMigration.php index e5b4eee..b04df4c 100644 --- a/src/Package/Concerns/HasMigrations.php +++ b/src/Package/Concerns/HasMigration.php @@ -5,7 +5,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Collection; -trait HasMigrations +trait HasMigration { protected string $migrationPath; diff --git a/src/Package/Contracts/WithCommand.php b/src/Package/Contracts/WithCommand.php new file mode 100644 index 0000000..316a8de --- /dev/null +++ b/src/Package/Contracts/WithCommand.php @@ -0,0 +1,54 @@ +hasCommands()) { - $this->commands($package->getRegisteredCommands()->all()); + if ($package->hasCommand()) { + $this->commands($package->getRegisteredCommand()->all()); } } } diff --git a/tests/Feature/Command/PackageregisterCommandTest.php b/tests/Feature/Command/PackageregisterCommandTest.php new file mode 100644 index 0000000..712167c --- /dev/null +++ b/tests/Feature/Command/PackageregisterCommandTest.php @@ -0,0 +1,51 @@ +setName('package') + ->withCommand([ + \Workbench\App\Console\Commands\Extra\PackageCommandThree::class, + \Workbench\App\Console\Commands\Extra\PackageCommandFour::class, + ]); + } +} + +uses(PackageRegisterCommandTest::class); + +test('package has registered files from folder', function () { + expect($this->package) + ->getRegisteredCommand() + ->toHaveCount(4); + + artisan('list') + ->expectsOutputToContain('package:test-one') + ->expectsOutputToContain('package:test-two') + ->expectsOutputToContain('package:test-three') + ->expectsOutputToContain('package:test-four') + ->assertSuccessful(); +}); + +it('run registered commands', function () { + artisan('package:test-one') + ->expectsOutput('Package test one') + ->assertSuccessful(); + + artisan('package:test-two') + ->expectsOutput('Package test two') + ->assertSuccessful(); + + artisan('package:test-three') + ->expectsOutput('Package test three') + ->assertSuccessful(); + + artisan('package:test-four') + ->expectsOutput('Package test four') + ->assertSuccessful(); +}); diff --git a/tests/Feature/Command/PackageuUnregisterCommandTest.php b/tests/Feature/Command/PackageuUnregisterCommandTest.php new file mode 100644 index 0000000..2e61e63 --- /dev/null +++ b/tests/Feature/Command/PackageuUnregisterCommandTest.php @@ -0,0 +1,58 @@ +setName('unregister-package') + ->withCommand(\Workbench\App\Console\Commands\PackageCommandOne::class) + ->withCommand([ + \Workbench\App\Console\Commands\Extra\PackageCommandThree::class, + \Workbench\App\Console\Commands\Extra\PackageCommandFour::class, + ]) + ->excludeCommand(\Workbench\App\Console\Commands\Extra\PackageCommandThree::class) + ->preventDefaultCommand(); + } +} + +uses(PackageUnregisterCommandTest::class); + +test('package has registered files from folder', function () { + expect($this->package) + ->getRegisteredCommand() + ->toHaveCount(2); + + artisan('list') + ->expectsOutputToContain('package:test-one') + ->doesntExpectOutputToContain('package:test-two') + ->doesntExpectOutputToContain('package:test-three') + ->expectsOutputToContain('package:test-four') + ->assertSuccessful(); +}); + +it('run registered commands', function () { + artisan('package:test-one') + ->expectsOutput('Package test one') + ->assertSuccessful(); + + expect(function () { + artisan('package:test-two') + ->assertFailed(); + })->toThrow(CommandNotFoundException::class) + ->and(function () { + artisan('package:test-three') + ->assertFailed(); + })->toThrow(CommandNotFoundException::class); + + artisan('package:test-four') + ->expectsOutput('Package test four') + ->assertSuccessful(); +}); diff --git a/workbench/app/Console/Commands/PackageCommandOne.php b/workbench/app/Console/Commands/PackageCommandOne.php new file mode 100644 index 0000000..e901e48 --- /dev/null +++ b/workbench/app/Console/Commands/PackageCommandOne.php @@ -0,0 +1,32 @@ +info('Package test one'); + + return self::SUCCESS; + } +} diff --git a/workbench/app/Console/Commands/PackageCommandTwo.php b/workbench/app/Console/Commands/PackageCommandTwo.php new file mode 100644 index 0000000..4872aca --- /dev/null +++ b/workbench/app/Console/Commands/PackageCommandTwo.php @@ -0,0 +1,32 @@ +info('Package test two'); + + return self::SUCCESS; + } +} diff --git a/workbench/app/Console/Commands/extra/PackageCommandFour.php b/workbench/app/Console/Commands/extra/PackageCommandFour.php new file mode 100644 index 0000000..95c29d7 --- /dev/null +++ b/workbench/app/Console/Commands/extra/PackageCommandFour.php @@ -0,0 +1,32 @@ +info('Package test four'); + + return self::SUCCESS; + } +} diff --git a/workbench/app/Console/Commands/extra/PackageCommandThree.php b/workbench/app/Console/Commands/extra/PackageCommandThree.php new file mode 100644 index 0000000..9d9f37d --- /dev/null +++ b/workbench/app/Console/Commands/extra/PackageCommandThree.php @@ -0,0 +1,32 @@ +info('Package test three'); + + return self::SUCCESS; + } +} From 19fcc410835f8d6ed44e6cd59faaf0019e3a0549 Mon Sep 17 00:00:00 2001 From: asciito Date: Tue, 3 Oct 2023 01:21:35 +0000 Subject: [PATCH 8/8] Fix styling --- src/Package/Concerns/HasCommand.php | 13 ++++++------- .../Command/PackageuUnregisterCommandTest.php | 10 +++++----- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Package/Concerns/HasCommand.php b/src/Package/Concerns/HasCommand.php index b0c706f..e4b6b45 100644 --- a/src/Package/Concerns/HasCommand.php +++ b/src/Package/Concerns/HasCommand.php @@ -57,13 +57,12 @@ public function getDefaultCommandFiles(): Collection return $this->getFilesFrom($this->getBasePath('console/commands')) ->map(fn (string $path): string => Str::of($path) - ->basename('.php') - ->prepend( - '\\', - $this->getNamespace(), - '\\Console\\Commands\\', - ) - , + ->basename('.php') + ->prepend( + '\\', + $this->getNamespace(), + '\\Console\\Commands\\', + ), ); } diff --git a/tests/Feature/Command/PackageuUnregisterCommandTest.php b/tests/Feature/Command/PackageuUnregisterCommandTest.php index 2e61e63..b05fc3b 100644 --- a/tests/Feature/Command/PackageuUnregisterCommandTest.php +++ b/tests/Feature/Command/PackageuUnregisterCommandTest.php @@ -3,8 +3,8 @@ namespace Asciito\LaravelPackage\Tests\Feature\Command; use Asciito\LaravelPackage\Package\Package; - use Symfony\Component\Console\Exception\CommandNotFoundException; + use function Pest\Laravel\artisan; trait PackageUnregisterCommandTest @@ -47,10 +47,10 @@ protected function configurePackage(Package $package): void artisan('package:test-two') ->assertFailed(); })->toThrow(CommandNotFoundException::class) - ->and(function () { - artisan('package:test-three') - ->assertFailed(); - })->toThrow(CommandNotFoundException::class); + ->and(function () { + artisan('package:test-three') + ->assertFailed(); + })->toThrow(CommandNotFoundException::class); artisan('package:test-four') ->expectsOutput('Package test four')