From 0581ca4a82a16bbcc002da3c353187e51e6e27c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ayax=20C=C3=B3rdova?= Date: Wed, 20 Sep 2023 19:59:17 -0600 Subject: [PATCH 1/2] fix: naming of protected properties on migration and config traits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ayax Córdova --- src/Package/Concerns/HasConfig.php | 2 +- src/Package/Concerns/HasMigrations.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Package/Concerns/HasConfig.php b/src/Package/Concerns/HasConfig.php index 16baad5..5f744e6 100644 --- a/src/Package/Concerns/HasConfig.php +++ b/src/Package/Concerns/HasConfig.php @@ -81,7 +81,7 @@ public function getConfigPath(string $path = ''): string private function shouldLoadDefaultConfigFolder(): bool { - return ! $this->preventLoadDefault && $this->shouldLoadDefault; + return ! $this->preventLoadDefault && $this->shouldIncludeConfigFromFolder; } private function loadConfigFilesFromFolder(): array diff --git a/src/Package/Concerns/HasMigrations.php b/src/Package/Concerns/HasMigrations.php index 6977d86..4f929a0 100644 --- a/src/Package/Concerns/HasMigrations.php +++ b/src/Package/Concerns/HasMigrations.php @@ -14,7 +14,7 @@ trait HasMigrations protected bool $preventLoadDefault = false; - protected bool $shouldLoadDefault = false; + protected bool $shouldIncludeMigrationsFromFolder = false; public function hasMigrations(): bool { @@ -30,7 +30,7 @@ public function withMigrations(string|array $migration = [], bool $publish = tru ->all(); } - $this->shouldLoadDefault = true; + $this->shouldIncludeMigrationsFromFolder = true; return $this; } @@ -81,7 +81,7 @@ public function getMigrationPath(string $path = ''): string private function shouldLoadDefaultMigrationsFolder(): bool { - return ! $this->preventLoadDefault && $this->shouldLoadDefault; + return ! $this->preventLoadDefault && $this->shouldIncludeMigrationsFromFolder; } private function loadDefaultFolder(): array From 3004315564660452287801ed2e00abc6593984cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ayax=20C=C3=B3rdova?= Date: Wed, 20 Sep 2023 20:03:08 -0600 Subject: [PATCH 2/2] fix: add missing nested service provider tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I add the missing tests to test the location of different ServiceProviders loading its own configuration. Signed-off-by: Ayax Córdova --- phpunit.xml | 2 +- .../RegisterCommandsTest.php | 34 ++++++++++++++ .../RegisterConfigTest.php | 41 +++++++++++++++++ .../RegisterMigrationsTest.php | 45 +++++++++++++++++++ .../Commands/ProjectTestCommandOne.php | 32 +++++++++++++ .../Commands/ProjectTestCommandTwo.php | 32 +++++++++++++ .../app/Nested/NestedServiceProvider.php | 23 ++++++++++ workbench/config/nested/nested-one.php | 5 +++ workbench/config/nested/nested-two.php | 5 +++ .../nested/create_nested_test_one_table.php | 31 +++++++++++++ .../nested/create_nested_test_two_table.php | 31 +++++++++++++ workbench/tests/ServiceProviderTest.php | 14 +++++- 12 files changed, 293 insertions(+), 2 deletions(-) create mode 100644 tests/Feature/NestedServiceProvider/RegisterCommandsTest.php create mode 100644 tests/Feature/NestedServiceProvider/RegisterConfigTest.php create mode 100644 tests/Feature/NestedServiceProvider/RegisterMigrationsTest.php create mode 100644 workbench/app/Nested/Console/Commands/ProjectTestCommandOne.php create mode 100644 workbench/app/Nested/Console/Commands/ProjectTestCommandTwo.php create mode 100644 workbench/app/Nested/NestedServiceProvider.php create mode 100644 workbench/config/nested/nested-one.php create mode 100644 workbench/config/nested/nested-two.php create mode 100644 workbench/database/migrations/nested/create_nested_test_one_table.php create mode 100644 workbench/database/migrations/nested/create_nested_test_two_table.php diff --git a/phpunit.xml b/phpunit.xml index 4b46a8b..76fffbc 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -24,4 +24,4 @@ - \ No newline at end of file + diff --git a/tests/Feature/NestedServiceProvider/RegisterCommandsTest.php b/tests/Feature/NestedServiceProvider/RegisterCommandsTest.php new file mode 100644 index 0000000..8c8a7f3 --- /dev/null +++ b/tests/Feature/NestedServiceProvider/RegisterCommandsTest.php @@ -0,0 +1,34 @@ +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 new file mode 100644 index 0000000..2880eeb --- /dev/null +++ b/tests/Feature/NestedServiceProvider/RegisterConfigTest.php @@ -0,0 +1,41 @@ +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(NestedServiceProviderWithConfig::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 new file mode 100644 index 0000000..6e918b5 --- /dev/null +++ b/tests/Feature/NestedServiceProvider/RegisterMigrationsTest.php @@ -0,0 +1,45 @@ +setName('nested-service') + ->setMigrationPath($package->getBasePath('../../database/migrations/nested')) + ->withMigrations(); + + expect($package) + ->getMigrationPath() + ->toBe($package->getBasePath('../../database/migrations/nested')) + ->getPublishableMigrations() + ->not->toBeEmpty() + ->each + ->toStartWith($package->getMigrationPath()) + ->toMatch('/\/\w+.php$/'); + } +} + +uses(NestedServiceProviderWithMigrations::class); + +it('publish migrations', function () { + artisan('vendor:publish', ['--tag' => 'nested-service-migrations'])->run(); + + assertFileExists(database_path('migrations/2023_01_01_000000_create_nested_test_one_table.php')); + assertFileExists(database_path('migrations/2023_01_01_000001_create_nested_test_two_table.php')); +}); + +it('run published migrations', function () { + artisan('vendor:publish', ['--tag' => 'nested-service-migrations'])->run(); + + artisan('migrate')->assertSuccessful(); + + assertdatabasecount('nested_test_one', 0); + assertdatabasecount('nested_test_one', 0); +}); diff --git a/workbench/app/Nested/Console/Commands/ProjectTestCommandOne.php b/workbench/app/Nested/Console/Commands/ProjectTestCommandOne.php new file mode 100644 index 0000000..04a5a61 --- /dev/null +++ b/workbench/app/Nested/Console/Commands/ProjectTestCommandOne.php @@ -0,0 +1,32 @@ +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 new file mode 100644 index 0000000..8a648d5 --- /dev/null +++ b/workbench/app/Nested/Console/Commands/ProjectTestCommandTwo.php @@ -0,0 +1,32 @@ +info('Project command two'); + + return self::SUCCESS; + } +} diff --git a/workbench/app/Nested/NestedServiceProvider.php b/workbench/app/Nested/NestedServiceProvider.php new file mode 100644 index 0000000..24e94f2 --- /dev/null +++ b/workbench/app/Nested/NestedServiceProvider.php @@ -0,0 +1,23 @@ + null; + + $configClosure($package); + } + + protected function getNamespace(): string + { + return __NAMESPACE__; + } +} diff --git a/workbench/config/nested/nested-one.php b/workbench/config/nested/nested-one.php new file mode 100644 index 0000000..e533c21 --- /dev/null +++ b/workbench/config/nested/nested-one.php @@ -0,0 +1,5 @@ + 'nested-one', +]; diff --git a/workbench/config/nested/nested-two.php b/workbench/config/nested/nested-two.php new file mode 100644 index 0000000..1a9b748 --- /dev/null +++ b/workbench/config/nested/nested-two.php @@ -0,0 +1,5 @@ + 'nested-two', +]; diff --git a/workbench/database/migrations/nested/create_nested_test_one_table.php b/workbench/database/migrations/nested/create_nested_test_one_table.php new file mode 100644 index 0000000..40735c5 --- /dev/null +++ b/workbench/database/migrations/nested/create_nested_test_one_table.php @@ -0,0 +1,31 @@ +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 new file mode 100644 index 0000000..7e0b4db --- /dev/null +++ b/workbench/database/migrations/nested/create_nested_test_two_table.php @@ -0,0 +1,31 @@ +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/ServiceProviderTest.php index ae175e1..adaf48f 100644 --- a/workbench/tests/ServiceProviderTest.php +++ b/workbench/tests/ServiceProviderTest.php @@ -5,6 +5,7 @@ use Asciito\LaravelPackage\Package\Package; use Asciito\LaravelPackage\Tests\TestCase; use Illuminate\Support\Facades\File; +use Workbench\App\Nested\NestedServiceProvider; use Workbench\App\ServiceProvider; use function Spatie\PestPluginTestTime\testTime; @@ -21,6 +22,8 @@ protected function setUp(): void { ServiceProvider::$configurePackageUsing = fn (Package $package) => $this->configurePackage($package); + NestedServiceProvider::$configureNestedUsing = fn (Package $package) => $this->configureNestedService($package); + testTime()->freeze('2023-01-01 00:00:00'); parent::setUp(); @@ -28,11 +31,20 @@ protected function setUp(): void $this->deletePublishable(); } - abstract protected function configurePackage(Package $package); + protected function configurePackage(Package $package): void + { + // + } + + protected function configureNestedService(Package $package): void + { + // + } protected function getPackageProviders($app): array { return [ + NestedServiceProvider::class, ServiceProvider::class, ]; }