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 KernelTestBase { |
17
|
|
|
const DEFAULT_URI = 'mongodb://localhost:27017'; |
18
|
|
|
const BAD_ALIAS = 'bad'; |
19
|
|
|
const TEST_ALIAS = 'test'; |
20
|
|
|
|
21
|
|
|
public static $modules = ['mongodb']; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A test-specific instance of Settings. |
25
|
|
|
* |
26
|
|
|
* @var \Drupal\Core\Site\Settings |
27
|
|
|
*/ |
28
|
|
|
protected $settings; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
protected $uri; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function setUp() { |
37
|
|
|
parent::setUp(); |
38
|
|
|
$this->uri = $_SERVER['MONGODB_URI'] ?? static::DEFAUlT_URI; |
|
|
|
|
39
|
|
|
|
40
|
|
|
$this->settings = new Settings([ |
41
|
|
|
'mongodb' => [ |
42
|
|
|
'clients' => [ |
43
|
|
|
static::BAD_ALIAS => [ |
44
|
|
|
'uri' => 'mongodb://localhost:80', |
45
|
|
|
'uriOptions' => [], |
46
|
|
|
'driverOptions' => [], |
47
|
|
|
], |
48
|
|
|
static::TEST_ALIAS => [ |
49
|
|
|
'uri' => $this->uri, |
50
|
|
|
'uriOptions' => [], |
51
|
|
|
'driverOptions' => [], |
52
|
|
|
], |
53
|
|
|
], |
54
|
|
|
'databases' => [ |
55
|
|
|
'default' => [static::TEST_ALIAS, $this->getDatabasePrefix()], |
56
|
|
|
], |
57
|
|
|
], |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Test a normal client creation attempt. |
63
|
|
|
*/ |
64
|
|
|
public function testGetHappy() { |
65
|
|
|
$cf = new ClientFactory($this->settings); |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$client = $cf->get(static::TEST_ALIAS); |
69
|
|
|
// Force connection attempt by executing a command. |
70
|
|
|
$client->listDatabases(); |
71
|
|
|
} |
72
|
|
|
catch (ConnectionTimeoutException $e) { |
|
|
|
|
73
|
|
|
$this->fail(new FormattableMarkup("Could not connect to server on @uri. Enable one on @default or specify one in MONGODB_URI.", [ |
|
|
|
|
74
|
|
|
'@default' => static::DEFAUlT_URI, |
75
|
|
|
'@uri' => $this->uri, |
76
|
|
|
])); |
77
|
|
|
} |
78
|
|
|
catch (\Exception $e) { |
79
|
|
|
$this->fail($e->getMessage()); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Test an existing alias pointing to an invalid server. |
85
|
|
|
*/ |
86
|
|
|
public function testGetSadBadAlias() { |
87
|
|
|
$cf = new ClientFactory($this->settings); |
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
$client = $cf->get(static::BAD_ALIAS); |
91
|
|
|
// Force connection attempt by executing a command. |
92
|
|
|
$client->listDatabases(); |
93
|
|
|
$this->fail("Should not have been able to connect to a non-server."); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
catch (ConnectionTimeoutException $e) { |
|
|
|
|
96
|
|
|
$this->pass("Cannot create a client to a non-server."); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
catch (\Exception $e) { |
99
|
|
|
$this->fail($e->getMessage()); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
|
|
|
|
104
|
|
|
|