Skip to content

Commit c1e6c57

Browse files
committed
lazy singleton
1 parent e7f48eb commit c1e6c57

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

tests/Container/ContainerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,31 @@ public function testLazyObjectWithLazyDependency()
983983
$this->assertTrue(ConstructionNotices::$constructed[ContainerDependentStub::class]);
984984
}
985985

986+
public function testLazyObjectAsSingleton()
987+
{
988+
if (version_compare(phpversion(), '8.4.0', '<')) {
989+
$this->markTestSkipped('Lazy objects are only available in 8.4 and later');
990+
}
991+
992+
ConstructionNotices::reset();
993+
994+
$container = new Container;
995+
$container->singleton(LazyClassWithLazyDependency::class);
996+
$class = $container->make(LazyClassWithLazyDependency::class);
997+
998+
$this->assertInstanceOf(LazyClassWithLazyDependency::class, $class);
999+
$this->assertCount(0, ConstructionNotices::$constructed);
1000+
$class2 = $container->make(LazyClassWithLazyDependency::class);
1001+
$this->assertCount(0, ConstructionNotices::$constructed);
1002+
$this->assertSame($class, $class2);
1003+
1004+
$class->setValue('hello');
1005+
$this->assertCount(2, ConstructionNotices::$constructed);
1006+
$this->assertTrue(ConstructionNotices::$constructed[ClassWithLazyDependencies::class]);
1007+
$this->assertTrue(ConstructionNotices::$constructed[LazyClassWithLazyDependency::class]);
1008+
$this->assertEquals('hello', $class2->value);
1009+
}
1010+
9861011
// public function testContainerCanCatchCircularDependency()
9871012
// {
9881013
// $this->expectException(\Illuminate\Contracts\Container\CircularDependencyException::class);

0 commit comments

Comments
 (0)