MongoDbTestBase::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
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 string[]
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 $settings;
47
48
  /**
49
   * The MongoDB URI for a test server.
50
   *
51
   * @var string
52
   */
53
  protected string $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
   * Provide a sane set of default settings.
70
   *
71
   * @return array{clients: array<string, array{uri: string, uriOptions: array<string,mixed>, driverOptions: array<string,mixed>}>, databases: array<string,array{0:string,1:string}>>}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{clients: array<str...y{0:string,1:string}>>} at position 50 could not be parsed: Expected '}' at position 50, but found '>'.
Loading history...
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
   * This setUp configures $this->settings and $this->>uri.
109
   */
110
  public function setUp(): void {
111
    parent::setUp();
112
    // $_ENV if it comes from phpunit.xml <env>
113
    // $_SERVER if it comes from the phpunit command line environment.
114
    $this->uri = $_ENV['MONGODB_URI']
115
      ?? $_SERVER['MONGODB_URI']
116
      ?? static::DEFAULT_URI;
117
118
    $this->settings = new Settings([MongoDb::MODULE => $this->getSettingsArray()]);
119
  }
120
121
  /**
122
   * {@inheritdoc}
123
   *
124
   * This tearDown drops the test database, so child classes do not need to
125
   * clean up behind them.
126
   */
127
  public function tearDown(): void {
128
    $clientFactory = new ClientFactory($this->settings);
129
    $databaseFactory = new DatabaseFactory($clientFactory, $this->settings);
130
    $databaseFactory->get(static::DB_DEFAULT_ALIAS)
131
      ->drop();
132
133
    parent::tearDown();
134
  }
135
136
}
137