From ed56555f5cc34130629db03d923ebb0e46c55ec3 Mon Sep 17 00:00:00 2001 From: Matthew Setter Date: Fri, 13 Dec 2024 21:38:33 +1000 Subject: [PATCH] Remove duplicate delgators for all services mergeDelegators' documentation says that it merges delegators avoiding multiple same delegators for the same service. However, from experimenting with it, duplicates were allowed, as best as I could tell. So, I've created this small patch to ensure that duplicates are removed, along with a covering test for it to verify the change. Signed-off-by: Matthew Setter --- src/ServiceManager.php | 3 +-- test/ServiceManagerTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 466702a8..ada28605 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -705,8 +705,7 @@ private function mergeDelegators(array $config): array { foreach ($config as $key => $delegators) { if (! array_key_exists($key, $this->delegators)) { - $this->delegators[$key] = $delegators; - continue; + $this->delegators[$key] = []; } foreach ($delegators as $delegator) { diff --git a/test/ServiceManagerTest.php b/test/ServiceManagerTest.php index 06fb2ad3..3fd452e7 100644 --- a/test/ServiceManagerTest.php +++ b/test/ServiceManagerTest.php @@ -5,6 +5,7 @@ namespace LaminasTest\ServiceManager; use DateTime; +use Laminas\ContainerConfigTest\TestAsset\DelegatorFactory; use Laminas\ServiceManager\Factory\AbstractFactoryInterface; use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\ServiceManager\Factory\InvokableFactory; @@ -372,6 +373,30 @@ public function testFactoryMayBeStaticMethodDescribedByCallableString(): void self::assertEquals(stdClass::class, $serviceManager->get(stdClass::class)::class); } + public function testDuplicateDelegatorsAreRemoved(): void + { + $dependencies = [ + 'delegators' => [ + DateTime::class => [ + DelegatorFactory::class, + DelegatorFactory::class, + ], + ], + ]; + $serviceManager = new ServiceManager($dependencies); + $property = new ReflectionProperty(ServiceManager::class, "delegators"); + $property->setAccessible(true); + $delegators = $property->getValue($serviceManager); + self::assertSame( + [ + DateTime::class => [ + DelegatorFactory::class, + ], + ], + $delegators + ); + } + public function testResolvedAliasFromAbstractFactory(): void { $abstractFactory = $this->createMock(AbstractFactoryInterface::class);