diff --git a/composer.json b/composer.json index c791f67..2bf858a 100644 --- a/composer.json +++ b/composer.json @@ -66,6 +66,7 @@ "ext-gd": "Required to use generate fake image files" }, "require-dev": { + "spiral/framework": "^3.11", "spiral/roadrunner-bridge": "^2.2 || ^3.0", "spiral-packages/league-event": "^1.0.1", "spiral/nyholm-bridge": "^1.2", diff --git a/src/TestCase.php b/src/TestCase.php index 314a407..691ec95 100644 --- a/src/TestCase.php +++ b/src/TestCase.php @@ -10,6 +10,8 @@ use Spiral\Boot\AbstractKernel; use Spiral\Boot\Environment; use Spiral\Boot\EnvironmentInterface; +use Spiral\Config\Patch\Set; +use Spiral\Core\ConfigsInterface; use Spiral\Core\Container; use Spiral\Core\ContainerScope; @@ -137,6 +139,19 @@ public function makeApp(array $env = []): AbstractKernel $app->getContainer()->invoke($callback); } + $configs = $this->getTestAttributes(Attribute\Config::class); + $app->booting(static function (ConfigsInterface $configManager) use ($configs) { + foreach ($configs as $attribute) { + \assert($attribute instanceof Attribute\Config); + [$config, $key] = explode('.', $attribute->path, 2); + + $configManager->modify( + $config, + new Set($key, $attribute->closure?->__invoke() ?? $attribute->value) + ); + } + }); + $app->booting(...$this->beforeBooting); $app->run($environment); @@ -147,7 +162,6 @@ public function initApp(array $env = []): void { $this->app = $this->makeApp($env); $this->suppressExceptionHandlingIfAttributeDefined(); - $this->updateConfigFromAttribute(); (new \ReflectionClass(ContainerScope::class)) ->setStaticPropertyValue('container', $this->app->getContainer()); diff --git a/src/Traits/InteractsWithConfig.php b/src/Traits/InteractsWithConfig.php index 773cbc1..1bd874c 100644 --- a/src/Traits/InteractsWithConfig.php +++ b/src/Traits/InteractsWithConfig.php @@ -54,6 +54,9 @@ public function updateConfig(string $key, mixed $data): void $this->getConfigs()->modify($config, new Set($key, $data)); } + /** + * @deprecated since v2.6.4 + */ private function updateConfigFromAttribute(): void { foreach ($this->getTestAttributes(Attribute\Config::class) as $attribute) { diff --git a/tests/app/Bootloader/BlogBootloader.php b/tests/app/Bootloader/BlogBootloader.php index bef9242..8f992fb 100644 --- a/tests/app/Bootloader/BlogBootloader.php +++ b/tests/app/Bootloader/BlogBootloader.php @@ -5,6 +5,7 @@ namespace Spiral\Testing\Tests\App\Bootloader; use Spiral\Boot\Bootloader\Bootloader; +use Spiral\Storage\Config\StorageConfig; use Spiral\Testing\Tests\App\Repositories\ArrayPostRepository; use Spiral\Testing\Tests\App\Repositories\PostRepositoryInterface; use Spiral\Testing\Tests\App\Services\BlogService; @@ -20,6 +21,13 @@ final class BlogBootloader extends Bootloader PostRepositoryInterface::class => [self::class, 'initPostRepository'] ]; + /** + * The configuration file should be modified by an attribute BEFORE it's used in the boot method + */ + public function boot(StorageConfig $config): void + { + } + protected function initPostRepository(): PostRepositoryInterface { return new ArrayPostRepository([ diff --git a/tests/src/Attribute/ConfigTest.php b/tests/src/Attribute/ConfigTest.php index 662fcdb..693ee06 100644 --- a/tests/src/Attribute/ConfigTest.php +++ b/tests/src/Attribute/ConfigTest.php @@ -24,11 +24,11 @@ public function testReplaceUsingAttribute(): void } #[Config('storage.default', 'replaced')] - #[Config('storage.servers.static.adapter', 'test')] + #[Config('storage.servers.static.directory', 'test')] public function testMultipleAttributes(): void { $config = $this->getConfig(StorageConfig::CONFIG); $this->assertSame('replaced', $config['default']); - $this->assertSame('test', $config['servers']['static']['adapter']); + $this->assertSame('test', $config['servers']['static']['directory']); } } diff --git a/tests/src/Scaffolder/ScaffolderTest.php b/tests/src/Scaffolder/ScaffolderTest.php index efd27a6..db65638 100644 --- a/tests/src/Scaffolder/ScaffolderTest.php +++ b/tests/src/Scaffolder/ScaffolderTest.php @@ -72,7 +72,7 @@ public function testCommandNameIsRequired(): void $this->assertScaffolderCommandSame( 'create:command', - [], + ['-n' => false], '', ); }