Skip to content

Commit

Permalink
fix: Fix LazyServiceFactory to handle exception in constructor
Browse files Browse the repository at this point in the history
Signed-off-by: Suleiman Dibirov <[email protected]>
  • Loading branch information
idsulik committed Dec 11, 2024
1 parent 340409a commit 0edf3b9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Proxy/LazyServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ProxyManager\Proxy\LazyLoadingInterface;
use ProxyManager\Proxy\VirtualProxyInterface;
use Psr\Container\ContainerInterface;
use Throwable;

use function sprintf;

Expand Down Expand Up @@ -42,8 +43,14 @@ public function __invoke(
): VirtualProxyInterface {
if (isset($this->servicesMap[$name])) {
$initializer = static function (&$wrappedInstance, LazyLoadingInterface $proxy) use ($callback): bool {
$initializer = $proxy->getProxyInitializer();
$proxy->setProxyInitializer(null);
$wrappedInstance = $callback();
try {
$wrappedInstance = $callback();
} catch (Throwable $e) {
$proxy->setProxyInitializer($initializer);
throw $e;
}

return true;
};
Expand Down
47 changes: 47 additions & 0 deletions test/Proxy/LazyServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -94,4 +95,50 @@ static function ($className, $initializer) use ($expectedService, $proxy): MockO

self::assertSame($expectedService, $result, 'service created not match the expected');
}

public function testHandlesExceptionInInitializer(): void
{
$exception = new RuntimeException('Test exception');
$callback = fn(): never => throw $exception;

$proxy = $this->createMock(LazyLoadingInterface::class);
$initializer = fn(): bool => true;

$proxy->expects(self::once())
->method('getProxyInitializer')
->willReturn($initializer);

$proxy
->expects(self::exactly(2))
->method('setProxyInitializer')
->willReturnMap(
[
[null],
[$initializer],
]
);

$expectedService = $this->createMock(VirtualProxyInterface::class);

$this->proxyFactory
->expects(self::once())
->method('createProxy')
->willReturnCallback(
static function (string $className, callable $initializer) use ($expectedService, $proxy): MockObject {
self::assertEquals('FooClass', $className);
$wrappedInstance = null;

Check failure on line 129 in test/Proxy/LazyServiceFactoryTest.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

UnusedVariable

test/Proxy/LazyServiceFactoryTest.php:129:21: UnusedVariable: $wrappedInstance is never referenced or the value is not used (see https://psalm.dev/077)

try {
$initializer($wrappedInstance, $proxy);
self::fail('Expected exception was not thrown');
} catch (RuntimeException) {
}

return $expectedService;
}
);

$result = $this->factory->__invoke($this->container, 'fooService', $callback);
self::assertSame($expectedService, $result);
}
}

0 comments on commit 0edf3b9

Please sign in to comment.