diff --git a/tests/Unit/ContainerTest.php b/tests/Unit/ContainerTest.php index 5a7672c6..434bf171 100644 --- a/tests/Unit/ContainerTest.php +++ b/tests/Unit/ContainerTest.php @@ -2016,4 +2016,58 @@ public function testNotFoundExceptionWithNotYiiContainer(): void $exception->getPrevious() ); } + + public function testExceptionOnGetInDelegate(): void + { + $container = new Container( + ContainerConfig::create()->withDelegates([ + static fn () => new SimpleContainer( + factory: static fn () => throw new RuntimeException('Error in delegate'), + ), + ]), + ); + + $exception = null; + try { + $container->get('identifier'); + } catch (Throwable $exception) { + } + + $this->assertInstanceOf(BuildingException::class, $exception); + $this->assertSame( + 'Caught unhandled error "Error in delegate" while building "identifier".', + $exception->getMessage(), + ); + + $previous = $exception->getPrevious(); + $this->assertInstanceOf(RuntimeException::class, $previous); + $this->assertSame('Error in delegate', $previous->getMessage()); + } + + public function testExceptionOnHasInDelegate(): void + { + $container = new Container( + ContainerConfig::create()->withDelegates([ + static fn () => new SimpleContainer( + hasCallback: static fn () => throw new RuntimeException('Error in delegate'), + ), + ]), + ); + + $exception = null; + try { + $container->get('identifier'); + } catch (Throwable $exception) { + } + + $this->assertInstanceOf(BuildingException::class, $exception); + $this->assertSame( + 'Caught unhandled error "Error in delegate" while building "identifier".', + $exception->getMessage(), + ); + + $previous = $exception->getPrevious(); + $this->assertInstanceOf(RuntimeException::class, $previous); + $this->assertSame('Error in delegate', $previous->getMessage()); + } }