Completed
Push — 8.x-2.x ( 4aaf30...f7089a )
by Frédéric G.
02:40
created

DatabaseFactoryTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
75