Skip to content

Commit

Permalink
Merge pull request #224 from acelaya-forks/feature/service-manager-4
Browse files Browse the repository at this point in the history
Add support for laminas-servicemanager 4
  • Loading branch information
acelaya authored Jul 22, 2024
2 parents ccda72e + 08ac905 commit 6a8e2d9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
### Added
* Add `ROBOTS_ALLOW_ALL_SHORT_URLS` config option.
* Add `ROBOTS_USER_AGENTS` config option.
* Add support for `laminas/laminas-servicemanager` v4.x.

### Changed
* Update to PHPUnit 11
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"php": "^8.2",
"laminas/laminas-config": "^3.9",
"laminas/laminas-config-aggregator": "^1.15",
"laminas/laminas-servicemanager": "^3.22",
"laminas/laminas-servicemanager": "^4.2 || ^3.22",
"laminas/laminas-stdlib": "^3.19",
"shlinkio/shlink-config": "^3.0",
"shlinkio/shlink-config": "^3.1",
"symfony/console": "^7.1",
"symfony/filesystem": "^7.1",
"symfony/process": "^7.1"
Expand All @@ -26,7 +26,7 @@
"devster/ubench": "^2.1",
"phpstan/phpstan": "^1.11",
"phpstan/phpstan-phpunit": "^1.4",
"phpunit/phpunit": "^11.1",
"phpunit/phpunit": "^11.2",
"roave/security-advisories": "dev-master",
"shlinkio/php-coding-standard": "~2.3.0",
"symfony/var-dumper": "^7.1"
Expand Down
22 changes: 21 additions & 1 deletion src/Config/ConfigOptionsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,31 @@
namespace Shlinkio\Shlink\Installer\Config;

use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\Exception\InvalidServiceException;

use function get_debug_type;
use function sprintf;

/**
* @extends AbstractPluginManager<Option\ConfigOptionInterface>
* @extends AbstractPluginManager<Option\ConfigOptionInterface>+
* @todo Extend from AbstractSingleInstancePluginManager once servicemanager 3 is no longer supported
*/
class ConfigOptionsManager extends AbstractPluginManager implements ConfigOptionsManagerInterface
{
/** @var class-string<Option\ConfigOptionInterface> */
protected $instanceOf = Option\ConfigOptionInterface::class; // phpcs:ignore

public function validate(mixed $instance): void
{
if ($instance instanceof $this->instanceOf) {
return;
}

throw new InvalidServiceException(sprintf(
'Plugin manager "%s" expected an instance of type "%s", but "%s" was received',
static::class,
$this->instanceOf,
get_debug_type($instance),
));
}
}
18 changes: 8 additions & 10 deletions test/Config/ConfigOptionsManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use ReflectionObject;
use Shlinkio\Shlink\Installer\Config\ConfigOptionsManagerFactory;
use Shlinkio\Shlink\Installer\Config\Option\ConfigOptionInterface;

Expand All @@ -25,23 +24,22 @@ public function setUp(): void
}

#[Test, DataProvider('provideConfigs')]
public function createsServiceWithExpectedPlugins(callable $configCreator, int $expectedSize): void
public function createsServiceWithExpectedPlugins(callable $configCreator, bool $servicesExist): void
{
$config = $configCreator($this);
$this->container->expects($this->once())->method('get')->with('config')->willReturn($config);

$service = ($this->factory)($this->container);
$ref = new ReflectionObject($service);
$servicesProp = $ref->getProperty('services');
$servicesProp->setAccessible(true);
$manager = ($this->factory)($this->container);

self::assertCount($expectedSize, $servicesProp->getValue($service));
self::assertEquals($servicesExist, $manager->has('a'));
self::assertEquals($servicesExist, $manager->has('b'));
self::assertEquals($servicesExist, $manager->has('c'));
}

public static function provideConfigs(): iterable
{
yield 'config_options not defined' => [static fn (TestCase $test) => [], 0];
yield 'config_options empty' => [static fn (TestCase $test) => ['config_options' => []], 0];
yield 'config_options not defined' => [static fn (TestCase $test) => [], false];
yield 'config_options empty' => [static fn (TestCase $test) => ['config_options' => []], false];
yield 'config_options with values' => [
static fn (TestCase $test) => [
'config_options' => [
Expand All @@ -52,7 +50,7 @@ public static function provideConfigs(): iterable
],
],
],
3,
true,
];
}
}

0 comments on commit 6a8e2d9

Please sign in to comment.