Completed
Pull Request — 8.x-2.x (#27)
by Frédéric G.
07:33
created

ClientFactoryTest::testGetSadBadAlias()   A

Complexity

Conditions 3
Paths 7

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 7
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\mongodb\Tests\Kernel;
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
    $clientFactory = new ClientFactory($this->settings);
21
22
    try {
23
      $client = $clientFactory->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
    $clientFactory = new ClientFactory($this->settings);
43
44
    try {
45
      $client = $clientFactory->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