Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing config modifications using an attribute #69

Merged
merged 3 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading