1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\mongodb\Tests; |
4
|
|
|
|
5
|
|
|
use Drupal\mongodb\ClientFactory; |
6
|
|
|
use Drupal\mongodb\DatabaseFactory; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class DatabaseFactoryTest. |
10
|
|
|
* |
11
|
|
|
* @group MongoDB |
12
|
|
|
*/ |
13
|
|
|
class DatabaseFactoryTest extends MongoDbTestBase { |
14
|
|
|
|
15
|
|
|
public static $modules = ['mongodb']; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The mongodb.client_factory service. |
19
|
|
|
* |
20
|
|
|
* @var \Drupal\mongodb\ClientFactory |
21
|
|
|
*/ |
22
|
|
|
protected $clientFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The mongodb.database_factory service. |
26
|
|
|
* |
27
|
|
|
* @var \Drupal\mongodb\DatabaseFactory |
28
|
|
|
*/ |
29
|
|
|
protected $databaseFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function setUp() { |
35
|
|
|
parent::setUp(); |
36
|
|
|
$this->clientFactory = new ClientFactory($this->settings); |
37
|
|
|
$this->databaseFactory = new DatabaseFactory($this->clientFactory, $this->settings); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Test normal case. |
42
|
|
|
*/ |
43
|
|
|
public function testGetHappy() { |
44
|
|
|
$drupal = $this->databaseFactory->get(static::DB_DEFAULT_ALIAS); |
45
|
|
|
$this->assertInstanceOf('MongoDB\Database', $drupal, 'get() returns a valid database instance.'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Test referencing an alias not present in settings. |
50
|
|
|
*/ |
51
|
|
|
public function testGetSadUnsetAlias() { |
52
|
|
|
try { |
53
|
|
|
$this->databaseFactory->get(static::DB_UNSET_ALIAS); |
54
|
|
|
$this->fail("Should not have returned a value for an unset database alias."); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
catch (\InvalidArgumentException $e) { |
57
|
|
|
$this->assertTrue(TRUE, 'Throws expected exception for unset database alias.'); |
58
|
|
|
} |
59
|
|
|
catch (\Exception $e) { |
60
|
|
|
$this->fail("Unexpected exception thrown for unset alias: @exception", [ |
|
|
|
|
61
|
|
|
'@exception' => $e->getMessage(), |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Test referencing an alias pointing to an ill-formed (empty) database name. |
68
|
|
|
*/ |
69
|
|
|
public function testGetSadAliasForBadDatabase() { |
70
|
|
|
$db = $this->databaseFactory->get(static::DB_INVALID_ALIAS); |
|
|
|
|
71
|
|
|
$this->assertNull($db, "Selecting an invalid alias returns a null database."); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
|
|
|
|
75
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.