diff --git a/CHANGELOG.md b/CHANGELOG.md index d2b7d61..2d2a0b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - New #155: Add ability to specify recursion depth for recursive modifier (@vjik) - Enh #157: Remove unnecessary code in `PackagesListBuilder` (@vjik) +- Bug #153: Do not throw "Duplicate key…" exception when using nested groups (@vjik) ## 1.4.0 November 17, 2023 diff --git a/src/Composer/MergePlanCollector.php b/src/Composer/MergePlanCollector.php new file mode 100644 index 0000000..50f3422 --- /dev/null +++ b/src/Composer/MergePlanCollector.php @@ -0,0 +1,180 @@ + 1, + Options::ROOT_PACKAGE_NAME => 2, + ]; + + /** + * @psalm-var array>> + */ + private array $mergePlan = []; + + /** + * @psalm-var array + */ + private array $processedGroups = []; + + /** + * Adds an item to the merge plan. + * + * @param array|string $file The config file. + * @param string $package The package name. + * @param string $group The group name. + * + * @psalm-param array{0:string,1:string}|string $file + */ + public function add(array|string $file, string $package, string $group): void + { + $this->mergePlan[$group][$package][] = $file; + } + + /** + * Adds a multiple items to the merge plan. + * + * @param array $files The config files. + * @param string $package The package name. + * @param string $group The group name. + * + * @psalm-param list $files + */ + public function addMultiple( + array $files, + string $package, + string $group, + ): void { + $this->mergePlan[$group][$package] = $files; + } + + /** + * Add empty group if it doesn't exist. + * + * @param string $group The group name. + */ + public function addGroup(string $group): void + { + if (!isset($this->mergePlan[$group])) { + $this->mergePlan[$group] = []; + } + } + + /** + * Returns the merge plan as an array. + * + * @psalm-return MergePlanType + */ + public function asArray(): array + { + $groups = []; + foreach ($this->mergePlan as $group => $packages) { + $groups[$group] = $this->expandVariablesInPackages($packages); + } + + $environments = []; + foreach ($groups as $packages) { + foreach ($packages as $files) { + foreach ($files as $file) { + if (is_array($file)) { + $environments[$file[0]] = true; + } + } + } + } + + return [ + 'groups' => $groups, + 'environments' => array_keys($environments), + ]; + } + + /** + * @throws ErrorException + * + * @psalm-param array> $packages + * @psalm-return array> + */ + private function expandVariablesInPackages(array $packages, ?string $targetGroup = null): array + { + if ($targetGroup !== null) { + if (!isset($this->mergePlan[$targetGroup])) { + throw new ErrorException( + sprintf('The "%s" configuration group does not exist.', $targetGroup), + severity: E_USER_ERROR + ); + } + + if (isset($this->processedGroups[$targetGroup])) { + throw new ErrorException('Circular references in configuration.', severity: E_USER_ERROR); + } + $this->processedGroups[$targetGroup] = true; + $groupPackages = $this->expandVariablesInPackages($this->mergePlan[$targetGroup]); + $this->processedGroups[$targetGroup] = null; + + $variable = '$' . $targetGroup; + foreach ($groupPackages as $groupPackage => $groupItems) { + $packageItems = $packages[$groupPackage] ?? []; + $packages[$groupPackage] = in_array($variable, $packageItems, true) + ? $this->replaceVariableToFiles($packageItems, $variable, $groupItems) + : array_merge($packageItems, $groupItems); + } + foreach ($packages as $package => $items) { + $packages[$package] = array_values( + array_filter( + $items, + static fn($item) => $item !== $variable, + ) + ); + } + uksort( + $packages, + static fn(string $a, string $b) => (self::PACKAGES_ORDER[$a] ?? 0) <=> (self::PACKAGES_ORDER[$b] ?? 0), + ); + } + + foreach ($packages as $items) { + foreach ($items as $item) { + if (!is_array($item) && Options::isVariable($item)) { + return $this->expandVariablesInPackages($packages, substr($item, 1)); + } + } + } + + return $packages; + } + + /** + * @psalm-param list $items + * @psalm-param list $files + * @psalm-return list + */ + private function replaceVariableToFiles(array $items, string $variable, array $files): array + { + $result = []; + foreach ($items as $item) { + if ($item === $variable) { + $result = array_merge($result, $files); + } else { + $result[] = $item; + } + } + return $result; + } +} diff --git a/src/Composer/MergePlanProcess.php b/src/Composer/MergePlanProcess.php index a4ad495..5211f31 100644 --- a/src/Composer/MergePlanProcess.php +++ b/src/Composer/MergePlanProcess.php @@ -7,7 +7,6 @@ use Composer\Composer; use Composer\Package\PackageInterface; use Composer\Util\Filesystem; -use Yiisoft\Config\MergePlan; use Yiisoft\Config\Options; use Yiisoft\VarDumper\VarDumper; @@ -24,7 +23,7 @@ */ final class MergePlanProcess { - private MergePlan $mergePlan; + private MergePlanCollector $mergePlanCollector; private ProcessHelper $helper; /** @@ -32,7 +31,7 @@ final class MergePlanProcess */ public function __construct(Composer $composer) { - $this->mergePlan = new MergePlan(); + $this->mergePlanCollector = new MergePlanCollector(); $this->helper = new ProcessHelper($composer); if (!$this->helper->shouldBuildMergePlan()) { @@ -43,7 +42,6 @@ public function __construct(Composer $composer) $this->addPackagesConfigsToMergePlan(true); $this->addRootPackageConfigToMergePlan(); - $this->addEnvironmentsConfigsToMergePlan(); $this->updateMergePlan(); } @@ -57,7 +55,7 @@ private function addPackagesConfigsToMergePlan(bool $isVendorOverrideLayer): voi $packageName = $isVendorOverrideLayer ? Options::VENDOR_OVERRIDE_PACKAGE_NAME : $name; foreach ($this->helper->getPackageConfig($package) as $group => $files) { - $this->mergePlan->addGroup($group); + $this->mergePlanCollector->addGroup($group); foreach ((array) $files as $file) { $isOptional = false; @@ -68,7 +66,7 @@ private function addPackagesConfigsToMergePlan(bool $isVendorOverrideLayer): voi } if (Options::isVariable($file)) { - $this->mergePlan->add($file, $packageName, $group); + $this->mergePlanCollector->add($file, $packageName, $group); continue; } @@ -82,7 +80,7 @@ private function addPackagesConfigsToMergePlan(bool $isVendorOverrideLayer): voi } foreach ($matches as $match) { - $this->mergePlan->add( + $this->mergePlanCollector->add( $this->normalizePackageFilePath($package, $match, $isVendorOverrideLayer), $packageName, $group, @@ -96,7 +94,7 @@ private function addPackagesConfigsToMergePlan(bool $isVendorOverrideLayer): voi continue; } - $this->mergePlan->add( + $this->mergePlanCollector->add( $this->normalizePackageFilePath($package, $absoluteFilePath, $isVendorOverrideLayer), $packageName, $group, @@ -109,7 +107,7 @@ private function addPackagesConfigsToMergePlan(bool $isVendorOverrideLayer): voi private function addRootPackageConfigToMergePlan(): void { foreach ($this->helper->getRootPackageConfig() as $group => $files) { - $this->mergePlan->addMultiple( + $this->mergePlanCollector->addMultiple( (array) $files, Options::ROOT_PACKAGE_NAME, $group, @@ -117,32 +115,9 @@ private function addRootPackageConfigToMergePlan(): void } } - private function addEnvironmentsConfigsToMergePlan(): void - { - foreach ($this->helper->getEnvironmentConfig() as $environment => $groups) { - if ($environment === Options::DEFAULT_ENVIRONMENT) { - continue; - } - - if (empty($groups)) { - $this->mergePlan->addEnvironmentWithoutConfigs($environment); - continue; - } - - foreach ($groups as $group => $files) { - $this->mergePlan->addMultiple( - (array) $files, - Options::ROOT_PACKAGE_NAME, - $group, - $environment, - ); - } - } - } - private function updateMergePlan(): void { - $mergePlan = $this->mergePlan->toArray(); + $mergePlan = $this->mergePlanCollector->asArray(); ksort($mergePlan); $filePath = $this->helper->getPaths()->absolute( diff --git a/src/Composer/ProcessHelper.php b/src/Composer/ProcessHelper.php index 692baea..9877b11 100644 --- a/src/Composer/ProcessHelper.php +++ b/src/Composer/ProcessHelper.php @@ -190,19 +190,6 @@ public function getRootPackageConfig(): array return (array) ($this->rootPackageExtra['config-plugin'] ?? []); } - /** - * Returns the environment configuration. - * - * @return array The environment configuration. - * - * @psalm-return array> - * @psalm-suppress MixedReturnTypeCoercion - */ - public function getEnvironmentConfig(): array - { - return (array) ($this->rootPackageExtra['config-plugin-environments'] ?? []); - } - /** * Returns the config paths instance. * diff --git a/src/Config.php b/src/Config.php index d559fbd..d577af2 100644 --- a/src/Config.php +++ b/src/Config.php @@ -11,7 +11,6 @@ use function restore_error_handler; use function set_error_handler; use function sprintf; -use function substr; /** * Config takes merge plan prepared by {@see \Yiisoft\Config\Composer\EventHandler} @@ -39,17 +38,14 @@ final class Config implements ConfigInterface */ public function __construct( ConfigPaths $paths, - string $environment = null, + ?string $environment = null, array $modifiers = [], private ?string $paramsGroup = 'params', string $mergePlanFile = Options::DEFAULT_MERGE_PLAN_FILE, ) { - $environment = empty($environment) ? Options::DEFAULT_ENVIRONMENT : $environment; + $mergePlan = new MergePlan($paths->absolute($mergePlanFile)); - /** @psalm-suppress UnresolvableInclude, MixedArgument */ - $mergePlan = new MergePlan(require $paths->absolute($mergePlanFile)); - - if (!$mergePlan->hasEnvironment($environment)) { + if ($environment !== null && !$mergePlan->hasEnvironment($environment)) { $this->throwException(sprintf('The "%s" configuration environment does not exist.', $environment)); } @@ -117,46 +113,14 @@ private function buildGroup(string $group): void $this->build[$group] = []; foreach ($this->filesExtractor->extract($group) as $file => $context) { - if (Options::isVariable($file)) { - $variable = $this->prepareVariable($file, $group); - $array = $this->get($variable); - } else { - $array = $this->buildFile($file); - } - $this->build[$group] = $this->merger->merge( $context, $this->build[$group], - $array, + $this->buildFile($file), ); } } - /** - * Checks the configuration variable and returns its name. - * - * @param string $variable The variable. - * @param string $group The group name. - * - * @throws ErrorException If the variable name is not valid. - * - * @return string The variable name. - */ - private function prepareVariable(string $variable, string $group): string - { - $name = substr($variable, 1); - - if ($name === $group) { - $this->throwException(sprintf( - 'The variable "%s" must not be located inside the "%s" config group.', - "$variable", - "$name", - )); - } - - return $name; - } - /** * Builds the configuration from the file. * diff --git a/src/Context.php b/src/Context.php index 280e5d5..ee0b6b9 100644 --- a/src/Context.php +++ b/src/Context.php @@ -19,7 +19,6 @@ public function __construct( private string $package, private int $layer, private string $file, - private bool $isVariable, ) { } @@ -42,9 +41,4 @@ public function file(): string { return $this->file; } - - public function isVariable(): bool - { - return $this->isVariable; - } } diff --git a/src/DataModifiers.php b/src/DataModifiers.php index 3d36730..68630af 100644 --- a/src/DataModifiers.php +++ b/src/DataModifiers.php @@ -123,12 +123,8 @@ public function isReverseMergeGroup(string $group): bool return array_key_exists($group, $this->reverseMergeGroupsIndex); } - public function shouldRemoveGroupFromVendor(string $package, string $group, int $layer): bool + public function shouldRemoveGroupFromVendor(string $package, string $group): bool { - if ($layer !== Context::VENDOR) { - return false; - } - return array_key_exists('*~*', $this->removeFromVendorGroupsIndex) || array_key_exists('*~' . $group, $this->removeFromVendorGroupsIndex) || array_key_exists($package . '~*', $this->removeFromVendorGroupsIndex) diff --git a/src/FilesExtractor.php b/src/FilesExtractor.php index a40be43..16f00eb 100644 --- a/src/FilesExtractor.php +++ b/src/FilesExtractor.php @@ -6,7 +6,6 @@ use ErrorException; -use function array_merge; use function glob; use function is_file; use function sprintf; @@ -21,7 +20,7 @@ public function __construct( private ConfigPaths $paths, private MergePlan $mergePlan, private DataModifiers $dataModifiers, - private string $environment, + private ?string $environment, ) { } @@ -36,61 +35,35 @@ public function __construct( */ public function extract(string $group): array { - $environment = $this->prepareEnvironment($group); - - $result = $this->process(Options::DEFAULT_ENVIRONMENT, $group, $this->mergePlan->getGroup($group)); - - if ($environment !== Options::DEFAULT_ENVIRONMENT) { - $result = array_merge( - $result, - $this->process( - $environment, - $group, - $this->mergePlan->getGroup($group, $environment) - ) - ); + if (!$this->mergePlan->hasGroup($group)) { + $this->throwException(sprintf('The "%s" configuration group does not exist.', $group)); } - return $result; - } - - /** - * Checks whether the group exists in the merge plan. - * - * @param string $group The group name. - * - * @return bool Whether the group exists in the merge plan. - */ - public function hasGroup(string $group): bool - { - return $this->mergePlan->hasGroup($group, $this->environment) || ( - $this->environment !== Options::DEFAULT_ENVIRONMENT && - $this->mergePlan->hasGroup($group, Options::DEFAULT_ENVIRONMENT) - ); - } - - /** - * @psalm-param array $data - * - * @throws ErrorException If an error occurred during the process. - * - * @psalm-return array - */ - private function process(string $environment, string $group, array $data): array - { $result = []; - foreach ($data as $package => $items) { - $layer = $this->detectLayer($environment, $package); + foreach ($this->mergePlan->getGroup($group) as $package => $items) { + $defaultLayer = match ($package) { + Options::ROOT_PACKAGE_NAME => Context::APPLICATION, + Options::VENDOR_OVERRIDE_PACKAGE_NAME => Context::VENDOR_OVERRIDE, + default => Context::VENDOR, + }; - if ($this->dataModifiers->shouldRemoveGroupFromVendor($package, $group, $layer)) { + if ( + $defaultLayer === Context::VENDOR + && $this->dataModifiers->shouldRemoveGroupFromVendor($package, $group) + ) { continue; } foreach ($items as $item) { - if (Options::isVariable($item)) { - $result[$item] = new Context($group, $package, $layer, $item, true); - continue; + if (is_array($item)) { + if ($item[0] !== $this->environment) { + continue; + } + $item = $item[1]; + $layer = Context::ENVIRONMENT; + } else { + $layer = $defaultLayer; } $isOptional = Options::isOptional($item); @@ -104,9 +77,12 @@ private function process(string $environment, string $group, array $data): array foreach ($files as $file) { if (is_file($file)) { - $result[$file] = new Context($group, $package, $layer, $file, false); + $result[$file] = new Context($group, $package, $layer, $file); } elseif (!$isOptional) { - $this->throwException(sprintf('The "%s" file does not found.', $file)); + $message = Options::isVariable($item) + ? sprintf('Don\'t allow to use variables in environments. Found variable "%s".', $item) + : sprintf('The "%s" file does not found.', $file); + $this->throwException($message); } } } @@ -116,49 +92,15 @@ private function process(string $environment, string $group, array $data): array } /** - * Calculates the layer for the context. - * - * @param string $environment The environment name. - * @param string $package The package name. - * - * @return int The layer for the context. - */ - private function detectLayer(string $environment, string $package): int - { - if ($package !== Options::ROOT_PACKAGE_NAME) { - return $package === Options::VENDOR_OVERRIDE_PACKAGE_NAME ? Context::VENDOR_OVERRIDE : Context::VENDOR; - } - - if ($environment === Options::DEFAULT_ENVIRONMENT) { - return Context::APPLICATION; - } - - return Context::ENVIRONMENT; - } - - /** - * Checks the group name and returns actual environment name. + * Checks whether the group exists in the merge plan. * * @param string $group The group name. * - * @throws ErrorException If the group does not exist. - * - * @return string The actual environment name. + * @return bool Whether the group exists in the merge plan. */ - private function prepareEnvironment(string $group): string + public function hasGroup(string $group): bool { - if (!$this->mergePlan->hasGroup($group, $this->environment)) { - if ( - $this->environment === Options::DEFAULT_ENVIRONMENT || - !$this->mergePlan->hasGroup($group, Options::DEFAULT_ENVIRONMENT) - ) { - $this->throwException(sprintf('The "%s" configuration group does not exist.', $group)); - } - - return Options::DEFAULT_ENVIRONMENT; - } - - return $this->environment; + return $this->mergePlan->hasGroup($group); } /** diff --git a/src/MergePlan.php b/src/MergePlan.php index f6d7ac1..6ac8d45 100644 --- a/src/MergePlan.php +++ b/src/MergePlan.php @@ -6,108 +6,48 @@ /** * @internal + * + * @psalm-type FileType = string|array{0:string,1:string} + * @psalm-type MergePlanType = array{ + * groups: array>>, + * environments: list, + * } */ final class MergePlan { /** - * @psalm-param array>> $mergePlan + * @psalm-var MergePlanType */ - public function __construct( - private array $mergePlan = [], - ) { - } - - /** - * Adds an item to the merge plan. - * - * @param string $file The config file. - * @param string $package The package name. - * @param string $group The group name. - * @param string $environment The environment name. - */ - public function add( - string $file, - string $package, - string $group, - string $environment = Options::DEFAULT_ENVIRONMENT - ): void { - $this->mergePlan[$environment][$group][$package][] = $file; - } - - /** - * Adds a multiple items to the merge plan. - * - * @param string[] $files The config files. - * @param string $package The package name. - * @param string $group The group name. - * @param string $environment The environment name. - */ - public function addMultiple( - array $files, - string $package, - string $group, - string $environment = Options::DEFAULT_ENVIRONMENT - ): void { - $this->mergePlan[$environment][$group][$package] = $files; - } + private array $mergePlan; - /** - * Adds an empty environment item to the merge plan. - * - * @param string $environment The environment name. - */ - public function addEnvironmentWithoutConfigs(string $environment): void - { - $this->mergePlan[$environment] = []; - } - - /** - * Add empty group if it not exists. - * - * @param string $group The group name. - * @param string $environment The environment name. - */ - public function addGroup(string $group, string $environment = Options::DEFAULT_ENVIRONMENT): void + public function __construct(string $file) { - if (!isset($this->mergePlan[$environment][$group])) { - $this->mergePlan[$environment][$group] = []; - } + /** @psalm-suppress UnresolvableInclude */ + $this->mergePlan = require $file; } /** * Returns the merge plan group. * * @param string $group The group name. - * @param string $environment The environment name. - * - * @return array - */ - public function getGroup(string $group, string $environment = Options::DEFAULT_ENVIRONMENT): array - { - return $this->mergePlan[$environment][$group] ?? []; - } - - /** - * Returns the merge plan as an array. * - * @psalm-return array>> + * @return array> */ - public function toArray(): array + public function getGroup(string $group): array { - return $this->mergePlan; + return $this->mergePlan['groups'][$group] ?? []; } /** * Checks whether the group exists in the merge plan. * * @param string $group The group name. - * @param string $environment The environment name. * * @return bool Whether the group exists in the merge plan. */ - public function hasGroup(string $group, string $environment): bool + public function hasGroup(string $group): bool { - return isset($this->mergePlan[$environment][$group]); + return isset($this->mergePlan['groups'][$group]); } /** @@ -119,6 +59,6 @@ public function hasGroup(string $group, string $environment): bool */ public function hasEnvironment(string $environment): bool { - return isset($this->mergePlan[$environment]); + return in_array($environment, $this->mergePlan['environments']); } } diff --git a/src/Merger.php b/src/Merger.php index 77cad9d..2943504 100644 --- a/src/Merger.php +++ b/src/Merger.php @@ -135,6 +135,14 @@ private function performMerge( $existKey = array_key_exists($k, $result); + if ( + $existKey + && $context->layer() !== Context::ENVIRONMENT + && isset($this->cacheKeys[Context::ENVIRONMENT][$k]) + ) { + continue; + } + if ($existKey && !$isReverseMerge) { /** @var string|null $file */ $file = ArrayHelper::getValue( @@ -150,7 +158,7 @@ private function performMerge( if (!$isReverseMerge || !$existKey) { $isSet = $this->setValue($context, $fullKeyPath, $result, $k, $v); - if ($isSet && !$isReverseMerge && !$context->isVariable()) { + if ($isSet && !$isReverseMerge) { /** @psalm-suppress MixedPropertyTypeCoercion */ ArrayHelper::setValue( $this->cacheKeys, @@ -197,11 +205,6 @@ private function prepareArrayForReverse( continue; } - if ($context->isVariable()) { - $result[$key] = $value; - continue; - } - $recursiveKeyPath[] = $key; /** @var string|null $file */ diff --git a/src/Options.php b/src/Options.php index 9c5bd88..81a54f7 100644 --- a/src/Options.php +++ b/src/Options.php @@ -66,6 +66,9 @@ public static function isOptional(string $file): bool return str_starts_with($file, '?'); } + /** + * @psalm-param string $file + */ public static function isVariable(string $file): bool { return str_starts_with($file, '$'); diff --git a/tests/Integration/BaseWithEnvironment/BaseWithEnvironmentTest.php b/tests/Integration/BaseWithEnvironment/BaseWithEnvironmentTest.php index 29aef24..d520cea 100644 --- a/tests/Integration/BaseWithEnvironment/BaseWithEnvironmentTest.php +++ b/tests/Integration/BaseWithEnvironment/BaseWithEnvironmentTest.php @@ -17,43 +17,12 @@ public function testBase(): void ], extra: [ 'config-plugin' => [ - 'params' => 'params.php', - 'web' => [], - ], - 'config-plugin-environments' => [ - 'dev' => [ - 'params' => 'params-dev.php', + 'params' => [ + 'params.php', + ['dev', 'params-dev.php'], ], - ], - ], - environment: 'dev', - ); - - $this->assertSame( - [ - 'a' => 1, - 'b' => 99, - 'c' => 3, - ], - $config->get('params') - ); - } - - public function testEmptyEnvironment(): void - { - $config = $this->runComposerUpdateAndCreateConfig( - rootPath: __DIR__, - packages: [ - 'test/a' => __DIR__ . '/packages/a', - ], - extra: [ - 'config-plugin' => [ - 'params' => 'params.php', 'web' => [], ], - 'config-plugin-environments' => [ - 'dev' => [], - ], ], environment: 'dev', ); @@ -61,7 +30,7 @@ public function testEmptyEnvironment(): void $this->assertSame( [ 'a' => 1, - 'b' => 2, + 'b' => 99, 'c' => 3, ], $config->get('params') diff --git a/tests/Integration/Complex/ComplexTest.php b/tests/Integration/Complex/ComplexTest.php index f403b55..d02a28e 100644 --- a/tests/Integration/Complex/ComplexTest.php +++ b/tests/Integration/Complex/ComplexTest.php @@ -55,10 +55,10 @@ public function testBase(): void 'ba-web' => true, 'c-web' => true, 'custom-source-web' => true, - 'd-dev-c-web' => true, - 'a-web' => true, 'custom-source-common-a' => true, 'custom-source-common-b' => true, + 'd-dev-c-web' => true, + 'a-web' => true, 'common' => 99, 'web' => 7, ], diff --git a/tests/Integration/Dummy/DummyTest.php b/tests/Integration/Dummy/DummyTest.php index 968060f..c29068a 100644 --- a/tests/Integration/Dummy/DummyTest.php +++ b/tests/Integration/Dummy/DummyTest.php @@ -20,15 +20,6 @@ public function testHas(): void $this->assertFalse($config->has('not-exist')); } - public function testHasWithEmptyEnvironment(): void - { - $config = $this->prepareConfig('empty'); - - $this->assertTrue($config->has('web')); - $this->assertTrue($config->has('empty')); - $this->assertFalse($config->has('not-exist')); - } - public function testHasWithEnvironment(): void { $config = $this->prepareConfig('alfa'); @@ -83,76 +74,6 @@ public function testGet(): void 'b-web-environment-override-key' => 'c-web-override-value', 'c-web-key' => 'c-web-value', 'c-web-environment-override-key' => 'c-web-override-value', - 'a-common-key' => 'a-common-value', - 'a-common-root-override-key' => 'common-root-override-value', - 'b-common-key' => 'b-common-value', - 'b-common-root-override-key' => 'common-root-override-value', - 'c-common-key' => 'c-common-value', - 'c-common-root-override-key' => 'common-root-override-value', - 'root-common-key-1' => 'root-common-value-1', - 'root-common-key-2' => 'root-common-value-2', - 'root-common-nested-key-1' => 'root-common-nested-value-1', - 'root-common-nested-key-2' => 'root-common-nested-value-2', - 'root-web-key' => 'root-web-value', - ], - $config->get('web') - ); - } - - public function testGetWithEmptyEnvironment(): void - { - $config = $this->prepareConfig('empty'); - - $this->assertSame([], $config->get('empty')); - $this->assertSame([], $config->get('emptyVariable')); - - $this->assertSame( - [ - 'a-common-key' => 'a-common-value', - 'a-common-root-override-key' => 'common-root-override-value', - 'b-common-key' => 'b-common-value', - 'b-common-root-override-key' => 'common-root-override-value', - 'c-common-key' => 'c-common-value', - 'c-common-root-override-key' => 'common-root-override-value', - 'root-common-key-1' => 'root-common-value-1', - 'root-common-key-2' => 'root-common-value-2', - 'root-common-nested-key-1' => 'root-common-nested-value-1', - 'root-common-nested-key-2' => 'root-common-nested-value-2', - ], - $config->get('common') - ); - - $this->assertSame( - [ - 'a-params-key' => 'a-params-value', - 'a-params-over-vendor-override-key' => 'c-params-over-vendor-override-value', - 'b-params-key' => 'b-params-value', - 'b-params-over-vendor-override-key' => 'c-params-over-vendor-override-value', - 'c-params-key' => 'c-params-value', - 'root-params-key' => 'root-params-value', - 'root-params-local-key' => 'root-params-local-value', - ], - $config->get('params') - ); - - $this->assertSame( - [ - 'a-web-key' => 'a-web-value', - 'a-web-environment-override-key' => 'a-web-override-value', - 'b-web-key' => 'b-web-value', - 'b-web-environment-override-key' => 'c-web-override-value', - 'c-web-key' => 'c-web-value', - 'c-web-environment-override-key' => 'c-web-override-value', - 'a-common-key' => 'a-common-value', - 'a-common-root-override-key' => 'common-root-override-value', - 'b-common-key' => 'b-common-value', - 'b-common-root-override-key' => 'common-root-override-value', - 'c-common-key' => 'c-common-value', - 'c-common-root-override-key' => 'common-root-override-value', - 'root-common-key-1' => 'root-common-value-1', - 'root-common-key-2' => 'root-common-value-2', - 'root-common-nested-key-1' => 'root-common-nested-value-1', - 'root-common-nested-key-2' => 'root-common-nested-value-2', 'root-web-key' => 'root-web-value', ], $config->get('web') @@ -184,26 +105,10 @@ public function testGetWithEnvironment(): void $this->assertSame( [ - 'a-web-key' => 'a-web-value', + 'alfa-main-key' => 'alfa-main-value', 'a-web-environment-override-key' => 'alfa-web-override-value', - 'b-web-key' => 'b-web-value', 'b-web-environment-override-key' => 'alfa-web-override-value', - 'c-web-key' => 'c-web-value', 'c-web-environment-override-key' => 'alfa-web-override-value', - 'a-common-key' => 'a-common-value', - 'a-common-root-override-key' => 'common-root-override-value', - 'b-common-key' => 'b-common-value', - 'b-common-root-override-key' => 'common-root-override-value', - 'c-common-key' => 'c-common-value', - 'c-common-root-override-key' => 'common-root-override-value', - 'root-common-key-1' => 'root-common-value-1', - 'root-common-key-2' => 'root-common-value-2', - 'root-common-nested-key-1' => 'root-common-nested-value-1', - 'root-common-nested-key-2' => 'root-common-nested-value-2', - 'root-web-key' => 'root-web-value', - 'alfa-web-key' => 'alfa-web-value', - 'alfa-web2-key' => 'alfa-web2-value', - 'alfa-main-key' => 'alfa-main-value', ], $config->get('main') ); @@ -227,19 +132,10 @@ public function testGetWithEnvironment(): void 'a-web-key' => 'a-web-value', 'a-web-environment-override-key' => 'a-web-override-value', 'b-web-key' => 'b-web-value', - 'b-web-environment-override-key' => 'c-web-override-value', + 'b-web-environment-override-key' => 'b-web-override-value', 'c-web-key' => 'c-web-value', + 'b-web-environment-override-key' => 'c-web-override-value', 'c-web-environment-override-key' => 'c-web-override-value', - 'a-common-key' => 'a-common-value', - 'a-common-root-override-key' => 'common-root-override-value', - 'b-common-key' => 'b-common-value', - 'b-common-root-override-key' => 'common-root-override-value', - 'c-common-key' => 'c-common-value', - 'c-common-root-override-key' => 'common-root-override-value', - 'root-common-key-1' => 'root-common-value-1', - 'root-common-key-2' => 'root-common-value-2', - 'root-common-nested-key-1' => 'root-common-nested-value-1', - 'root-common-nested-key-2' => 'root-common-nested-value-2', 'root-web-key' => 'root-web-value', 'alfa-web-key' => 'alfa-web-value', 'alfa-web2-key' => 'alfa-web2-value', @@ -279,16 +175,6 @@ public function testGetWithScopeExistenceCheck(): void 'b-web-environment-override-key' => 'beta-web-override-value', 'c-web-key' => 'c-web-value', 'c-web-environment-override-key' => 'beta-web-override-value', - 'a-common-key' => 'a-common-value', - 'a-common-root-override-key' => 'common-root-override-value', - 'b-common-key' => 'b-common-value', - 'b-common-root-override-key' => 'common-root-override-value', - 'c-common-key' => 'c-common-value', - 'c-common-root-override-key' => 'common-root-override-value', - 'root-common-key-1' => 'root-common-value-1', - 'root-common-key-2' => 'root-common-value-2', - 'root-common-nested-key-1' => 'root-common-nested-value-1', - 'root-common-nested-key-2' => 'root-common-nested-value-2', 'root-web-key' => 'root-web-value', 'beta-web-key' => 'beta-web-value', 'beta-web-isset-config' => true, @@ -308,16 +194,6 @@ public function testGetWithEnvironmentVariableExistAndRootVariableNotExist(): vo $this->assertSame( [ 'root-events-key' => 'root-events-value', - 'a-common-key' => 'a-common-value', - 'a-common-root-override-key' => 'common-root-override-value', - 'b-common-key' => 'b-common-value', - 'b-common-root-override-key' => 'common-root-override-value', - 'c-common-key' => 'c-common-value', - 'c-common-root-override-key' => 'common-root-override-value', - 'root-common-key-1' => 'root-common-value-1', - 'root-common-key-2' => 'root-common-value-2', - 'root-common-nested-key-1' => 'root-common-nested-value-1', - 'root-common-nested-key-2' => 'root-common-nested-value-2', 'beta-events-key' => 'beta-events-value', ], $config->get('events') @@ -369,16 +245,6 @@ public function testConfigWithReverseMerge(): void 'b-web-environment-override-key' => 'c-web-override-value', 'c-web-key' => 'c-web-value', 'c-web-environment-override-key' => 'c-web-override-value', - 'root-common-nested-key-2' => 'root-common-nested-value-2', - 'root-common-nested-key-1' => 'root-common-nested-value-1', - 'a-common-root-override-key' => 'common-root-override-value', - 'b-common-root-override-key' => 'common-root-override-value', - 'c-common-root-override-key' => 'common-root-override-value', - 'root-common-key-2' => 'root-common-value-2', - 'root-common-key-1' => 'root-common-value-1', - 'c-common-key' => 'c-common-value', - 'b-common-key' => 'b-common-value', - 'a-common-key' => 'a-common-value', 'root-web-key' => 'root-web-value', ], $config->get('web') @@ -389,7 +255,7 @@ public function testGetThrowExceptionForEnvironmentNotExist(): void { $this->expectException(ErrorException::class); $this->expectExceptionMessage('The "not-exist" configuration environment does not exist.'); - $this->prepareConfig('not-exist', echoOutputOnException: false); + $this->prepareConfig('not-exist'); } public function testGetThrowExceptionForGroupNotExist(): void @@ -401,59 +267,10 @@ public function testGetThrowExceptionForGroupNotExist(): void $config->get('not-exist'); } - public function testGetEnvironmentThrowExceptionForGroupNotExist(): void - { - $config = $this->prepareConfig('alfa'); - - $this->expectException(ErrorException::class); - $this->expectExceptionMessage('The "not-exist" configuration group does not exist.'); - $config->get('not-exist'); - } - - public function testGetThrowExceptionForVariableGroupEqual(): void - { - $config = $this->prepareConfig(); - - $this->expectException(ErrorException::class); - $this->expectExceptionMessage( - 'The variable "$failVariableGroupEqual" must not be located inside the "failVariableGroupEqual" config group.' - ); - $config->get('failVariableGroupEqual'); - } - - public function testGetEnvironmentThrowExceptionForVariableGroupEqual(): void - { - $config = $this->prepareConfig('alfa'); - - $this->expectException(ErrorException::class); - $this->expectExceptionMessage( - 'The variable "$failVariableGroupEqual" must not be located inside the "failVariableGroupEqual" config group.' - ); - $config->get('failVariableGroupEqual'); - } - - public function testGetThrowExceptionForVariableGroupNotExist(): void - { - $config = $this->prepareConfig(); - - $this->expectException(ErrorException::class); - $this->expectExceptionMessage('The "failVariableNotExist" configuration group does not exist.'); - $config->get('failVariableNotExist'); - } - - public function testGetEnvironmentThrowExceptionForVariableGroupNotExist(): void - { - $config = $this->prepareConfig('alfa'); - - $this->expectException(ErrorException::class); - $this->expectExceptionMessage('The "failVariableNotExist" configuration group does not exist.'); - $config->get('failVariableNotExist'); - } - private function prepareConfig( ?string $environment = null, array $modifiers = [], - bool $echoOutputOnException = true, + bool $echoOutputOnException = false, ): Config { return $this->runComposerUpdateAndCreateConfig( rootPath: __DIR__, @@ -470,9 +287,10 @@ private function prepareConfig( 'config-plugin' => [ 'empty' => [], 'emptyVariable' => '$empty', - 'events' => 'events.php', - 'failVariableGroupEqual' => '$failVariableGroupEqual', - 'failVariableGroupNotExist' => '$failVariableNotExist', + 'events' => [ + 'events.php', + ['beta', 'beta/events.php'], + ], 'common' => [ 'common/*.php', 'common/*/*.php', @@ -480,35 +298,18 @@ private function prepareConfig( 'params' => [ 'params.php', '?params-local.php', + ['alfa', 'alfa/params.php'], + ['beta', 'beta/params.php'], ], 'web' => [ - '$common', 'web.php', + ['alfa', 'alfa/web.php'], + ['alfa', 'alfa/web2.php'], + ['beta', 'beta/web.php'], ], - ], - 'config-plugin-environments' => [ - 'alfa' => [ - 'failVariableGroupEqual' => '$failVariableGroupEqual', - 'failVariableGroupNotExist' => '$failVariableNotExist', - 'main' => [ - '$web', - 'alfa/main.php', - ], - 'params' => 'alfa/params.php', - 'web' => [ - 'alfa/web.php', - 'alfa/web2.php', - ], + 'main' => [ + ['alfa', 'alfa/main.php'], ], - 'beta' => [ - 'events' => [ - '$common', - 'beta/events.php', - ], - 'web' => 'beta/web.php', - 'params' => 'beta/params.php', - ], - 'empty' => [], ], ], configDirectory: 'config', diff --git a/tests/Integration/DuplicateEnvironmentKeys/DuplicateEnvironmentKeysTest.php b/tests/Integration/DuplicateEnvironmentKeys/DuplicateEnvironmentKeysTest.php index e137d68..4b05a08 100644 --- a/tests/Integration/DuplicateEnvironmentKeys/DuplicateEnvironmentKeysTest.php +++ b/tests/Integration/DuplicateEnvironmentKeys/DuplicateEnvironmentKeysTest.php @@ -23,11 +23,9 @@ public function testBase(): void 'source-directory' => 'config', ], 'config-plugin' => [ - 'params' => 'params/*.php', - ], - 'config-plugin-environments' => [ - 'environment' => [ - 'params' => 'environment/params/*.php', + 'params' => [ + 'params/*.php', + ['environment', 'environment/params/*.php'], ], ], ], @@ -57,11 +55,9 @@ public function testWithReverseMerge(): void 'source-directory' => 'config', ], 'config-plugin' => [ - 'params' => 'params/*.php', - ], - 'config-plugin-environments' => [ - 'environment' => [ - 'params' => 'environment/params/*.php', + 'params' => [ + 'params/*.php', + ['environment', 'environment/params/*.php'], ], ], ], diff --git a/tests/Integration/IntegrationTestCase.php b/tests/Integration/IntegrationTestCase.php index 6d0ae51..998c2e9 100644 --- a/tests/Integration/IntegrationTestCase.php +++ b/tests/Integration/IntegrationTestCase.php @@ -105,7 +105,7 @@ protected function runComposerUpdateAndCreateConfig( ?string $environment = null, ?string $paramsGroup = 'params', array $modifiers = [], - bool $echoOutputOnException = true, + bool $echoOutputOnException = false, ): Config { $output = $this->runComposerUpdate($rootPath, $packages, $extra, $configDirectory, $mergePlanFile); diff --git a/tests/Integration/InvalidVariable/InvalidVariableTest.php b/tests/Integration/InvalidVariable/InvalidVariableTest.php new file mode 100644 index 0000000..b4dc01a --- /dev/null +++ b/tests/Integration/InvalidVariable/InvalidVariableTest.php @@ -0,0 +1,42 @@ +runComposerUpdate( + rootPath: __DIR__, + extra: [ + 'config-plugin' => [ + 'params' => 'params.php', + 'params-web' => '$unknown', + ], + ], + ); + + $this->assertStringContainsString('The "unknown" configuration group does not exist.', $output); + } + + public function testCircularDependency(): void + { + $output = $this->runComposerUpdate( + rootPath: __DIR__, + extra: [ + 'config-plugin' => [ + 'params' => 'params.php', + 'params1' => '$params2', + 'params2' => '$params3', + 'params3' => '$params1', + ], + ], + ); + + $this->assertStringContainsString('Circular references in configuration.', $output); + } +} diff --git a/tests/Integration/NestedGroup/NestedGroupTest.php b/tests/Integration/NestedGroup/NestedGroupTest.php new file mode 100644 index 0000000..fe8a5f5 --- /dev/null +++ b/tests/Integration/NestedGroup/NestedGroupTest.php @@ -0,0 +1,54 @@ +runComposerUpdateAndCreateConfig( + rootPath: __DIR__, + packages: [ + 'test/a' => __DIR__ . '/packages/a', + ], + extra: [ + 'config-plugin' => [ + 'params' => [], + 'di' => 'di.php', + 'di-web' => ['$di', 'di-web.php'], + ], + ], + ); + + $this->assertSame(['key' => 42], $config->get('di')); + $this->assertSame(['key' => 42, 'over' => 1, 'test' => 19], $config->get('di-web')); + } + + public function testVendorOverrideLayer(): void + { + $config = $this->runComposerUpdateAndCreateConfig( + rootPath: __DIR__, + packages: [ + 'test/a' => __DIR__ . '/packages/a', + 'test/over' => __DIR__ . '/packages/over', + ], + extra: [ + 'config-plugin' => [ + 'params' => [], + 'di' => 'di.php', + 'di-web' => ['$di', 'di-web.php'], + ], + 'config-plugin-options' => [ + 'vendor-override-layer' => 'test/over', + ], + ], + ); + + $this->assertSame(['key' => 42], $config->get('di')); + $this->assertSame(['key' => 42, 'over' => 2, 'test' => 19], $config->get('di-web')); + } +} diff --git a/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/config/web.php b/tests/Integration/NestedGroup/di-web.php similarity index 52% rename from tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/config/web.php rename to tests/Integration/NestedGroup/di-web.php index 59713f0..efee593 100644 --- a/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/config/web.php +++ b/tests/Integration/NestedGroup/di-web.php @@ -3,5 +3,5 @@ declare(strict_types=1); return [ - 'root-web-key' => 'root-web-value', + 'test' => 19, ]; diff --git a/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/config/alfa/web.php b/tests/Integration/NestedGroup/di.php similarity index 52% rename from tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/config/alfa/web.php rename to tests/Integration/NestedGroup/di.php index dbe0217..95ae27a 100644 --- a/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/config/alfa/web.php +++ b/tests/Integration/NestedGroup/di.php @@ -3,5 +3,5 @@ declare(strict_types=1); return [ - 'alfa-web-key' => 'alfa-web-value', + 'key' => 42, ]; diff --git a/tests/Integration/NestedGroup/packages/a/composer.json b/tests/Integration/NestedGroup/packages/a/composer.json new file mode 100644 index 0000000..8eed753 --- /dev/null +++ b/tests/Integration/NestedGroup/packages/a/composer.json @@ -0,0 +1,9 @@ +{ + "name": "test/a", + "version": "1.0.0", + "extra": { + "config-plugin": { + "di-web": "di-web.php" + } + } +} diff --git a/tests/Integration/NestedGroup/packages/a/di-web.php b/tests/Integration/NestedGroup/packages/a/di-web.php new file mode 100644 index 0000000..1fced08 --- /dev/null +++ b/tests/Integration/NestedGroup/packages/a/di-web.php @@ -0,0 +1,8 @@ + 7, + 'over' => 1, +]; diff --git a/tests/Integration/NestedGroup/packages/over/composer.json b/tests/Integration/NestedGroup/packages/over/composer.json new file mode 100644 index 0000000..3fa5e59 --- /dev/null +++ b/tests/Integration/NestedGroup/packages/over/composer.json @@ -0,0 +1,9 @@ +{ + "name": "test/over", + "version": "1.0.0", + "extra": { + "config-plugin": { + "di-web": "di-web.php" + } + } +} diff --git a/tests/Integration/NestedGroup/packages/over/di-web.php b/tests/Integration/NestedGroup/packages/over/di-web.php new file mode 100644 index 0000000..8802938 --- /dev/null +++ b/tests/Integration/NestedGroup/packages/over/di-web.php @@ -0,0 +1,8 @@ + 2, + 'test' => 53, +]; diff --git a/tests/Integration/NestedGroupWithEnvironment/NestedGroupWithEnvironmentTest.php b/tests/Integration/NestedGroupWithEnvironment/NestedGroupWithEnvironmentTest.php new file mode 100644 index 0000000..3b8e89a --- /dev/null +++ b/tests/Integration/NestedGroupWithEnvironment/NestedGroupWithEnvironmentTest.php @@ -0,0 +1,36 @@ +runComposerUpdateAndCreateConfig( + rootPath: __DIR__, + packages: [ + 'test/a' => __DIR__ . '/packages/a', + ], + extra: [ + 'config-plugin' => [ + 'params' => [ + 'params.php', + ['dev', 'params-dev.php'], + ], + 'params-web' => [ + '$params', + 'params-web.php', + ], + ], + ], + environment: 'dev', + ); + + $this->assertSame(['key' => 'environment'], $config->get('params')); + $this->assertSame(['key' => 'environment'], $config->get('params-web')); + } +} diff --git a/tests/Integration/NestedGroupWithEnvironment/packages/a/composer.json b/tests/Integration/NestedGroupWithEnvironment/packages/a/composer.json new file mode 100644 index 0000000..96c9695 --- /dev/null +++ b/tests/Integration/NestedGroupWithEnvironment/packages/a/composer.json @@ -0,0 +1,9 @@ +{ + "name": "test/a", + "version": "1.0.0", + "extra": { + "config-plugin": { + "params": "params.php" + } + } +} diff --git a/tests/Integration/NestedGroupWithEnvironment/packages/a/params.php b/tests/Integration/NestedGroupWithEnvironment/packages/a/params.php new file mode 100644 index 0000000..3b75052 --- /dev/null +++ b/tests/Integration/NestedGroupWithEnvironment/packages/a/params.php @@ -0,0 +1,7 @@ + 'package', +]; diff --git a/tests/Integration/NestedGroupWithEnvironment/params-dev.php b/tests/Integration/NestedGroupWithEnvironment/params-dev.php new file mode 100644 index 0000000..24fbf23 --- /dev/null +++ b/tests/Integration/NestedGroupWithEnvironment/params-dev.php @@ -0,0 +1,7 @@ + 'environment', +]; diff --git a/tests/Integration/NestedGroupWithEnvironment/params-web.php b/tests/Integration/NestedGroupWithEnvironment/params-web.php new file mode 100644 index 0000000..3d2a385 --- /dev/null +++ b/tests/Integration/NestedGroupWithEnvironment/params-web.php @@ -0,0 +1,7 @@ + 'web', +]; diff --git a/tests/Integration/NestedGroupWithEnvironment/params.php b/tests/Integration/NestedGroupWithEnvironment/params.php new file mode 100644 index 0000000..0dae23d --- /dev/null +++ b/tests/Integration/NestedGroupWithEnvironment/params.php @@ -0,0 +1,5 @@ + [ 'params' => 'params.php', - 'definitions' => 'definitions.php', + 'definitions' => [ + 'definitions.php', + ['environment', 'environment/definitions.php'], + ], 'definitions-backend' => [ '$definitions', 'definitions-backend.php', ], ], - 'config-plugin-environments' => [ - 'environment' => [ - 'definitions' => 'environment/definitions.php', - ], - ], ], configDirectory: 'config', environment: 'environment', diff --git a/tests/Integration/NestedInEnvironment/config/definitions-backend.php b/tests/Integration/NestedInEnvironment/config/definitions-backend.php index a7b277c..63f756f 100644 --- a/tests/Integration/NestedInEnvironment/config/definitions-backend.php +++ b/tests/Integration/NestedInEnvironment/config/definitions-backend.php @@ -6,4 +6,5 @@ return [ 'app-backend' => 2, + 'env-base' => 100, ]; diff --git a/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/ProcessWithIgnoreAdditionalDefaultEnvironmentTest.php b/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/ProcessWithIgnoreAdditionalDefaultEnvironmentTest.php deleted file mode 100644 index ceddff1..0000000 --- a/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/ProcessWithIgnoreAdditionalDefaultEnvironmentTest.php +++ /dev/null @@ -1,70 +0,0 @@ -runComposerUpdateAndCreateConfig( - rootPath: __DIR__, - extra: [ - 'config-plugin-options' => [ - 'source-directory' => 'config', - ], - 'config-plugin' => [ - 'params' => 'params.php', - ], - 'config-plugin-environments' => [ - 'alfa' => [ - 'params' => 'alfa/params.php', - 'web' => 'alfa/web.php', - 'main' => [ - '$web', - 'alfa/main.php', - ], - ], - Options::DEFAULT_ENVIRONMENT => [ - 'params' => 'params.php', - 'web' => 'web.php', - 'main' => [ - '$web', - 'main.php', - ], - ], - ], - ], - configDirectory: 'config', - environment: 'alfa', - ); - - $this->assertSame( - [ - 'root-params-key' => 'root-params-value', - 'alfa-params-key' => 'alfa-params-value', - ], - $config->get('params'), - ); - $this->assertSame( - [ - 'alfa-web-key' => 'alfa-web-value', - ], - $config->get('web'), - ); - $this->assertSame( - [ - 'alfa-web-key' => 'alfa-web-value', - 'alfa-main-key' => 'alfa-main-value', - 'a-web-environment-override-key' => 'alfa-web-override-value', - 'b-web-environment-override-key' => 'alfa-web-override-value', - 'c-web-environment-override-key' => 'alfa-web-override-value', - ], - $config->get('main'), - ); - } -} diff --git a/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/config/alfa/main.php b/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/config/alfa/main.php deleted file mode 100644 index 52f7b4b..0000000 --- a/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/config/alfa/main.php +++ /dev/null @@ -1,10 +0,0 @@ - 'alfa-main-value', - 'a-web-environment-override-key' => 'alfa-web-override-value', - 'b-web-environment-override-key' => 'alfa-web-override-value', - 'c-web-environment-override-key' => 'alfa-web-override-value', -]; diff --git a/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/config/alfa/params.php b/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/config/alfa/params.php deleted file mode 100644 index 48fb9ff..0000000 --- a/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/config/alfa/params.php +++ /dev/null @@ -1,7 +0,0 @@ - 'alfa-params-value', -]; diff --git a/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/config/params.php b/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/config/params.php deleted file mode 100644 index 5df4498..0000000 --- a/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/config/params.php +++ /dev/null @@ -1,7 +0,0 @@ - 'root-params-value', -]; diff --git a/tests/Integration/VariableInEnvironment/VariableInEnvironmentTest.php b/tests/Integration/VariableInEnvironment/VariableInEnvironmentTest.php new file mode 100644 index 0000000..8f1affd --- /dev/null +++ b/tests/Integration/VariableInEnvironment/VariableInEnvironmentTest.php @@ -0,0 +1,31 @@ +runComposerUpdateAndCreateConfig( + rootPath: __DIR__, + extra: [ + 'config-plugin' => [ + 'params' => 'params.php', + 'params-web' => [ + ['dev', '$params'], + ], + ], + ], + environment: 'dev', + ); + + $this->expectException(ErrorException::class); + $this->expectExceptionMessage('Don\'t allow to use variables in environments. Found variable "$params".'); + $config->get('params-web'); + } +} diff --git a/tests/Integration/VariableInEnvironment/params.php b/tests/Integration/VariableInEnvironment/params.php new file mode 100644 index 0000000..0dae23d --- /dev/null +++ b/tests/Integration/VariableInEnvironment/params.php @@ -0,0 +1,5 @@ +get('web') ); } - - public function dataDefaultEnvironment(): array - { - return [ - 'null' => [null], - 'empty-string' => [''], - 'default-environment' => ['/'], - ]; - } - - /** - * @dataProvider dataDefaultEnvironment - */ - public function testDefaultEnvironment(?string $environment): void - { - $config = $this->runComposerUpdateAndCreateConfig( - rootPath: __DIR__, - packages: [ - 'test/a' => __DIR__ . '/packages/a', - ], - extra: [ - 'config-plugin-options' => [ - 'source-directory' => 'config', - ], - 'config-plugin' => [ - 'params' => 'params.php', - 'web1' => 'web1.php', - 'web2' => 'web2.php', - 'web' => [ - '$web1', - '$web2', - ], - ], - ], - configDirectory: 'config', - environment: $environment, - ); - - $this->assertSame( - [ - 'a' => 1, - 'b' => 2, - ], - $config->get('web') - ); - } }