Passed
Pull Request — 8.x-2.x (#58)
by Frédéric G.
03:34
created

MongoDbTestBase   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTestDatabaseName() 0 3 1
A getSettingsArray() 0 21 1
A setUp() 0 10 1
A tearDown() 0 8 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Drupal\Tests\mongodb\Kernel;
6
7
use Drupal\Core\Site\Settings;
8
use Drupal\KernelTests\KernelTestBase;
9
use Drupal\mongodb\ClientFactory;
10
use Drupal\mongodb\DatabaseFactory;
11
use Drupal\mongodb\MongoDb;
12
13
/**
14
 * Class MongoDbTestBase provides basic setUp()/tearDown() for MongoDB.
15
 *
16
 * @group MongoDB
17
 */
18
abstract class MongoDbTestBase extends KernelTestBase {
19
  const DEFAULT_URI = 'mongodb://localhost:27017';
20
  const CLIENT_BAD_ALIAS = 'bad';
21
  const CLIENT_TEST_ALIAS = 'test';
22
23
  const DB_BAD_CLIENT_ALIAS = 'bad';
24
  const DB_INVALID_ALIAS = 'invalid';
25
  const DB_DEFAULT_ALIAS = 'default';
26
  const DB_UNSET_ALIAS = 'unset';
27
28
  public static $modules = [MongoDb::MODULE];
29
30
  /**
31
   * A test-specific instance of Settings.
32
   *
33
   * @var \Drupal\Core\Site\Settings
34
   */
35
  protected $settings;
36
37
  protected $uri;
38
39
  /**
40
   * Obtain the name of a per-test database.
41
   *
42
   * @param string $postfix
43
   *   The way for the caller to differentiate this database from others.
44
   *
45
   * @return string
46
   *   The name of the per-test database, like 'simpletest1234_foo'.
47
   */
48
  public function getTestDatabaseName($postfix) {
49
    return $this->getDatabasePrefix() . '_' . $postfix;
50
  }
51
52
  /**
53
   * Prepare the Settings from a base set of MongoDB settings.
54
   *
55
   * @return array
56
   *   A settings array only containing MongoDB-related settings.
57
   */
58
  protected function getSettingsArray() : array {
59
    return [
60
      'clients' => [
61
        static::CLIENT_BAD_ALIAS => [
62
          'uri' => 'mongodb://localhost:80',
63
          'uriOptions' => [],
64
          'driverOptions' => [],
65
        ],
66
        static::CLIENT_TEST_ALIAS => [
67
          'uri' => $this->uri,
68
          'uriOptions' => [],
69
          'driverOptions' => [],
70
        ],
71
      ],
72
      'databases' => [
73
        static::DB_DEFAULT_ALIAS => [static::CLIENT_TEST_ALIAS, $this->getDatabasePrefix()],
74
        static::DB_INVALID_ALIAS => [static::CLIENT_TEST_ALIAS, ''],
75
        static::DB_BAD_CLIENT_ALIAS => [static::CLIENT_BAD_ALIAS, $this->getDatabasePrefix()],
76
      ],
77
    ];
78
  }
79
80
  /**
81
   * {@inheritdoc}
82
   */
83
  public function setUp() {
84
    parent::setUp();
85
    // $_ENV if it comes from phpunit.xml <env>
86
    // $_SERVER if it comes from the phpunit command line environment.
87
    $this->uri = $_ENV['MONGODB_URI']
88
      ?? $_SERVER['MONGODB_URI']
89
      ?? static::DEFAULT_URI;
90
91
    $this->settings = new Settings([MongoDb::MODULE => $this->getSettingsArray()]);
92
  }
93
94
  /**
95
   * {@inheritdoc}
96
   */
97
  public function tearDown() {
98
    $clientFactory = new ClientFactory($this->settings);
99
    $databaseFactory = new DatabaseFactory($clientFactory, $this->settings);
100
    $databaseFactory->get(static::DB_DEFAULT_ALIAS)
101
      ->drop();
102
103
    parent::tearDown();
104
  }
105
106
}
107