Passed
Push — 8.x-2.x ( 28cdbd...dac389 )
by Frédéric G.
01:03 queued 11s
created

DatabaseFactoryTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testGetHappy() 0 4 1
A testGetSadUnsetAlias() 0 14 3
A testGetSadAliasForBadDatabase() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Drupal\Tests\mongodb\Kernel;
6
7
use Drupal\mongodb\ClientFactory;
8
use Drupal\mongodb\DatabaseFactory;
9
use Drupal\mongodb\MongoDb;
10
use MongoDB\Database;
11
12
/**
13
 * Class DatabaseFactoryTest.
14
 *
15
 * @coversDefaultClass \Drupal\mongodb\DatabaseFactory
16
 *
17
 * @group MongoDB
18
 */
19
class DatabaseFactoryTest extends MongoDbTestBase {
20
21
  public static $modules = [MongoDb::MODULE];
22
23
  /**
24
   * The mongodb.client_factory service.
25
   *
26
   * @var \Drupal\mongodb\ClientFactory
27
   */
28
  protected $clientFactory;
29
30
  /**
31
   * The mongodb.database_factory service.
32
   *
33
   * @var \Drupal\mongodb\DatabaseFactory
34
   */
35
  protected $databaseFactory;
36
37
  /**
38
   * {@inheritdoc}
39
   */
40
  public function setUp() {
41
    parent::setUp();
42
    $this->clientFactory = new ClientFactory($this->settings);
43
    $this->databaseFactory = new DatabaseFactory($this->clientFactory, $this->settings);
44
  }
45
46
  /**
47
   * Test normal case.
48
   */
49
  public function testGetHappy() {
50
    $drupal = $this->databaseFactory->get(static::DB_DEFAULT_ALIAS);
51
    $this->assertInstanceOf(Database::class, $drupal, 'get() returns a valid database instance.');
52
  }
53
54
  /**
55
   * Test referencing an alias not present in settings.
56
   */
57
  public function testGetSadUnsetAlias() {
58
    try {
59
      $this->databaseFactory->get(static::DB_UNSET_ALIAS);
60
      $this->fail('Should not have returned a value for an unset database alias.');
61
    }
62
    catch (\InvalidArgumentException $e) {
63
      $this->assertTrue(TRUE, 'Throws expected exception for unset database alias.');
64
    }
65
    catch (\Exception $e) {
66
      $this->fail(strtr('Unexpected exception thrown for unset alias: @exception', [
67
        '@exception' => $e->getMessage(),
68
      ]));
69
    }
70
  }
71
72
  /**
73
   * Test referencing an alias pointing to an ill-formed (empty) database name.
74
   */
75
  public function testGetSadAliasForBadDatabase() {
76
    $database = $this->databaseFactory->get(static::DB_INVALID_ALIAS);
77
    $this->assertNull($database, 'Selecting an invalid alias returns a null database.');
78
  }
79
80
}
81