@@ -3098,9 +3098,37 @@ public function testStringAndDateWithTTL(): void
30983098 $ this ->assertEquals ('another_random_string_xyz ' , $ expiresAt4 );
30993099 $ this ->assertTrue (is_string ($ expiresAt4 ));
31003100
3101+ // Wait for the MongoDB TTL monitor to physically delete the expired datetime document.
3102+ $ maxRetries = 25 ;
3103+ $ retryDelay = 5 ;
3104+ $ expiredDocDeleted = false ;
3105+
3106+ for ($ i = 0 ; $ i < $ maxRetries ; $ i ++) {
3107+ sleep ($ retryDelay );
3108+
3109+ // Fetch collection to trigger TTL cleanup check
3110+ $ collection = $ database ->getCollection ($ col );
3111+ $ this ->assertNotNull ($ collection );
3112+
3113+ $ remainingDocs = $ database ->find ($ col );
3114+ $ remainingIds = array_map (fn ($ doc ) => $ doc ->getId (), $ remainingDocs );
3115+
3116+ if (!in_array ('doc_datetime_expired ' , $ remainingIds )) {
3117+ $ expiredDocDeleted = true ;
3118+ break ;
3119+ }
3120+ }
3121+
3122+ // Assert that expired datetime document was eventually deleted by TTL monitor
3123+ $ this ->assertTrue ($ expiredDocDeleted , 'Expired datetime document should have been deleted after TTL expiry ' );
3124+
3125+ // After expiry, re-check remaining documents
31013126 $ remainingDocs = $ database ->find ($ col );
31023127 $ remainingIds = array_map (fn ($ doc ) => $ doc ->getId (), $ remainingDocs );
31033128
3129+ // The expired datetime document should be deleted from the collection
3130+ $ this ->assertNotContains ('doc_datetime_expired ' , $ remainingIds );
3131+
31043132 // Documents with random strings should still exist (TTL doesn't affect non-datetime values)
31053133 $ this ->assertContains ('doc_string_random ' , $ remainingIds );
31063134 $ this ->assertContains ('doc_string_another ' , $ remainingIds );
0 commit comments