diff --git a/src/Proxy/LazyServiceFactory.php b/src/Proxy/LazyServiceFactory.php index 56ccfe4a..438bbf6a 100644 --- a/src/Proxy/LazyServiceFactory.php +++ b/src/Proxy/LazyServiceFactory.php @@ -42,8 +42,8 @@ public function __invoke( ): VirtualProxyInterface { if (isset($this->servicesMap[$name])) { $initializer = static function (&$wrappedInstance, LazyLoadingInterface $proxy) use ($callback): bool { - $proxy->setProxyInitializer(null); $wrappedInstance = $callback(); + $proxy->setProxyInitializer(null); return true; }; diff --git a/test/Proxy/LazyServiceFactoryTest.php b/test/Proxy/LazyServiceFactoryTest.php index 582f135f..7553285e 100644 --- a/test/Proxy/LazyServiceFactoryTest.php +++ b/test/Proxy/LazyServiceFactoryTest.php @@ -15,6 +15,7 @@ use ProxyManager\Proxy\LazyLoadingInterface; use ProxyManager\Proxy\VirtualProxyInterface; use Psr\Container\ContainerInterface; +use RuntimeException; #[CoversClass(LazyServiceFactory::class)] final class LazyServiceFactoryTest extends TestCase @@ -94,4 +95,35 @@ static function ($className, $initializer) use ($expectedService, $proxy): MockO self::assertSame($expectedService, $result, 'service created not match the expected'); } + + public function testDoesNotResetInitializerWhenCallbackThrowsException(): void + { + $exception = new RuntimeException('Test exception'); + $callback = function () use ($exception): void { + throw $exception; + }; + + $proxy = $this->createMock(LazyLoadingInterface::class); + $proxy + ->expects(self::never()) + ->method('setProxyInitializer'); + + $expectedService = $this->createMock(VirtualProxyInterface::class); + + $this->proxyFactory + ->expects(self::once()) + ->method('createProxy') + ->willReturnCallback( + static function (string $className, callable $initializer) use ($expectedService, $proxy): MockObject { + $wrappedInstance = null; + $initializer($wrappedInstance, $proxy); + return $expectedService; + } + ); + + $this->expectExceptionObject($exception); + $result = $this->factory->__invoke($this->container, 'fooService', $callback); + + self::assertSame($expectedService, $result); + } }