Skip to content

Commit

Permalink
Merge pull request #69 from spiral/bugfix/config-attribute
Browse files Browse the repository at this point in the history
Fixing config modifications using an attribute
  • Loading branch information
butschster committed Dec 28, 2023
2 parents 9364cbc + 500f700 commit 03808a8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 15 additions & 1 deletion src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand All @@ -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());
Expand Down
3 changes: 3 additions & 0 deletions src/Traits/InteractsWithConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions tests/app/Bootloader/BlogBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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([
Expand Down
4 changes: 2 additions & 2 deletions tests/src/Attribute/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
2 changes: 1 addition & 1 deletion tests/src/Scaffolder/ScaffolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function testCommandNameIsRequired(): void

$this->assertScaffolderCommandSame(
'create:command',
[],
['-n' => false],
'',
);
}
Expand Down

0 comments on commit 03808a8

Please sign in to comment.