1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\mongodb\Tests; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Util\Debug; |
6
|
|
|
use Drupal\Component\Render\FormattableMarkup; |
7
|
|
|
use Drupal\KernelTests\KernelTestBase; |
8
|
|
|
use Drupal\mongodb\ClientFactory; |
9
|
|
|
use Drupal\mongodb\DatabaseFactory; |
10
|
|
|
use MongoDB\Driver\Exception\InvalidArgumentException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class DatabaseFactoryTest |
14
|
|
|
* |
15
|
|
|
* @group MongoDB |
16
|
|
|
*/ |
17
|
|
|
class DatabaseFactoryTest extends MongoDbTestBase { |
18
|
|
|
|
19
|
|
|
public static $modules = ['mongodb']; |
20
|
|
|
|
21
|
|
|
/** |
|
|
|
|
22
|
|
|
* @var \Drupal\mongodb\ClientFactory |
23
|
|
|
*/ |
24
|
|
|
protected $clientFactory; |
25
|
|
|
|
26
|
|
|
/** |
|
|
|
|
27
|
|
|
* @var \Drupal\mongodb\DatabaseFactory |
28
|
|
|
*/ |
29
|
|
|
protected $databaseFactory; |
30
|
|
|
|
31
|
|
|
public function setUp() { |
|
|
|
|
32
|
|
|
parent::setUp(); |
33
|
|
|
$this->clientFactory = new ClientFactory($this->settings); |
34
|
|
|
$this->databaseFactory = new DatabaseFactory($this->clientFactory, $this->settings); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testGetHappy() { |
|
|
|
|
38
|
|
|
$drupal = $this->databaseFactory->get(static::DB_DEFAULT_ALIAS); |
39
|
|
|
$this->assertInstanceOf('MongoDB\Database', $drupal, 'get() returns a valid database instance.'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testGetSadUnsetAlias() { |
|
|
|
|
43
|
|
|
try { |
44
|
|
|
$this->databaseFactory->get(static::DB_UNSET_ALIAS); |
45
|
|
|
$this->fail("Should not have returned a value for an unset database alias."); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
catch (\InvalidArgumentException $e) { |
48
|
|
|
$this->assertTrue(TRUE, 'Throws expected exception for unset database alias.'); |
49
|
|
|
} |
50
|
|
|
catch (\Exception $e) { |
51
|
|
|
$this->fail("Unexpected exception thrown for unset alias: @exception", [ |
|
|
|
|
52
|
|
|
'@exception' => $e->getMessage(), |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testGetSadAliasForBadDatabase() { |
|
|
|
|
58
|
|
|
$db = $this->databaseFactory->get(static::DB_INVALID_ALIAS); |
|
|
|
|
59
|
|
|
$this->assertNull($db, "Selecting an invalid alias returns a null database."); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
} |
|
|
|
|
63
|
|
|
|