1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Drupal\Tests\mongodb\Kernel; |
6
|
|
|
|
7
|
|
|
use Drupal\Component\Render\FormattableMarkup; |
8
|
|
|
use Drupal\mongodb\ClientFactory; |
9
|
|
|
use MongoDB\Driver\Exception\ConnectionTimeoutException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ClientFactoryTest. |
13
|
|
|
* |
14
|
|
|
* @coversDefaultClass \Drupal\mongodb\ClientFactory |
15
|
|
|
* |
16
|
|
|
* @group MongoDB |
17
|
|
|
*/ |
18
|
|
|
class ClientFactoryTest extends MongoDbTestBase { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Test a normal client creation attempt. |
22
|
|
|
*/ |
23
|
|
|
public function testGetHappy() { |
24
|
|
|
$clientFactory = new ClientFactory($this->settings); |
25
|
|
|
|
26
|
|
|
try { |
27
|
|
|
$client = $clientFactory->get(static::CLIENT_TEST_ALIAS); |
28
|
|
|
// Force connection attempt by executing a command. |
29
|
|
|
$client->listDatabases(); |
30
|
|
|
} |
31
|
|
|
catch (ConnectionTimeoutException $e) { |
32
|
|
|
$fail = new FormattableMarkup('Could not connect to server on @uri. Enable one on @default or specify one in MONGODB_URI.', [ |
33
|
|
|
'@default' => static::DEFAULT_URI, |
34
|
|
|
'@uri' => $this->uri, |
35
|
|
|
]); |
36
|
|
|
$this->fail("$fail"); |
37
|
|
|
} |
38
|
|
|
catch (\Exception $e) { |
39
|
|
|
$this->fail($e->getMessage()); |
40
|
|
|
} |
41
|
|
|
$this->assertNotNull($clientFactory, "clientFactory must not be null"); |
42
|
|
|
$this->assertEquals(ClientFactory::class, get_class($clientFactory)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Test an existing alias pointing to an invalid server. |
47
|
|
|
*/ |
48
|
|
|
public function testGetSadBadAlias() { |
49
|
|
|
$clientFactory = new ClientFactory($this->settings); |
50
|
|
|
|
51
|
|
|
try { |
52
|
|
|
$client = $clientFactory->get(static::CLIENT_BAD_ALIAS); |
53
|
|
|
// Force connection attempt by executing a command. |
54
|
|
|
$client->listDatabases(); |
55
|
|
|
$this->fail('Should not have been able to connect to a non-server.'); |
56
|
|
|
} |
57
|
|
|
catch (ConnectionTimeoutException $e) { |
58
|
|
|
$this->assertTrue(TRUE, 'Cannot create a client to a non-server.'); |
59
|
|
|
} |
60
|
|
|
catch (\Exception $e) { |
61
|
|
|
$this->fail($e->getMessage()); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|