From ed56555f5cc34130629db03d923ebb0e46c55ec3 Mon Sep 17 00:00:00 2001 From: Matthew Setter Date: Fri, 13 Dec 2024 21:38:33 +1000 Subject: [PATCH 1/2] 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); From d734c7d019f42afa48f1f3cf55978dac5a4f89cb Mon Sep 17 00:00:00 2001 From: Matthew Setter Date: Sat, 14 Dec 2024 12:03:08 +1000 Subject: [PATCH 2/2] Remove unused call to ReflectionProperty::setAccessible Signed-off-by: Matthew Setter --- test/ServiceManagerTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/ServiceManagerTest.php b/test/ServiceManagerTest.php index 3fd452e7..585dade2 100644 --- a/test/ServiceManagerTest.php +++ b/test/ServiceManagerTest.php @@ -385,8 +385,7 @@ public function testDuplicateDelegatorsAreRemoved(): void ]; $serviceManager = new ServiceManager($dependencies); $property = new ReflectionProperty(ServiceManager::class, "delegators"); - $property->setAccessible(true); - $delegators = $property->getValue($serviceManager); + $delegators = $property->getValue($serviceManager); self::assertSame( [ DateTime::class => [