1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\mongodb\Tests; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\Render\FormattableMarkup; |
6
|
|
|
use Drupal\Core\Site\Settings; |
7
|
|
|
use Drupal\KernelTests\KernelTestBase; |
8
|
|
|
use Drupal\mongodb\ClientFactory; |
9
|
|
|
use MongoDB\Driver\Exception\ConnectionTimeoutException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ClientFactoryTest. |
13
|
|
|
* |
14
|
|
|
* @group MongoDB |
15
|
|
|
*/ |
16
|
|
|
class ClientFactoryTest extends MongoDbTestBase { |
17
|
|
|
/** |
18
|
|
|
* Test a normal client creation attempt. |
19
|
|
|
*/ |
20
|
|
|
public function testGetHappy() { |
21
|
|
|
$cf = new ClientFactory($this->settings); |
|
|
|
|
22
|
|
|
|
23
|
|
|
try { |
24
|
|
|
$client = $cf->get(static::CLIENT_TEST_ALIAS); |
25
|
|
|
// Force connection attempt by executing a command. |
26
|
|
|
$client->listDatabases(); |
27
|
|
|
} |
28
|
|
|
catch (ConnectionTimeoutException $e) { |
|
|
|
|
29
|
|
|
$this->fail(new FormattableMarkup("Could not connect to server on @uri. Enable one on @default or specify one in MONGODB_URI.", [ |
|
|
|
|
30
|
|
|
'@default' => static::DEFAULT_URI, |
31
|
|
|
'@uri' => $this->uri, |
32
|
|
|
])); |
33
|
|
|
} |
34
|
|
|
catch (\Exception $e) { |
35
|
|
|
$this->fail($e->getMessage()); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Test an existing alias pointing to an invalid server. |
41
|
|
|
*/ |
42
|
|
|
public function testGetSadBadAlias() { |
43
|
|
|
$cf = new ClientFactory($this->settings); |
|
|
|
|
44
|
|
|
|
45
|
|
|
try { |
46
|
|
|
$client = $cf->get(static::CLIENT_BAD_ALIAS); |
47
|
|
|
// Force connection attempt by executing a command. |
48
|
|
|
$client->listDatabases(); |
49
|
|
|
$this->fail("Should not have been able to connect to a non-server."); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
catch (ConnectionTimeoutException $e) { |
|
|
|
|
52
|
|
|
$this->assertTrue(TRUE, "Cannot create a client to a non-server."); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
catch (\Exception $e) { |
55
|
|
|
$this->fail($e->getMessage()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
} |
|
|
|
|
60
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.