Passed
Pull Request — 8.x-2.x (#71)
by Frédéric G.
05:50
created

DatabaseFactoryTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 64
rs 10
wmc 6

4 Methods

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