Skip to content

Commit

Permalink
Merge pull request #230 from acelaya-forks/feature/remove-laminas-config
Browse files Browse the repository at this point in the history
Feature/remove laminas config
  • Loading branch information
acelaya authored Nov 19, 2024
2 parents 44f2fa1 + b7a09c5 commit b7503ad
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ vendor/
config/*.local.php
data
bin/rr
test-resources/config/params/generated-in-test.php
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/Command/AbstractInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
) {
Expand All @@ -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('<info>Custom configuration properly generated!</info>');
$io->newLine();

Expand Down
6 changes: 3 additions & 3 deletions src/Command/SetOptionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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');
Expand Down
29 changes: 29 additions & 0 deletions src/Util/ConfigWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Shlinkio\Shlink\Installer\Util;

use Webimpress\SafeWriter\FileWriter;

use function sprintf;
use function var_export;

class ConfigWriter implements ConfigWriterInterface
{
private const CONFIG_TEMPLATE = <<<TEMPLATE
<?php
/* Shlink config generated by shlink-installer */
return %s;
TEMPLATE;


public function toFile(string $fileName, array $config): void
{
$content = sprintf(self::CONFIG_TEMPLATE, var_export($config, return: true));
FileWriter::writeFile($fileName, $content);
}
}
10 changes: 10 additions & 0 deletions src/Util/ConfigWriterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Shlinkio\Shlink\Installer\Util;

interface ConfigWriterInterface
{
public function toFile(string $fileName, array $config): void;
}
12 changes: 4 additions & 8 deletions test/Command/InstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace ShlinkioTest\Shlink\Installer\Command;

use Laminas\Config\Writer\WriterInterface;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -13,6 +12,7 @@
use Shlinkio\Shlink\Installer\Command\InstallCommand;
use Shlinkio\Shlink\Installer\Config\ConfigGeneratorInterface;
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;
Expand All @@ -21,7 +21,7 @@
class InstallCommandTest extends TestCase
{
private CommandTester $commandTester;
private MockObject & WriterInterface $configWriter;
private MockObject & ConfigWriterInterface $configWriter;
private MockObject & ShlinkAssetsHandlerInterface $assetsHandler;
private MockObject & Command $initCommand;

Expand All @@ -30,7 +30,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);

$configGenerator = $this->createMock(ConfigGeneratorInterface::class);
$configGenerator->method('generateConfigInteractively')->willReturn([]);
Expand Down Expand Up @@ -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([]);
Expand Down
6 changes: 3 additions & 3 deletions test/Command/SetOptionCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down
12 changes: 4 additions & 8 deletions test/Command/UpdateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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([]);
Expand Down Expand Up @@ -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([]);
Expand Down
48 changes: 48 additions & 0 deletions test/Util/ConfigWriterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace ShlinkioTest\Shlink\Installer\Util;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Installer\Util\ConfigWriter;

use function unlink;

class ConfigWriterTest extends TestCase
{
private const FILENAME = __DIR__ . '/../../test-resources/config/params/generated-in-test.php';

private ConfigWriter $configWriter;

protected function setUp(): void
{
$this->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);
}
}

0 comments on commit b7503ad

Please sign in to comment.