DatabaseFactoryTest.php in MongoDB 8.2
File
modules/mongodb/tests/src/Kernel/DatabaseFactoryTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\mongodb\Kernel;
use Drupal\mongodb\ClientFactory;
use Drupal\mongodb\DatabaseFactory;
use Drupal\mongodb\MongoDb;
use MongoDB\Database;
class DatabaseFactoryTest extends MongoDbTestBase {
protected static $modules = [
MongoDb::MODULE,
];
protected $clientFactory;
protected $databaseFactory;
public function setUp() : void {
parent::setUp();
$this->clientFactory = new ClientFactory($this->settings);
$this->databaseFactory = new DatabaseFactory($this->clientFactory, $this->settings);
}
public function testGetHappy() {
$drupal = $this->databaseFactory
->get(static::DB_DEFAULT_ALIAS);
$this
->assertInstanceOf(Database::class, $drupal, 'get() returns a valid database instance.');
}
public function testGetSadUnsetAlias() {
try {
$this->databaseFactory
->get(static::DB_UNSET_ALIAS);
$this
->fail('Should not have returned a value for an unset database alias.');
} catch (\InvalidArgumentException $e) {
$this
->assertTrue(TRUE, 'Throws expected exception for unset database alias.');
} catch (\Exception $e) {
$this
->fail(strtr('Unexpected exception thrown for unset alias: @exception', [
'@exception' => $e
->getMessage(),
]));
}
}
public function testGetSadAliasForBadDatabase() {
$database = $this->databaseFactory
->get(static::DB_INVALID_ALIAS);
$this
->assertNull($database, 'Selecting an invalid alias returns a null database.');
}
}