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

DatabaseFactoryTest::testGetSadUnsetAlias()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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