ToolsTest.php in MongoDB 8.2
File
modules/mongodb/tests/src/Kernel/ToolsTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\mongodb\Kernel;
use Drupal\mongodb\Install\Tools;
use Drupal\mongodb\MongoDb;
class ToolsTest extends MongoDbTestBase {
public function testToolsService() {
$tools = $this->container
->get(MongoDb::SERVICE_TOOLS);
$this
->assertInstanceOf(Tools::class, $tools, "Tools service is available");
$this
->assertTrue(method_exists($tools, 'find'));
$this
->assertTrue(method_exists($tools, 'settings'));
}
public function testToolsSettings() {
$tools = $this->container
->get(MongoDb::SERVICE_TOOLS);
$actual = $tools
->settings();
$this
->assertIsArray($actual);
$expected = $this
->getSettingsArray();
$this
->assertEquals($expected, $actual);
}
public function testFind() {
$dbFactory = $this->container
->get(MongoDb::SERVICE_DB_FACTORY);
$database = $dbFactory
->get(MongoDb::DB_DEFAULT);
$collectionName = $this
->randomMachineName();
$collection = $database
->selectCollection($collectionName);
$collection
->drop();
$documents = [
[
"foo" => "bar",
],
[
"foo" => "baz",
],
];
$docCount = count($documents);
$collection
->insertMany($documents);
$this
->assertEquals($docCount, $collection
->countDocuments());
$tools = $this->container
->get(MongoDb::SERVICE_TOOLS);
$expectations = [
[
[],
2,
],
[
[
"foo" => "baz",
],
1,
],
[
[
"foo" => "qux",
],
0,
],
];
foreach ($expectations as $expectation) {
list($selector, $count) = $expectation;
$selectorString = json_encode($selector);
$actual = $tools
->find(MongoDb::DB_DEFAULT, $collectionName, $selectorString);
$this
->assertIsArray($actual);
$this
->assertEquals($count, count($actual));
}
}
}