1717use function collect ;
1818use function count ;
1919use function sprintf ;
20+ use function str_starts_with ;
2021
2122class SchemaTest extends TestCase
2223{
2324 public function tearDown (): void
2425 {
25- $ database = $ this ->getConnection ('mongodb ' )->getMongoDB ();
26+ $ database = $ this ->getConnection ('mongodb ' )->getDatabase ();
2627 assert ($ database instanceof Database);
2728 $ database ->dropCollection ('newcollection ' );
2829 $ database ->dropCollection ('newcollection_two ' );
30+ $ database ->dropCollection ('test_view ' );
2931
3032 parent ::tearDown ();
3133 }
@@ -397,6 +399,13 @@ public function testGetTables()
397399 DB ::connection ('mongodb ' )->table ('newcollection_two ' )->insert (['test ' => 'value ' ]);
398400 $ dbName = DB ::connection ('mongodb ' )->getDatabaseName ();
399401
402+ // Create a view (this creates system.views)
403+ DB ::connection ('mongodb ' )->getDatabase ()->command ([
404+ 'create ' => 'test_view ' ,
405+ 'viewOn ' => 'newcollection ' ,
406+ 'pipeline ' => [],
407+ ]);
408+
400409 $ tables = Schema::getTables ();
401410 $ this ->assertIsArray ($ tables );
402411 $ this ->assertGreaterThanOrEqual (2 , count ($ tables ));
@@ -413,6 +422,9 @@ public function testGetTables()
413422 $ this ->assertEquals ($ dbName . '.newcollection ' , $ table ['schema_qualified_name ' ]);
414423 $ found = true ;
415424 }
425+
426+ // Ensure system collections are excluded
427+ $ this ->assertFalse (str_starts_with ($ table ['name ' ], 'system. ' ));
416428 }
417429
418430 if (! $ found ) {
@@ -425,12 +437,22 @@ public function testGetTableListing()
425437 DB ::connection ('mongodb ' )->table ('newcollection ' )->insert (['test ' => 'value ' ]);
426438 DB ::connection ('mongodb ' )->table ('newcollection_two ' )->insert (['test ' => 'value ' ]);
427439
440+ // Create a view (this creates system.views)
441+ DB ::connection ('mongodb ' )->getDatabase ()->command ([
442+ 'create ' => 'test_view ' ,
443+ 'viewOn ' => 'newcollection ' ,
444+ 'pipeline ' => [],
445+ ]);
446+
428447 $ tables = Schema::getTableListing ();
429448
430449 $ this ->assertIsArray ($ tables );
431450 $ this ->assertGreaterThanOrEqual (2 , count ($ tables ));
432451 $ this ->assertContains ('newcollection ' , $ tables );
433452 $ this ->assertContains ('newcollection_two ' , $ tables );
453+
454+ // Ensure system collections are excluded
455+ $ this ->assertNotContains ('system.views ' , $ tables );
434456 }
435457
436458 public function testGetTableListingBySchema ()
@@ -453,6 +475,58 @@ public function testGetTableListingBySchema()
453475 $ this ->assertContains ('newcollection ' , $ tables );
454476 $ this ->assertContains ('newcollection_two ' , $ tables );
455477 }
478+ // Protected method cannot test
479+ // public function testGetAllCollections()
480+ // {
481+ // // Insert test data into normal collections
482+ // DB::connection('mongodb')->table('newcollection')->insert(['test' => 'value']);
483+ // DB::connection('mongodb')->table('newcollection_two')->insert(['test' => 'value']);
484+
485+ // // Create a view (this creates system.views)
486+ // DB::connection('mongodb')->getDatabase()->command([
487+ // 'create' => 'test_view',
488+ // 'viewOn' => 'newcollection',
489+ // 'pipeline' => [],
490+ // ]);
491+
492+ // $collections = Schema::getAllCollections();
493+
494+ // $this->assertIsArray($collections);
495+ // $this->assertGreaterThanOrEqual(2, count($collections));
496+
497+ // // Ensure normal collections are present
498+ // $this->assertContains('newcollection', $collections);
499+ // $this->assertContains('newcollection_two', $collections);
500+
501+ // // Ensure system collections are excluded
502+ // $this->assertNotContains('system.views', $collections);
503+ // }
504+
505+ public function testSystemCollectionsArePresentButFiltered ()
506+ {
507+ // Create a view to trigger system.views collection
508+ DB ::connection ('mongodb ' )->getDatabase ()->command ([
509+ 'create ' => 'test_view ' ,
510+ 'viewOn ' => 'newcollection ' ,
511+ 'pipeline ' => [],
512+ ]);
513+
514+ // Get all collections directly from MongoDB
515+ $ allCollections = DB ::connection ('mongodb ' )->getDatabase ()->listCollectionNames ();
516+
517+ // Ensure the system.views collection exists in MongoDB
518+ $ this ->assertContains ('system.views ' , $ allCollections );
519+
520+ // Ensure Schema::getTables does NOT include system collections
521+ $ tables = Schema::getTables ();
522+ foreach ($ tables as $ table ) {
523+ $ this ->assertFalse (str_starts_with ($ table ['name ' ], 'system. ' ));
524+ }
525+
526+ // Ensure Schema::getTableListing does NOT include system collections
527+ $ tableListing = Schema::getTableListing ();
528+ $ this ->assertNotContains ('system.views ' , $ tableListing );
529+ }
456530
457531 public function testGetColumns ()
458532 {
0 commit comments