Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix LazyServiceFactory to handle exception in constructor #249

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
idsulik marked this conversation as resolved.
Show resolved Hide resolved
}

return true;
};
Expand Down
49 changes: 49 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,52 @@ static function ($className, $initializer) use ($expectedService, $proxy): MockO

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

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

$proxy = $this->createMock(LazyLoadingInterface::class);
$initializer = static 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(
/** @psalm-suppress UnusedVariable */
static function (string $className, callable $initializer) use ($expectedService, $proxy): MockObject {
self::assertEquals('FooClass', $className);

try {
$wrappedInstance = null;
$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);
}
}
Loading