ClientFactoryTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 44
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetSadBadAlias() 0 14 3
A testGetHappy() 0 20 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\Tests\mongodb\Kernel;
6
7
use Drupal\Component\Render\FormattableMarkup;
0 ignored issues
show
Bug introduced by
The type Drupal\Component\Render\FormattableMarkup was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Drupal\mongodb\ClientFactory;
9
use MongoDB\Driver\Exception\ConnectionTimeoutException;
10
11
/**
12
 * Tests the ClientFactory.
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(): void {
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(): void {
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