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
|
|
|
|
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}>>} |
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths