MongoDbTestBase.php in MongoDB 8.2
File
modules/mongodb/tests/src/Kernel/MongoDbTestBase.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\mongodb\Kernel;
use Drupal\Core\Site\Settings;
use Drupal\KernelTests\KernelTestBase;
use Drupal\mongodb\ClientFactory;
use Drupal\mongodb\DatabaseFactory;
use Drupal\mongodb\MongoDb;
abstract class MongoDbTestBase extends KernelTestBase {
const DEFAULT_URI = 'mongodb://localhost:27017';
const CLIENT_BAD_ALIAS = 'bad';
const CLIENT_TEST_ALIAS = 'test';
const DB_BAD_CLIENT_ALIAS = 'bad';
const DB_INVALID_ALIAS = 'invalid';
const DB_DEFAULT_ALIAS = 'default';
const DB_UNSET_ALIAS = 'unset';
protected static $modules = [
MongoDb::MODULE,
];
protected $settings;
protected $uri;
public function getTestDatabaseName($postfix) {
return $this
->getDatabasePrefix() . '_' . $postfix;
}
protected function getSettingsArray() : array {
return [
'clients' => [
static::CLIENT_BAD_ALIAS => [
'uri' => 'mongodb://localhost:80',
'uriOptions' => [],
'driverOptions' => [],
],
static::CLIENT_TEST_ALIAS => [
'uri' => $this->uri,
'uriOptions' => [],
'driverOptions' => [],
],
],
'databases' => [
static::DB_DEFAULT_ALIAS => [
static::CLIENT_TEST_ALIAS,
$this
->getDatabasePrefix(),
],
static::DB_INVALID_ALIAS => [
static::CLIENT_TEST_ALIAS,
'',
],
static::DB_BAD_CLIENT_ALIAS => [
static::CLIENT_BAD_ALIAS,
$this
->getDatabasePrefix(),
],
],
];
}
public function setUp() : void {
parent::setUp();
$this->uri = $_ENV['MONGODB_URI'] ?? $_SERVER['MONGODB_URI'] ?? static::DEFAULT_URI;
$this->settings = new Settings([
MongoDb::MODULE => $this
->getSettingsArray(),
]);
}
public function tearDown() : void {
$clientFactory = new ClientFactory($this->settings);
$databaseFactory = new DatabaseFactory($clientFactory, $this->settings);
$databaseFactory
->get(static::DB_DEFAULT_ALIAS)
->drop();
parent::tearDown();
}
}
Classes
Name |
Description |
MongoDbTestBase |
Class MongoDbTestBase provides basic setUp()/tearDown() for MongoDB. |