diff --git a/src/ConfigDiscovery.php b/src/ConfigDiscovery.php index 6a03099..945af07 100644 --- a/src/ConfigDiscovery.php +++ b/src/ConfigDiscovery.php @@ -21,8 +21,10 @@ final class ConfigDiscovery 'config/application.config.php' => ConfigDiscovery\ApplicationConfig::class, 'config/modules.config.php' => ConfigDiscovery\ModulesConfig::class, 'config/development.config.php.dist' => [ - 'dist' => ConfigDiscovery\DevelopmentConfig::class, - 'work' => ConfigDiscovery\DevelopmentWorkConfig::class, + 'dist' => ConfigDiscovery\DevelopmentConfig::class, + 'work' => ConfigDiscovery\DevelopmentWorkConfig::class, + 'mezzio-dist' => ConfigDiscovery\MezzioDevelopmentConfig::class, + 'mezzio-work' => ConfigDiscovery\MezzioDevelopmentWorkConfig::class, ], 'config/config.php' => [ 'aggregator' => ConfigDiscovery\ConfigAggregator::class, @@ -37,8 +39,10 @@ final class ConfigDiscovery 'config/application.config.php' => Injector\ApplicationConfigInjector::class, 'config/modules.config.php' => Injector\ModulesConfigInjector::class, 'config/development.config.php.dist' => [ - 'dist' => Injector\DevelopmentConfigInjector::class, - 'work' => Injector\DevelopmentWorkConfigInjector::class, + 'dist' => Injector\DevelopmentConfigInjector::class, + 'work' => Injector\DevelopmentWorkConfigInjector::class, + 'mezzio-dist' => Injector\MezzioDevelopmentConfigInjector::class, + 'mezzio-work' => Injector\MezzioDevelopmentWorkConfigInjector::class, ], 'config/config.php' => [ 'aggregator' => Injector\ConfigAggregatorInjector::class, diff --git a/src/ConfigDiscovery/ConfigAggregator.php b/src/ConfigDiscovery/ConfigAggregator.php index 13870a2..782c7b3 100644 --- a/src/ConfigDiscovery/ConfigAggregator.php +++ b/src/ConfigDiscovery/ConfigAggregator.php @@ -15,7 +15,7 @@ final class ConfigAggregator extends AbstractDiscovery /** * Configuration file to look for. */ - protected string $configFile = 'config/config.php'; + protected string $configFile; /** * Expected pattern to match if the configuration file exists. @@ -24,9 +24,10 @@ final class ConfigAggregator extends AbstractDiscovery */ protected string $expected = ''; - public function __construct(string $projectDirectory = '') + public function __construct(string $projectDirectory = '', string $configFile = 'config/config.php') { - $this->expected = sprintf( + $this->configFile = $configFile; + $this->expected = sprintf( '/new (?:%s?%s)?ConfigAggregator\(\s*(?:array\(|\[)/s', preg_quote('\\'), preg_quote('Laminas\ConfigAggregator\\') diff --git a/src/ConfigDiscovery/MezzioDevelopmentConfig.php b/src/ConfigDiscovery/MezzioDevelopmentConfig.php new file mode 100644 index 0000000..6a774c9 --- /dev/null +++ b/src/ConfigDiscovery/MezzioDevelopmentConfig.php @@ -0,0 +1,25 @@ +discovery = new ConfigAggregator($projectDirectory, self::CONFIG_FILE); + } + + public function locate(): bool + { + return $this->discovery->locate(); + } +} diff --git a/src/ConfigDiscovery/MezzioDevelopmentWorkConfig.php b/src/ConfigDiscovery/MezzioDevelopmentWorkConfig.php new file mode 100644 index 0000000..b48b068 --- /dev/null +++ b/src/ConfigDiscovery/MezzioDevelopmentWorkConfig.php @@ -0,0 +1,25 @@ +discovery = new ConfigAggregator($projectDirectory, self::CONFIG_FILE); + } + + public function locate(): bool + { + return $this->discovery->locate(); + } +} diff --git a/src/Injector/ConditionalDiscoveryTrait.php b/src/Injector/ConditionalDiscoveryTrait.php index 8e7ab70..a0353b6 100644 --- a/src/Injector/ConditionalDiscoveryTrait.php +++ b/src/Injector/ConditionalDiscoveryTrait.php @@ -40,7 +40,7 @@ public function remove(string $package): bool private function validConfigAggregatorConfig(): bool { $discoveryClass = $this->getDiscoveryClass(); - $discovery = new $discoveryClass($this->getProjectRoot()); + $discovery = new $discoveryClass($this->getProjectRoot(), $this->configFile); return $discovery->locate(); } diff --git a/src/Injector/ConfigAggregatorInjector.php b/src/Injector/ConfigAggregatorInjector.php index 3271b76..274f7b4 100644 --- a/src/Injector/ConfigAggregatorInjector.php +++ b/src/Injector/ConfigAggregatorInjector.php @@ -18,6 +18,7 @@ final class ConfigAggregatorInjector extends AbstractInjector { use ConditionalDiscoveryTrait; + /** @var non-empty-string */ public const DEFAULT_CONFIG_FILE = 'config/config.php'; /** @var list */ @@ -25,9 +26,6 @@ final class ConfigAggregatorInjector extends AbstractInjector self::TYPE_CONFIG_PROVIDER, ]; - /** @var non-empty-string */ - protected string $configFile = self::DEFAULT_CONFIG_FILE; - /** * Discovery class, for testing if this injector is valid for the given * configuration. @@ -68,9 +66,13 @@ final class ConfigAggregatorInjector extends AbstractInjector * * Sets $isRegisteredPattern and pattern for $injectionPatterns to ensure * proper PCRE quoting. + * + * @param non-empty-string $configFile */ - public function __construct(string $projectRoot = '') + public function __construct(string $projectRoot = '', string $configFile = self::DEFAULT_CONFIG_FILE) { + $this->configFile = $configFile; + $ns = preg_quote('\\'); $this->isRegisteredPattern = '/new (?:' . $ns @@ -96,7 +98,7 @@ public function __construct(string $projectRoot = '') protected function getDefaultConfigFile(): string { - return self::DEFAULT_CONFIG_FILE; + return $this->configFile; } protected function getDiscoveryClass(): string diff --git a/src/Injector/MezzioDevelopmentConfigInjector.php b/src/Injector/MezzioDevelopmentConfigInjector.php new file mode 100644 index 0000000..6ed3b8e --- /dev/null +++ b/src/Injector/MezzioDevelopmentConfigInjector.php @@ -0,0 +1,55 @@ +injector = new ConfigAggregatorInjector($projectRoot, self::CONFIG_FILE); + } + + public function registersType(int $type): bool + { + return $this->injector->registersType($type); + } + + public function getTypesAllowed(): array + { + return $this->injector->getTypesAllowed(); + } + + public function isRegistered(string $package): bool + { + return $this->injector->isRegistered($package); + } + + public function inject(string $package, int $type): bool + { + return $this->injector->inject($package, $type); + } + + public function remove(string $package): bool + { + return $this->injector->remove($package); + } + + public function setApplicationModules(array $modules): InjectorInterface + { + return $this->injector->setApplicationModules($modules); + } + + public function setModuleDependencies(array $modules): InjectorInterface + { + return $this->injector->setModuleDependencies($modules); + } +} diff --git a/src/Injector/MezzioDevelopmentWorkConfigInjector.php b/src/Injector/MezzioDevelopmentWorkConfigInjector.php new file mode 100644 index 0000000..7a14b60 --- /dev/null +++ b/src/Injector/MezzioDevelopmentWorkConfigInjector.php @@ -0,0 +1,55 @@ +injector = new ConfigAggregatorInjector($projectRoot, self::CONFIG_FILE); + } + + public function registersType(int $type): bool + { + return $this->injector->registersType($type); + } + + public function getTypesAllowed(): array + { + return $this->injector->getTypesAllowed(); + } + + public function isRegistered(string $package): bool + { + return $this->injector->isRegistered($package); + } + + public function inject(string $package, int $type): bool + { + return $this->injector->inject($package, $type); + } + + public function remove(string $package): bool + { + return $this->injector->remove($package); + } + + public function setApplicationModules(array $modules): InjectorInterface + { + return $this->injector->setApplicationModules($modules); + } + + public function setModuleDependencies(array $modules): InjectorInterface + { + return $this->injector->setModuleDependencies($modules); + } +} diff --git a/test/ConfigDiscovery/AbstractConfigAggregatorTestCase.php b/test/ConfigDiscovery/AbstractConfigAggregatorTestCase.php new file mode 100644 index 0000000..6d6eefc --- /dev/null +++ b/test/ConfigDiscovery/AbstractConfigAggregatorTestCase.php @@ -0,0 +1,74 @@ + */ + protected string $discoveryClass; + + protected string $configFile; + + protected function setUp(): void + { + $this->configDir = vfsStream::setup('project'); + $this->locator = new $this->discoveryClass( + vfsStream::url('project') + ); + } + + public function testAbsenceOfFileReturnsFalseOnLocate(): void + { + $this->assertFalse($this->locator->locate()); + } + + public function testLocateReturnsFalseWhenFileDoesNotHaveExpectedContents(): void + { + vfsStream::newFile($this->configFile) + ->at($this->configDir) + ->setContent('<' . "?php\nreturn [];"); + $this->assertFalse($this->locator->locate()); + } + + /** + * @psalm-return array + */ + public function validMezzioConfigContents(): array + { + // @codingStandardsIgnoreStart + return [ + 'fqcn-short-array' => ['<' . "?php\n\$aggregator = new Laminas\ConfigAggregator\ConfigAggregator([\n]);"], + 'globally-qualified-short-array' => ['<' . "?php\n\$aggregator = new \Laminas\ConfigAggregator\ConfigAggregator([\n]);"], + 'imported-short-array' => ['<' . "?php\n\$aggregator = new ConfigAggregator([\n]);"], + 'fqcn-long-array' => ['<' . "?php\n\$aggregator = new Laminas\ConfigAggregator\ConfigAggregator(array(\n));"], + 'globally-qualified-long-array' => ['<' . "?php\n\$aggregator = new \Laminas\ConfigAggregator\ConfigAggregator(array(\n));"], + 'imported-long-array' => ['<' . "?php\n\$aggregator = new ConfigAggregator(array(\n));"], + ]; + // @codingStandardsIgnoreEnd + } + + /** + * @dataProvider validMezzioConfigContents + */ + public function testLocateReturnsTrueWhenFileExistsAndHasExpectedContent(string $contents): void + { + vfsStream::newFile($this->configFile) + ->at($this->configDir) + ->setContent($contents); + + $this->assertTrue($this->locator->locate()); + } +} diff --git a/test/ConfigDiscovery/ConfigAggregatorTest.php b/test/ConfigDiscovery/ConfigAggregatorTest.php index 16ee693..ab00076 100644 --- a/test/ConfigDiscovery/ConfigAggregatorTest.php +++ b/test/ConfigDiscovery/ConfigAggregatorTest.php @@ -5,67 +5,11 @@ namespace LaminasTest\ComponentInstaller\ConfigDiscovery; use Laminas\ComponentInstaller\ConfigDiscovery\ConfigAggregator; -use org\bovigo\vfs\vfsStream; -use org\bovigo\vfs\vfsStreamDirectory; -use PHPUnit\Framework\TestCase; +use Laminas\ComponentInstaller\ConfigDiscovery\DiscoveryInterface; -class ConfigAggregatorTest extends TestCase +final class ConfigAggregatorTest extends AbstractConfigAggregatorTestCase { - /** @var vfsStreamDirectory */ - private $configDir; - - /** @var ConfigAggregator */ - private $locator; - - protected function setUp(): void - { - $this->configDir = vfsStream::setup('project'); - $this->locator = new ConfigAggregator( - vfsStream::url('project') - ); - } - - public function testAbsenceOfFileReturnsFalseOnLocate(): void - { - $this->assertFalse($this->locator->locate()); - } - - public function testLocateReturnsFalseWhenFileDoesNotHaveExpectedContents(): void - { - vfsStream::newFile('config/config.php') - ->at($this->configDir) - ->setContent('<' . "?php\nreturn [];"); - $this->assertFalse($this->locator->locate()); - } - - /** - * @psalm-return array - */ - public function validMezzioConfigContents(): array - { - // @codingStandardsIgnoreStart - return [ - 'fqcn-short-array' => ['<' . "?php\n\$aggregator = new Laminas\ConfigAggregator\ConfigAggregator([\n]);"], - 'globally-qualified-short-array' => ['<' . "?php\n\$aggregator = new \Laminas\ConfigAggregator\ConfigAggregator([\n]);"], - 'imported-short-array' => ['<' . "?php\n\$aggregator = new ConfigAggregator([\n]);"], - 'fqcn-long-array' => ['<' . "?php\n\$aggregator = new Laminas\ConfigAggregator\ConfigAggregator(array(\n));"], - 'globally-qualified-long-array' => ['<' . "?php\n\$aggregator = new \Laminas\ConfigAggregator\ConfigAggregator(array(\n));"], - 'imported-long-array' => ['<' . "?php\n\$aggregator = new ConfigAggregator(array(\n));"], - ]; - // @codingStandardsIgnoreEnd - } - - /** - * @dataProvider validMezzioConfigContents - */ - public function testLocateReturnsTrueWhenFileExistsAndHasExpectedContent(string $contents): void - { - vfsStream::newFile('config/config.php') - ->at($this->configDir) - ->setContent($contents); - - $this->assertTrue($this->locator->locate()); - } + /** @var class-string */ + protected string $discoveryClass = ConfigAggregator::class; + protected string $configFile = 'config/config.php'; } diff --git a/test/ConfigDiscovery/MezzioDevelopmentConfigTest.php b/test/ConfigDiscovery/MezzioDevelopmentConfigTest.php new file mode 100644 index 0000000..a0790ea --- /dev/null +++ b/test/ConfigDiscovery/MezzioDevelopmentConfigTest.php @@ -0,0 +1,15 @@ + */ + protected string $discoveryClass = MezzioDevelopmentConfig::class; +} diff --git a/test/ConfigDiscovery/MezzioDevelopmentWorkConfigTest.php b/test/ConfigDiscovery/MezzioDevelopmentWorkConfigTest.php new file mode 100644 index 0000000..8bc55e5 --- /dev/null +++ b/test/ConfigDiscovery/MezzioDevelopmentWorkConfigTest.php @@ -0,0 +1,15 @@ + */ + protected string $discoveryClass = MezzioDevelopmentWorkConfig::class; + protected string $configFile = 'config/development.config.php'; +} diff --git a/test/ConfigDiscoveryTest.php b/test/ConfigDiscoveryTest.php index c7ba7bd..db85846 100644 --- a/test/ConfigDiscoveryTest.php +++ b/test/ConfigDiscoveryTest.php @@ -69,6 +69,14 @@ public function createDevelopmentWorkConfig(): void $this->createDevelopmentConfig(false); } + public function createMezzioDevelopmentConfig(bool $dist = true): void + { + $configFileName = 'config/development.config.php' . ($dist ? '.dist' : ''); + vfsStream::newFile($configFileName) + ->at($this->projectRoot) + ->setContent('<' . "?php\n\$aggregator = new ConfigAggregator([\n]);"); + } + public function createAggregatorConfig(): void { vfsStream::newFile('config/config.php') @@ -157,6 +165,7 @@ public function testGetAvailableConfigOptionsReturnsOptionsForEachSupportedPacka $this->createApplicationConfig(); $this->createDevelopmentConfig(); $this->createAggregatorConfig(); + $this->createMezzioDevelopmentConfig(); $this->createMezzioConfig(); $this->createModulesConfig(); diff --git a/test/Injector/AbstractConfigAggregatorInjectorTestCase.php b/test/Injector/AbstractConfigAggregatorInjectorTestCase.php new file mode 100644 index 0000000..12b6c31 --- /dev/null +++ b/test/Injector/AbstractConfigAggregatorInjectorTestCase.php @@ -0,0 +1,169 @@ + + */ + protected $injectorTypesAllowed = [ + InjectorInterface::TYPE_CONFIG_PROVIDER, + ]; + + public function convertToShortArraySyntax(string $contents): string + { + return preg_replace('/array\(([^)]+)\)/s', '[$1]', $contents); + } + + public function allowedTypes(): array + { + return [ + 'config-provider' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, true], + 'component' => [ConfigAggregatorInjector::TYPE_COMPONENT, false], + 'module' => [ConfigAggregatorInjector::TYPE_MODULE, false], + ]; + } + + public function injectComponentProvider(): array + { + // phpcs:disable Generic.Files.LineLength.TooLong + $baseContentsFqcnLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-fqcn.config.php'); + $baseContentsGloballyQualifiedLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-globally-qualified.config.php'); + $baseContentsImportLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-import.config.php'); + $baseContentsImportLongArrayAltIndent = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-import-alt-indent.config.php'); + + $baseContentsFqcnShortArray = $this->convertToShortArraySyntax($baseContentsFqcnLongArray); + $baseContentsGloballyQualifiedShortArray = $this->convertToShortArraySyntax($baseContentsGloballyQualifiedLongArray); + $baseContentsImportShortArray = $this->convertToShortArraySyntax($baseContentsImportLongArray); + $baseContentsImportShortArrayAltIndent = $this->convertToShortArraySyntax($baseContentsImportLongArrayAltIndent); + + $expectedContentsFqcnLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-fqcn.config.php'); + $expectedContentsGloballyQualifiedLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-globally-qualified.config.php'); + $expectedContentsImportLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-import.config.php'); + $expectedContentsImportLongArrayAltIndent = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-import-alt-indent.config.php'); + + $expectedContentsFqcnShortArray = $this->convertToShortArraySyntax($expectedContentsFqcnLongArray); + $expectedContentsGloballyQualifiedShortArray = $this->convertToShortArraySyntax($expectedContentsGloballyQualifiedLongArray); + $expectedContentsImportShortArray = $this->convertToShortArraySyntax($expectedContentsImportLongArray); + $expectedContentsImportShortArrayAltIndent = $this->convertToShortArraySyntax($expectedContentsImportLongArrayAltIndent); + + $injectOnlyFirstOccurrenceInitial = file_get_contents(__DIR__ . '/TestAsset/mezzio-with-postprocessor.config.php'); + $injectOnlyFirstOccurrenceExpected = file_get_contents(__DIR__ . '/TestAsset/mezzio-with-postprocessor-post-injection.config.php'); + + return [ + 'fqcn-long-array' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $baseContentsFqcnLongArray, $expectedContentsFqcnLongArray], + 'global-long-array' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $baseContentsGloballyQualifiedLongArray, $expectedContentsGloballyQualifiedLongArray], + 'import-long-array' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $baseContentsImportLongArray, $expectedContentsImportLongArray], + 'import-long-array-alt-indent' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $baseContentsImportLongArrayAltIndent, $expectedContentsImportLongArrayAltIndent], + 'fqcn-short-array' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $baseContentsFqcnShortArray, $expectedContentsFqcnShortArray], + 'global-short-array' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $baseContentsGloballyQualifiedShortArray, $expectedContentsGloballyQualifiedShortArray], + 'import-short-array' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $baseContentsImportShortArray, $expectedContentsImportShortArray], + 'import-short-array-alt-indent' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $baseContentsImportShortArrayAltIndent, $expectedContentsImportShortArrayAltIndent], + + // see https://github.com/laminas/laminas-component-installer/issues/21 + 'inject-only-first-occurence' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $injectOnlyFirstOccurrenceInitial, $injectOnlyFirstOccurrenceExpected], + ]; + // phpcs:enable + } + + public function packageAlreadyRegisteredProvider(): array + { + // phpcs:disable Generic.Files.LineLength.TooLong + $fqcnLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-fqcn.config.php'); + $globallyQualifiedLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-globally-qualified.config.php'); + $importLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-import.config.php'); + + $fqcnShortArray = $this->convertToShortArraySyntax($fqcnLongArray); + $globallyQualifiedShortArray = $this->convertToShortArraySyntax($globallyQualifiedLongArray); + $importShortArray = $this->convertToShortArraySyntax($importLongArray); + + return [ + 'fqcn-long-array' => [$fqcnLongArray, ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER], + 'global-long-array' => [$globallyQualifiedLongArray, ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER], + 'import-long-array' => [$importLongArray, ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER], + 'fqcn-short-array' => [$fqcnShortArray, ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER], + 'global-short-array' => [$globallyQualifiedShortArray, ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER], + 'import-short-array' => [$importShortArray, ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER], + ]; + // phpcs:enable + } + + public function emptyConfiguration(): array + { + // phpcs:disable Generic.Files.LineLength.TooLong + $fqcnLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-empty-fqcn.config.php'); + $globallyQualifiedLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-empty-globally-qualified.config.php'); + $importLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-empty-import.config.php'); + // phpcs:enable + + $fqcnShortArray = $this->convertToShortArraySyntax($fqcnLongArray); + $globallyQualifiedShortArray = $this->convertToShortArraySyntax($globallyQualifiedLongArray); + $importShortArray = $this->convertToShortArraySyntax($importLongArray); + + return [ + 'fqcn-long-array' => [$fqcnLongArray], + 'global-long-array' => [$globallyQualifiedLongArray], + 'import-long-array' => [$importLongArray], + 'fqcn-short-array' => [$fqcnShortArray], + 'global-short-array' => [$globallyQualifiedShortArray], + 'import-short-array' => [$importShortArray], + ]; + } + + public function packagePopulatedInConfiguration(): array + { + // phpcs:disable Generic.Files.LineLength.TooLong + $baseContentsFqcnLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-fqcn.config.php'); + $baseContentsGloballyQualifiedLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-globally-qualified.config.php'); + $baseContentsImportLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-import.config.php'); + $baseContentsImportLongArrayAltIndent = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-import-alt-indent.config.php'); + + $baseContentsFqcnShortArray = $this->convertToShortArraySyntax($baseContentsFqcnLongArray); + $baseContentsGloballyQualifiedShortArray = $this->convertToShortArraySyntax($baseContentsGloballyQualifiedLongArray); + $baseContentsImportShortArray = $this->convertToShortArraySyntax($baseContentsImportLongArray); + $baseContentsImportShortArrayAltIndent = $this->convertToShortArraySyntax($baseContentsImportLongArrayAltIndent); + + $expectedContentsFqcnLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-fqcn.config.php'); + $expectedContentsGloballyQualifiedLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-globally-qualified.config.php'); + $expectedContentsImportLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-import.config.php'); + $expectedContentsImportLongArrayAltIndent = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-import-alt-indent.config.php'); + + $expectedContentsFqcnShortArray = $this->convertToShortArraySyntax($expectedContentsFqcnLongArray); + $expectedContentsGloballyQualifiedShortArray = $this->convertToShortArraySyntax($expectedContentsGloballyQualifiedLongArray); + $expectedContentsImportShortArray = $this->convertToShortArraySyntax($expectedContentsImportLongArray); + $expectedContentsImportShortArrayAltIndent = $this->convertToShortArraySyntax($expectedContentsImportLongArrayAltIndent); + + return [ + 'fqcn-long-array' => [$baseContentsFqcnLongArray, $expectedContentsFqcnLongArray], + 'global-long-array' => [$baseContentsGloballyQualifiedLongArray, $expectedContentsGloballyQualifiedLongArray], + 'import-long-array' => [$baseContentsImportLongArray, $expectedContentsImportLongArray], + 'import-long-array-alt-indent' => [$baseContentsImportLongArrayAltIndent, $expectedContentsImportLongArrayAltIndent], + 'fqcn-short-array' => [$baseContentsFqcnShortArray, $expectedContentsFqcnShortArray], + 'global-short-array' => [$baseContentsGloballyQualifiedShortArray, $expectedContentsGloballyQualifiedShortArray], + 'import-short-array' => [$baseContentsImportShortArray, $expectedContentsImportShortArray], + 'import-short-array-alt-indent' => [$baseContentsImportShortArrayAltIndent, $expectedContentsImportShortArrayAltIndent], + ]; + // phpcs:enable + } + + public function testProperlyDetectsExistingConfigProviderInConfigWithMixedRelativeAndGloballyQualifiedNames(): void + { + $contents = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-from-skeleton.config.php'); + vfsStream::newFile($this->configFile) + ->at($this->configDir) + ->setContent($contents); + + $this->assertTrue($this->injector->isRegistered('Laminas\Validator\ConfigProvider')); + } +} diff --git a/test/Injector/ConfigAggregatorInjectorTest.php b/test/Injector/ConfigAggregatorInjectorTest.php index c203872..e439919 100644 --- a/test/Injector/ConfigAggregatorInjectorTest.php +++ b/test/Injector/ConfigAggregatorInjectorTest.php @@ -6,173 +6,14 @@ use Laminas\ComponentInstaller\Injector\ConfigAggregatorInjector; use Laminas\ComponentInstaller\Injector\InjectorInterface; -use org\bovigo\vfs\vfsStream; -use function file_get_contents; -use function preg_replace; - -class ConfigAggregatorInjectorTest extends AbstractInjectorTestCase +final class ConfigAggregatorInjectorTest extends AbstractConfigAggregatorInjectorTestCase { /** @var non-empty-string */ protected $configFile = 'config/config.php'; - /** * @var string * @psalm-var class-string */ protected $injectorClass = ConfigAggregatorInjector::class; - - /** - * @var array - * @psalm-var list - */ - protected $injectorTypesAllowed = [ - InjectorInterface::TYPE_CONFIG_PROVIDER, - ]; - - public function convertToShortArraySyntax(string $contents): string - { - return preg_replace('/array\(([^)]+)\)/s', '[$1]', $contents); - } - - public function allowedTypes(): array - { - return [ - 'config-provider' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, true], - 'component' => [ConfigAggregatorInjector::TYPE_COMPONENT, false], - 'module' => [ConfigAggregatorInjector::TYPE_MODULE, false], - ]; - } - - public function injectComponentProvider(): array - { - // phpcs:disable Generic.Files.LineLength.TooLong - $baseContentsFqcnLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-fqcn.config.php'); - $baseContentsGloballyQualifiedLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-globally-qualified.config.php'); - $baseContentsImportLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-import.config.php'); - $baseContentsImportLongArrayAltIndent = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-import-alt-indent.config.php'); - - $baseContentsFqcnShortArray = $this->convertToShortArraySyntax($baseContentsFqcnLongArray); - $baseContentsGloballyQualifiedShortArray = $this->convertToShortArraySyntax($baseContentsGloballyQualifiedLongArray); - $baseContentsImportShortArray = $this->convertToShortArraySyntax($baseContentsImportLongArray); - $baseContentsImportShortArrayAltIndent = $this->convertToShortArraySyntax($baseContentsImportLongArrayAltIndent); - - $expectedContentsFqcnLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-fqcn.config.php'); - $expectedContentsGloballyQualifiedLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-globally-qualified.config.php'); - $expectedContentsImportLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-import.config.php'); - $expectedContentsImportLongArrayAltIndent = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-import-alt-indent.config.php'); - - $expectedContentsFqcnShortArray = $this->convertToShortArraySyntax($expectedContentsFqcnLongArray); - $expectedContentsGloballyQualifiedShortArray = $this->convertToShortArraySyntax($expectedContentsGloballyQualifiedLongArray); - $expectedContentsImportShortArray = $this->convertToShortArraySyntax($expectedContentsImportLongArray); - $expectedContentsImportShortArrayAltIndent = $this->convertToShortArraySyntax($expectedContentsImportLongArrayAltIndent); - - $injectOnlyFirstOccurrenceInitial = file_get_contents(__DIR__ . '/TestAsset/mezzio-with-postprocessor.config.php'); - $injectOnlyFirstOccurrenceExpected = file_get_contents(__DIR__ . '/TestAsset/mezzio-with-postprocessor-post-injection.config.php'); - - return [ - 'fqcn-long-array' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $baseContentsFqcnLongArray, $expectedContentsFqcnLongArray], - 'global-long-array' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $baseContentsGloballyQualifiedLongArray, $expectedContentsGloballyQualifiedLongArray], - 'import-long-array' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $baseContentsImportLongArray, $expectedContentsImportLongArray], - 'import-long-array-alt-indent' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $baseContentsImportLongArrayAltIndent, $expectedContentsImportLongArrayAltIndent], - 'fqcn-short-array' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $baseContentsFqcnShortArray, $expectedContentsFqcnShortArray], - 'global-short-array' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $baseContentsGloballyQualifiedShortArray, $expectedContentsGloballyQualifiedShortArray], - 'import-short-array' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $baseContentsImportShortArray, $expectedContentsImportShortArray], - 'import-short-array-alt-indent' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $baseContentsImportShortArrayAltIndent, $expectedContentsImportShortArrayAltIndent], - - // see https://github.com/laminas/laminas-component-installer/issues/21 - 'inject-only-first-occurence' => [ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER, $injectOnlyFirstOccurrenceInitial, $injectOnlyFirstOccurrenceExpected], - ]; - // phpcs:enable - } - - public function packageAlreadyRegisteredProvider(): array - { - // phpcs:disable Generic.Files.LineLength.TooLong - $fqcnLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-fqcn.config.php'); - $globallyQualifiedLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-globally-qualified.config.php'); - $importLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-import.config.php'); - - $fqcnShortArray = $this->convertToShortArraySyntax($fqcnLongArray); - $globallyQualifiedShortArray = $this->convertToShortArraySyntax($globallyQualifiedLongArray); - $importShortArray = $this->convertToShortArraySyntax($importLongArray); - - return [ - 'fqcn-long-array' => [$fqcnLongArray, ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER], - 'global-long-array' => [$globallyQualifiedLongArray, ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER], - 'import-long-array' => [$importLongArray, ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER], - 'fqcn-short-array' => [$fqcnShortArray, ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER], - 'global-short-array' => [$globallyQualifiedShortArray, ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER], - 'import-short-array' => [$importShortArray, ConfigAggregatorInjector::TYPE_CONFIG_PROVIDER], - ]; - // phpcs:enable - } - - public function emptyConfiguration(): array - { - // phpcs:disable Generic.Files.LineLength.TooLong - $fqcnLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-empty-fqcn.config.php'); - $globallyQualifiedLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-empty-globally-qualified.config.php'); - $importLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-empty-import.config.php'); - // phpcs:enable - - $fqcnShortArray = $this->convertToShortArraySyntax($fqcnLongArray); - $globallyQualifiedShortArray = $this->convertToShortArraySyntax($globallyQualifiedLongArray); - $importShortArray = $this->convertToShortArraySyntax($importLongArray); - - return [ - 'fqcn-long-array' => [$fqcnLongArray], - 'global-long-array' => [$globallyQualifiedLongArray], - 'import-long-array' => [$importLongArray], - 'fqcn-short-array' => [$fqcnShortArray], - 'global-short-array' => [$globallyQualifiedShortArray], - 'import-short-array' => [$importShortArray], - ]; - } - - public function packagePopulatedInConfiguration(): array - { - // phpcs:disable Generic.Files.LineLength.TooLong - $baseContentsFqcnLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-fqcn.config.php'); - $baseContentsGloballyQualifiedLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-globally-qualified.config.php'); - $baseContentsImportLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-import.config.php'); - $baseContentsImportLongArrayAltIndent = file_get_contents(__DIR__ . '/TestAsset/mezzio-populated-import-alt-indent.config.php'); - - $baseContentsFqcnShortArray = $this->convertToShortArraySyntax($baseContentsFqcnLongArray); - $baseContentsGloballyQualifiedShortArray = $this->convertToShortArraySyntax($baseContentsGloballyQualifiedLongArray); - $baseContentsImportShortArray = $this->convertToShortArraySyntax($baseContentsImportLongArray); - $baseContentsImportShortArrayAltIndent = $this->convertToShortArraySyntax($baseContentsImportLongArrayAltIndent); - - $expectedContentsFqcnLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-fqcn.config.php'); - $expectedContentsGloballyQualifiedLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-globally-qualified.config.php'); - $expectedContentsImportLongArray = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-import.config.php'); - $expectedContentsImportLongArrayAltIndent = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-import-alt-indent.config.php'); - - $expectedContentsFqcnShortArray = $this->convertToShortArraySyntax($expectedContentsFqcnLongArray); - $expectedContentsGloballyQualifiedShortArray = $this->convertToShortArraySyntax($expectedContentsGloballyQualifiedLongArray); - $expectedContentsImportShortArray = $this->convertToShortArraySyntax($expectedContentsImportLongArray); - $expectedContentsImportShortArrayAltIndent = $this->convertToShortArraySyntax($expectedContentsImportLongArrayAltIndent); - - return [ - 'fqcn-long-array' => [$baseContentsFqcnLongArray, $expectedContentsFqcnLongArray], - 'global-long-array' => [$baseContentsGloballyQualifiedLongArray, $expectedContentsGloballyQualifiedLongArray], - 'import-long-array' => [$baseContentsImportLongArray, $expectedContentsImportLongArray], - 'import-long-array-alt-indent' => [$baseContentsImportLongArrayAltIndent, $expectedContentsImportLongArrayAltIndent], - 'fqcn-short-array' => [$baseContentsFqcnShortArray, $expectedContentsFqcnShortArray], - 'global-short-array' => [$baseContentsGloballyQualifiedShortArray, $expectedContentsGloballyQualifiedShortArray], - 'import-short-array' => [$baseContentsImportShortArray, $expectedContentsImportShortArray], - 'import-short-array-alt-indent' => [$baseContentsImportShortArrayAltIndent, $expectedContentsImportShortArrayAltIndent], - ]; - // phpcs:enable - } - - public function testProperlyDetectsExistingConfigProviderInConfigWithMixedRelativeAndGloballyQualifiedNames(): void - { - $contents = file_get_contents(__DIR__ . '/TestAsset/mezzio-application-from-skeleton.config.php'); - vfsStream::newFile('config/config.php') - ->at($this->configDir) - ->setContent($contents); - - $this->assertTrue($this->injector->isRegistered('Laminas\Validator\ConfigProvider')); - } } diff --git a/test/Injector/MezzioDevelopmentConfigInjectorTest.php b/test/Injector/MezzioDevelopmentConfigInjectorTest.php new file mode 100644 index 0000000..6295260 --- /dev/null +++ b/test/Injector/MezzioDevelopmentConfigInjectorTest.php @@ -0,0 +1,19 @@ + */ + protected $injectorClass = MezzioDevelopmentConfigInjector::class; +} diff --git a/test/Injector/MezzioDevelopmentWorkConfigInjectorTest.php b/test/Injector/MezzioDevelopmentWorkConfigInjectorTest.php new file mode 100644 index 0000000..f5c283a --- /dev/null +++ b/test/Injector/MezzioDevelopmentWorkConfigInjectorTest.php @@ -0,0 +1,19 @@ + */ + protected $injectorClass = MezzioDevelopmentWorkConfigInjector::class; +}