ClientFactoryTest.php in MongoDB 8.2
File
modules/mongodb/tests/src/Kernel/ClientFactoryTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\mongodb\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\mongodb\ClientFactory;
use MongoDB\Driver\Exception\ConnectionTimeoutException;
class ClientFactoryTest extends MongoDbTestBase {
public function testGetHappy() {
$clientFactory = new ClientFactory($this->settings);
try {
$client = $clientFactory
->get(static::CLIENT_TEST_ALIAS);
$client
->listDatabases();
} catch (ConnectionTimeoutException $e) {
$fail = new FormattableMarkup('Could not connect to server on @uri. Enable one on @default or specify one in MONGODB_URI.', [
'@default' => static::DEFAULT_URI,
'@uri' => $this->uri,
]);
$this
->fail("{$fail}");
} catch (\Exception $e) {
$this
->fail($e
->getMessage());
}
$this
->assertNotNull($clientFactory, "clientFactory must not be null");
$this
->assertEquals(ClientFactory::class, get_class($clientFactory));
}
public function testGetSadBadAlias() {
$clientFactory = new ClientFactory($this->settings);
try {
$client = $clientFactory
->get(static::CLIENT_BAD_ALIAS);
$client
->listDatabases();
$this
->fail('Should not have been able to connect to a non-server.');
} catch (ConnectionTimeoutException $e) {
$this
->assertTrue(TRUE, 'Cannot create a client to a non-server.');
} catch (\Exception $e) {
$this
->fail($e
->getMessage());
}
}
}