1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Drupal\Tests\mongodb\Kernel; |
6
|
|
|
|
7
|
|
|
use Drupal\mongodb\ClientFactory; |
8
|
|
|
use Drupal\mongodb\DatabaseFactory; |
9
|
|
|
use Drupal\mongodb\MongoDb; |
10
|
|
|
use MongoDB\Database; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Tests the DatabaseFactory. |
14
|
|
|
* |
15
|
|
|
* @coversDefaultClass \Drupal\mongodb\DatabaseFactory |
16
|
|
|
* |
17
|
|
|
* @group MongoDB |
18
|
|
|
*/ |
19
|
|
|
class DatabaseFactoryTest extends MongoDbTestBase { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Modules to enable. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected static $modules = [MongoDb::MODULE]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The mongodb.client_factory service. |
30
|
|
|
* |
31
|
|
|
* @var \Drupal\mongodb\ClientFactory |
32
|
|
|
*/ |
33
|
|
|
protected $clientFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The mongodb.database_factory service. |
37
|
|
|
* |
38
|
|
|
* @var \Drupal\mongodb\DatabaseFactory |
39
|
|
|
*/ |
40
|
|
|
protected $databaseFactory; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function setUp(): void { |
46
|
|
|
parent::setUp(); |
47
|
|
|
$this->clientFactory = new ClientFactory($this->settings); |
48
|
|
|
$this->databaseFactory = new DatabaseFactory($this->clientFactory, $this->settings); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Test normal case. |
53
|
|
|
*/ |
54
|
|
|
public function testGetHappy() { |
55
|
|
|
$drupal = $this->databaseFactory->get(static::DB_DEFAULT_ALIAS); |
56
|
|
|
$this->assertInstanceOf(Database::class, $drupal, 'get() returns a valid database instance.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Test referencing an alias not present in settings. |
61
|
|
|
*/ |
62
|
|
|
public function testGetSadUnsetAlias() { |
63
|
|
|
try { |
64
|
|
|
$this->databaseFactory->get(static::DB_UNSET_ALIAS); |
65
|
|
|
$this->fail('Should not have returned a value for an unset database alias.'); |
66
|
|
|
} |
67
|
|
|
catch (\InvalidArgumentException $e) { |
68
|
|
|
$this->assertTrue(TRUE, 'Throws expected exception for unset database alias.'); |
69
|
|
|
} |
70
|
|
|
catch (\Exception $e) { |
71
|
|
|
$this->fail(strtr('Unexpected exception thrown for unset alias: @exception', [ |
72
|
|
|
'@exception' => $e->getMessage(), |
73
|
|
|
])); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Test referencing an alias pointing to an ill-formed (empty) database name. |
79
|
|
|
*/ |
80
|
|
|
public function testGetSadAliasForBadDatabase() { |
81
|
|
|
$database = $this->databaseFactory->get(static::DB_INVALID_ALIAS); |
82
|
|
|
$this->assertNull($database, 'Selecting an invalid alias returns a null database.'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|