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

MongoDbTestBase   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
c 0
b 0
f 0
dl 0
loc 111
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSettingsArray() 0 26 1
A getTestDatabaseName() 0 2 1
A tearDown() 0 7 1
A setUp() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\Tests\mongodb\Kernel;
6
7
use Drupal\Core\Site\Settings;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Site\Settings was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Drupal\KernelTests\KernelTestBase;
0 ignored issues
show
Bug introduced by
The type Drupal\KernelTests\KernelTestBase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
20
  const DEFAULT_URI = 'mongodb://localhost:27017';
21
22
  const CLIENT_BAD_ALIAS = 'bad';
23
24
  const CLIENT_TEST_ALIAS = 'test';
25
26
  const DB_BAD_CLIENT_ALIAS = 'bad';
27
28
  const DB_INVALID_ALIAS = 'invalid';
29
30
  const DB_DEFAULT_ALIAS = 'default';
31
32
  const DB_UNSET_ALIAS = 'unset';
33
34
  /**
35
   * Modules to enable.
36
   *
37
   * @var array
38
   */
39
  protected static $modules = [MongoDb::MODULE];
40
41
  /**
42
   * A test-specific instance of Settings.
43
   *
44
   * @var \Drupal\Core\Site\Settings
45
   */
46
  protected $settings;
47
48
  /**
49
   * The MongoDB URI for a test server.
50
   *
51
   * @var string
52
   */
53
  protected $uri;
54
55
  /**
56
   * Obtain the name of a per-test database.
57
   *
58
   * @param string $postfix
59
   *   The way for the caller to differentiate this database from others.
60
   *
61
   * @return string
62
   *   The name of the per-test database, like 'simpletest1234_foo'.
63
   */
64
  public function getTestDatabaseName($postfix) {
65
    return $this->getDatabasePrefix() . '_' . $postfix;
66
  }
67
68
  /**
69
   * Prepare the Settings from a base set of MongoDB settings.
70
   *
71
   * @return array
72
   *   A settings array only containing MongoDB-related settings.
73
   */
74
  protected function getSettingsArray(): array {
75
    return [
76
      'clients' => [
77
        static::CLIENT_BAD_ALIAS => [
78
          'uri' => 'mongodb://localhost:80',
79
          'uriOptions' => [],
80
          'driverOptions' => [],
81
        ],
82
        static::CLIENT_TEST_ALIAS => [
83
          'uri' => $this->uri,
84
          'uriOptions' => [],
85
          'driverOptions' => [],
86
        ],
87
      ],
88
      'databases' => [
89
        static::DB_DEFAULT_ALIAS => [
90
          static::CLIENT_TEST_ALIAS,
91
          $this->getDatabasePrefix(),
92
        ],
93
        static::DB_INVALID_ALIAS => [
94
          static::CLIENT_TEST_ALIAS,
95
          '',
96
        ],
97
        static::DB_BAD_CLIENT_ALIAS => [
98
          static::CLIENT_BAD_ALIAS,
99
          $this->getDatabasePrefix(),
100
        ],
101
      ],
102
    ];
103
  }
104
105
  /**
106
   * {@inheritdoc}
107
   */
108
  public function setUp(): void {
109
    parent::setUp();
110
    // $_ENV if it comes from phpunit.xml <env>
111
    // $_SERVER if it comes from the phpunit command line environment.
112
    $this->uri = $_ENV['MONGODB_URI']
113
      ?? $_SERVER['MONGODB_URI']
114
      ?? static::DEFAULT_URI;
115
116
    $this->settings = new Settings([MongoDb::MODULE => $this->getSettingsArray()]);
117
  }
118
119
  /**
120
   * {@inheritdoc}
121
   */
122
  public function tearDown(): void {
123
    $clientFactory = new ClientFactory($this->settings);
124
    $databaseFactory = new DatabaseFactory($clientFactory, $this->settings);
125
    $databaseFactory->get(static::DB_DEFAULT_ALIAS)
126
      ->drop();
127
128
    parent::tearDown();
129
  }
130
131
}
132