You are here

public function MongoDbTest::testCountCollection in MongoDB 8.2

@covers ::countCollection

File

modules/mongodb/tests/src/Kernel/MongoDbTest.php, line 53

Class

MongoDbTest
Tests the MongoDB main class.

Namespace

Drupal\Tests\mongodb\Kernel

Code

public function testCountCollection() {

  /** @var \Drupal\mongodb\DatabaseFactory $dbFactory */
  $dbFactory = $this->container
    ->get(MongoDb::SERVICE_DB_FACTORY);
  $database = $dbFactory
    ->get(MongoDb::DB_DEFAULT);
  $collectionName = $this
    ->getDatabasePrefix() . $this
    ->randomMachineName();
  $collection = $database
    ->selectCollection($collectionName);
  $collection
    ->drop();
  $expected = mt_rand(0, 100);
  $docs = [];
  for ($i = 0; $i < $expected; $i++) {
    $docs[] = [
      "index" => $i,
    ];
  }
  $collection
    ->insertMany($docs);
  $actual = MongoDb::countCollection($collection);
  $this
    ->assertEquals($expected, $actual, "countCollection finds the correct number of documents");
}