From 071d97d262eeb575d07a327a5b01fdf22a98f92f Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 19 Nov 2024 10:42:31 +0100 Subject: [PATCH 1/2] Remove dependency on laminas-config --- composer.json | 4 ++-- config/config.php | 10 ++++----- src/Command/AbstractInstallCommand.php | 6 +++--- src/Command/SetOptionCommand.php | 6 +++--- src/Util/ConfigWriter.php | 28 ++++++++++++++++++++++++++ src/Util/ConfigWriterInterface.php | 10 +++++++++ test/Command/InstallCommandTest.php | 12 ++++------- test/Command/SetOptionCommandTest.php | 6 +++--- test/Command/UpdateCommandTest.php | 12 ++++------- 9 files changed, 62 insertions(+), 32 deletions(-) create mode 100644 src/Util/ConfigWriter.php create mode 100644 src/Util/ConfigWriterInterface.php diff --git a/composer.json b/composer.json index bd4ee3c..318c8c5 100644 --- a/composer.json +++ b/composer.json @@ -13,14 +13,14 @@ ], "require": { "php": "^8.2", - "laminas/laminas-config": "^3.9", "laminas/laminas-config-aggregator": "^1.15", "laminas/laminas-servicemanager": "^4.2 || ^3.22", "laminas/laminas-stdlib": "^3.19", "shlinkio/shlink-config": "^3.1", "symfony/console": "^7.1", "symfony/filesystem": "^7.1", - "symfony/process": "^7.1" + "symfony/process": "^7.1", + "webimpress/safe-writer": "^2.2" }, "require-dev": { "devster/ubench": "^2.1", diff --git a/config/config.php b/config/config.php index d3771e2..cb9c951 100644 --- a/config/config.php +++ b/config/config.php @@ -4,9 +4,9 @@ namespace Shlinkio\Shlink\Installer; -use Laminas\Config\Writer\PhpArray as PhpArrayConfigWriter; use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory; use Laminas\ServiceManager\Factory\InvokableFactory; +use Shlinkio\Shlink\Installer\Util\ConfigWriter; use Shlinkio\Shlink\Installer\Util\InstallationCommand; use Symfony\Component\Console; use Symfony\Component\Filesystem\Filesystem; @@ -19,7 +19,7 @@ Console\Application::class => Factory\ApplicationFactory::class, Filesystem::class => InvokableFactory::class, PhpExecutableFinder::class => InvokableFactory::class, - PhpArrayConfigWriter::class => InvokableFactory::class, + ConfigWriter::class => InvokableFactory::class, Console\Helper\ProcessHelper::class => Factory\ProcessHelperFactory::class, Service\InstallationCommandsRunner::class => ConfigAbstractFactory::class, @@ -209,17 +209,17 @@ ], Command\InstallCommand::class => [ - PhpArrayConfigWriter::class, + ConfigWriter::class, Service\ShlinkAssetsHandler::class, Config\ConfigGenerator::class, ], Command\UpdateCommand::class => [ - PhpArrayConfigWriter::class, + ConfigWriter::class, Service\ShlinkAssetsHandler::class, Config\ConfigGenerator::class, ], Command\SetOptionCommand::class => [ - PhpArrayConfigWriter::class, + ConfigWriter::class, Service\ShlinkAssetsHandler::class, Config\ConfigOptionsManager::class, Filesystem::class, diff --git a/src/Command/AbstractInstallCommand.php b/src/Command/AbstractInstallCommand.php index 02aa003..6d3c753 100644 --- a/src/Command/AbstractInstallCommand.php +++ b/src/Command/AbstractInstallCommand.php @@ -4,12 +4,12 @@ namespace Shlinkio\Shlink\Installer\Command; -use Laminas\Config\Writer\WriterInterface; use Shlinkio\Shlink\Installer\Command\Model\InitOption; use Shlinkio\Shlink\Installer\Config\ConfigGeneratorInterface; use Shlinkio\Shlink\Installer\Model\ImportedConfig; use Shlinkio\Shlink\Installer\Service\ShlinkAssetsHandler; use Shlinkio\Shlink\Installer\Service\ShlinkAssetsHandlerInterface; +use Shlinkio\Shlink\Installer\Util\ConfigWriterInterface; use Shlinkio\Shlink\Installer\Util\Utils; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\ArrayInput; @@ -20,7 +20,7 @@ abstract class AbstractInstallCommand extends Command { public function __construct( - private readonly WriterInterface $configWriter, + private readonly ConfigWriterInterface $configWriter, private readonly ShlinkAssetsHandlerInterface $assetsHandler, private readonly ConfigGeneratorInterface $configGenerator, ) { @@ -47,7 +47,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $normalizedConfig = Utils::normalizeAndKeepEnvVarKeys($config); // Generate config params files - $this->configWriter->toFile(ShlinkAssetsHandler::GENERATED_CONFIG_PATH, $normalizedConfig, false); + $this->configWriter->toFile(ShlinkAssetsHandler::GENERATED_CONFIG_PATH, $normalizedConfig); $io->text('Custom configuration properly generated!'); $io->newLine(); diff --git a/src/Command/SetOptionCommand.php b/src/Command/SetOptionCommand.php index 4e60f41..acdc88c 100644 --- a/src/Command/SetOptionCommand.php +++ b/src/Command/SetOptionCommand.php @@ -5,13 +5,13 @@ namespace Shlinkio\Shlink\Installer\Command; use Generator; -use Laminas\Config\Writer\WriterInterface; use Shlinkio\Shlink\Installer\Config\ConfigOptionsManagerInterface; use Shlinkio\Shlink\Installer\Config\Option\ConfigOptionInterface; use Shlinkio\Shlink\Installer\Exception\InvalidShlinkPathException; use Shlinkio\Shlink\Installer\Service\ShlinkAssetsHandler; use Shlinkio\Shlink\Installer\Service\ShlinkAssetsHandlerInterface; use Shlinkio\Shlink\Installer\Util\ArrayUtils; +use Shlinkio\Shlink\Installer\Util\ConfigWriterInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -33,7 +33,7 @@ class SetOptionCommand extends Command private string $generatedConfigPath; public function __construct( - private WriterInterface $configWriter, + private ConfigWriterInterface $configWriter, private ShlinkAssetsHandlerInterface $assetsHandler, private ConfigOptionsManagerInterface $optionsManager, private Filesystem $filesystem, @@ -85,7 +85,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $plugin = $this->optionsManager->get($this->groups[$optionTitle]); $answers = include $this->generatedConfigPath; $answers[$plugin->getEnvVar()] = $plugin->ask($io, $answers); - $this->configWriter->toFile($this->generatedConfigPath, $answers, false); + $this->configWriter->toFile($this->generatedConfigPath, $answers); $this->assetsHandler->dropCachedConfigIfAny($io); $io->success('Configuration properly updated'); diff --git a/src/Util/ConfigWriter.php b/src/Util/ConfigWriter.php new file mode 100644 index 0000000..91d8805 --- /dev/null +++ b/src/Util/ConfigWriter.php @@ -0,0 +1,28 @@ +assetsHandler = $this->createMock(ShlinkAssetsHandlerInterface::class); $this->assetsHandler->expects($this->once())->method('dropCachedConfigIfAny'); - $this->configWriter = $this->createMock(WriterInterface::class); + $this->configWriter = $this->createMock(ConfigWriterInterface::class); $configGenerator = $this->createMock(ConfigGeneratorInterface::class); $configGenerator->method('generateConfigInteractively')->willReturn([]); @@ -66,11 +66,7 @@ public function commandIsExecutedAsExpected(): void )->willReturn(0); $this->assetsHandler->expects($this->never())->method('resolvePreviousConfig'); $this->assetsHandler->expects($this->never())->method('importShlinkAssetsFromPath'); - $this->configWriter->expects($this->once())->method('toFile')->with( - $this->anything(), - $this->isType('array'), - false, - ); + $this->configWriter->expects($this->once())->method('toFile')->with($this->anything(), $this->isType('array')); $this->commandTester->setInputs(['no']); $this->commandTester->execute([]); diff --git a/test/Command/SetOptionCommandTest.php b/test/Command/SetOptionCommandTest.php index 71a72b8..0ef290c 100644 --- a/test/Command/SetOptionCommandTest.php +++ b/test/Command/SetOptionCommandTest.php @@ -4,7 +4,6 @@ namespace ShlinkioTest\Shlink\Installer\Command; -use Laminas\Config\Writer\WriterInterface; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -13,6 +12,7 @@ use Shlinkio\Shlink\Installer\Config\Option\ConfigOptionInterface; use Shlinkio\Shlink\Installer\Exception\InvalidShlinkPathException; use Shlinkio\Shlink\Installer\Service\ShlinkAssetsHandlerInterface; +use Shlinkio\Shlink\Installer\Util\ConfigWriterInterface; use Symfony\Component\Console\Application; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Tester\CommandTester; @@ -24,7 +24,7 @@ class SetOptionCommandTest extends TestCase { private CommandTester $commandTester; - private MockObject & WriterInterface $configWriter; + private MockObject & ConfigWriterInterface $configWriter; private MockObject & ShlinkAssetsHandlerInterface $assetsHandler; private MockObject & ConfigOptionsManagerInterface $optionsManager; private MockObject & Filesystem $filesystem; @@ -35,7 +35,7 @@ public function setUp(): void $this->initialCwd = getcwd() ?: ''; chdir(__DIR__ . '/../../test-resources'); - $this->configWriter = $this->createMock(WriterInterface::class); + $this->configWriter = $this->createMock(ConfigWriterInterface::class); $this->assetsHandler = $this->createMock(ShlinkAssetsHandlerInterface::class); $this->optionsManager = $this->createMock(ConfigOptionsManagerInterface::class); $this->filesystem = $this->createMock(Filesystem::class); diff --git a/test/Command/UpdateCommandTest.php b/test/Command/UpdateCommandTest.php index a9e36bd..e137486 100644 --- a/test/Command/UpdateCommandTest.php +++ b/test/Command/UpdateCommandTest.php @@ -4,7 +4,6 @@ namespace ShlinkioTest\Shlink\Installer\Command; -use Laminas\Config\Writer\WriterInterface; use PHPUnit\Framework\Assert; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; @@ -15,6 +14,7 @@ use Shlinkio\Shlink\Installer\Config\ConfigGeneratorInterface; use Shlinkio\Shlink\Installer\Model\ImportedConfig; use Shlinkio\Shlink\Installer\Service\ShlinkAssetsHandlerInterface; +use Shlinkio\Shlink\Installer\Util\ConfigWriterInterface; use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\ArrayInput; @@ -23,7 +23,7 @@ class UpdateCommandTest extends TestCase { private CommandTester $commandTester; - private MockObject & WriterInterface $configWriter; + private MockObject & ConfigWriterInterface $configWriter; private MockObject & ShlinkAssetsHandlerInterface $assetsHandler; private MockObject & Command $initCommand; @@ -32,7 +32,7 @@ public function setUp(): void $this->assetsHandler = $this->createMock(ShlinkAssetsHandlerInterface::class); $this->assetsHandler->expects($this->once())->method('dropCachedConfigIfAny'); - $this->configWriter = $this->createMock(WriterInterface::class); + $this->configWriter = $this->createMock(ConfigWriterInterface::class); $generator = $this->createMock(ConfigGeneratorInterface::class); $generator->method('generateConfigInteractively')->willReturn([]); @@ -67,11 +67,7 @@ public function commandIsExecutedAsExpected(bool $rrBinExists, string $postUpdat ImportedConfig::notImported(), ); $this->assetsHandler->expects($this->once())->method('importShlinkAssetsFromPath'); - $this->configWriter->expects($this->once())->method('toFile')->with( - $this->anything(), - $this->isType('array'), - false, - ); + $this->configWriter->expects($this->once())->method('toFile')->with($this->anything(), $this->isType('array')); $this->commandTester->setInputs(['no']); $this->commandTester->execute([]); From b7a09c50c348f4cb705f0d18cf446e23e3b2c1ae Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 19 Nov 2024 10:58:16 +0100 Subject: [PATCH 2/2] Create ConfigWriter test --- .gitignore | 1 + CHANGELOG.md | 2 +- src/Util/ConfigWriter.php | 1 + test/Util/ConfigWriterTest.php | 48 ++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 test/Util/ConfigWriterTest.php diff --git a/.gitignore b/.gitignore index 099b63d..8ee36dd 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ vendor/ config/*.local.php data bin/rr +test-resources/config/params/generated-in-test.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e8204c..ce0e35b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this * *Nothing* ### Removed -* *Nothing* +* Remove dependency on `laminas/laminas-config`. ### Fixed * *Nothing* diff --git a/src/Util/ConfigWriter.php b/src/Util/ConfigWriter.php index 91d8805..25ba8a1 100644 --- a/src/Util/ConfigWriter.php +++ b/src/Util/ConfigWriter.php @@ -17,6 +17,7 @@ class ConfigWriter implements ConfigWriterInterface /* Shlink config generated by shlink-installer */ return %s; + TEMPLATE; diff --git a/test/Util/ConfigWriterTest.php b/test/Util/ConfigWriterTest.php new file mode 100644 index 0000000..043084a --- /dev/null +++ b/test/Util/ConfigWriterTest.php @@ -0,0 +1,48 @@ +configWriter = new ConfigWriter(); + } + + public static function tearDownAfterClass(): void + { + unlink(self::FILENAME); + } + + #[Test] + #[TestWith([[ + 'foo' => 'foo', + 'bar' => 'bar', + 'baz' => 'baz', + ]])] + #[TestWith([[ + 'foo' => null, + 'bar' => 123, + 'baz' => true, + ]])] + public function configIsExportedAndWrittenToFile(array $config): void + { + $this->configWriter->toFile(self::FILENAME, $config); + + self::assertFileExists(self::FILENAME); + self::assertEquals($config, require self::FILENAME); + } +}