17
17
use function collect ;
18
18
use function count ;
19
19
use function sprintf ;
20
+ use function str_starts_with ;
20
21
21
22
class SchemaTest extends TestCase
22
23
{
23
24
public function tearDown (): void
24
25
{
25
- $ database = $ this ->getConnection ('mongodb ' )->getMongoDB ();
26
+ $ database = $ this ->getConnection ('mongodb ' )->getDatabase ();
26
27
assert ($ database instanceof Database);
27
28
$ database ->dropCollection ('newcollection ' );
28
29
$ database ->dropCollection ('newcollection_two ' );
30
+ $ database ->dropCollection ('test_view ' );
29
31
30
32
parent ::tearDown ();
31
33
}
@@ -397,6 +399,13 @@ public function testGetTables()
397
399
DB ::connection ('mongodb ' )->table ('newcollection_two ' )->insert (['test ' => 'value ' ]);
398
400
$ dbName = DB ::connection ('mongodb ' )->getDatabaseName ();
399
401
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
+
400
409
$ tables = Schema::getTables ();
401
410
$ this ->assertIsArray ($ tables );
402
411
$ this ->assertGreaterThanOrEqual (2 , count ($ tables ));
@@ -413,6 +422,9 @@ public function testGetTables()
413
422
$ this ->assertEquals ($ dbName . '.newcollection ' , $ table ['schema_qualified_name ' ]);
414
423
$ found = true ;
415
424
}
425
+
426
+ // Ensure system collections are excluded
427
+ $ this ->assertFalse (str_starts_with ($ table ['name ' ], 'system. ' ));
416
428
}
417
429
418
430
if (! $ found ) {
@@ -425,12 +437,22 @@ public function testGetTableListing()
425
437
DB ::connection ('mongodb ' )->table ('newcollection ' )->insert (['test ' => 'value ' ]);
426
438
DB ::connection ('mongodb ' )->table ('newcollection_two ' )->insert (['test ' => 'value ' ]);
427
439
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
+
428
447
$ tables = Schema::getTableListing ();
429
448
430
449
$ this ->assertIsArray ($ tables );
431
450
$ this ->assertGreaterThanOrEqual (2 , count ($ tables ));
432
451
$ this ->assertContains ('newcollection ' , $ tables );
433
452
$ this ->assertContains ('newcollection_two ' , $ tables );
453
+
454
+ // Ensure system collections are excluded
455
+ $ this ->assertNotContains ('system.views ' , $ tables );
434
456
}
435
457
436
458
public function testGetTableListingBySchema ()
@@ -453,6 +475,58 @@ public function testGetTableListingBySchema()
453
475
$ this ->assertContains ('newcollection ' , $ tables );
454
476
$ this ->assertContains ('newcollection_two ' , $ tables );
455
477
}
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
+ }
456
530
457
531
public function testGetColumns ()
458
532
{
0 commit comments