55namespace Doctrine \Tests \ORM \Functional ;
66
77use Doctrine \ORM \PersistentCollection ;
8+ use Doctrine \Tests \Models \CMS \CmsUser ;
89use Doctrine \Tests \Models \ECommerce \ECommerceCart ;
910use Doctrine \Tests \Models \ECommerce \ECommerceCustomer ;
1011use Doctrine \Tests \Models \ECommerce \ECommerceFeature ;
1112use Doctrine \Tests \Models \ECommerce \ECommerceProduct ;
1213use Doctrine \Tests \OrmFunctionalTestCase ;
14+ use ReflectionClass ;
15+
16+ use const PHP_VERSION_ID ;
1317
1418/**
1519 * Tests capabilities of the persister.
@@ -19,6 +23,7 @@ class StandardEntityPersisterTest extends OrmFunctionalTestCase
1923 protected function setUp (): void
2024 {
2125 $ this ->useModelSet ('ecommerce ' );
26+ $ this ->useModelSet ('cms ' );
2227
2328 parent ::setUp ();
2429 }
@@ -107,4 +112,29 @@ public function testAddPersistRetrieve(): void
107112 // Persisted Product now must have 3 Feature items
108113 self ::assertCount (3 , $ res [0 ]->getFeatures ());
109114 }
115+
116+ public function testPersistLazyGhost (): void
117+ {
118+ if (PHP_VERSION_ID < 80400 ) {
119+ $ this ->markTestSkipped ('Lazy objects are only available in PHP 8.4+. ' );
120+ }
121+
122+ $ initialized = false ;
123+ $ reflector = new ReflectionClass (CmsUser::class);
124+ $ lazyGhost = $ reflector ->newLazyGhost (static function (CmsUser $ object ) use (&$ initialized ): void {
125+ $ initialized = true ;
126+ $ object ->username = 'lazyGhost ' ;
127+ $ object ->name = 'LazyGhostInitialized ' ;
128+ $ object ->status = 'active ' ;
129+ });
130+
131+ self ::assertFalse ($ initialized , 'Lazy ghost should not be initialized before persist. ' );
132+ $ this ->_em ->persist ($ lazyGhost );
133+ $ this ->_em ->flush ();
134+ self ::assertTrue ($ initialized , 'Lazy ghost should be initialized during flush. ' );
135+ $ this ->_em ->clear ();
136+ $ retrievedEntity = $ this ->_em ->find (CmsUser::class, $ lazyGhost ->id );
137+ self ::assertNotNull ($ retrievedEntity );
138+ self ::assertEquals ('LazyGhostInitialized ' , $ retrievedEntity ->name );
139+ }
110140}
0 commit comments