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