1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Drupal\Tests\mongodb\Kernel; |
6
|
|
|
|
7
|
|
|
use Drupal\mongodb\Install\Tools; |
8
|
|
|
use Drupal\mongodb\MongoDb; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class CommandsTest. |
12
|
|
|
* |
13
|
|
|
* @coversDefaultClass \Drupal\mongodb\Install\Tools |
14
|
|
|
* |
15
|
|
|
* @group MongoDB |
16
|
|
|
*/ |
17
|
|
|
class ToolsTest extends MongoDbTestBase { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @covers ::__construct |
21
|
|
|
*/ |
22
|
|
|
public function testToolsService() { |
23
|
|
|
$tools = $this->container->get(MongoDb::SERVICE_TOOLS); |
24
|
|
|
$this->assertInstanceOf(Tools::class, $tools, "Tools service is available"); |
25
|
|
|
$this->assertTrue(method_exists($tools, 'find')); |
26
|
|
|
$this->assertTrue(method_exists($tools, 'settings')); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @covers ::settings |
31
|
|
|
*/ |
32
|
|
|
public function testToolsSettings() { |
33
|
|
|
$tools = $this->container->get(MongoDb::SERVICE_TOOLS); |
34
|
|
|
$actual = $tools->settings(); |
35
|
|
|
$this->assertIsArray($actual); |
36
|
|
|
$expected = $this->getSettingsArray(); |
37
|
|
|
$this->assertEquals($expected, $actual); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @covers ::find |
42
|
|
|
*/ |
43
|
|
|
public function testFind() { |
44
|
|
|
/** @var \Drupal\mongodb\DatabaseFactory $database */ |
45
|
|
|
$dbFactory = $this->container->get(MongoDb::SERVICE_DB_FACTORY); |
46
|
|
|
/** @var \MongoDB\Database $database */ |
47
|
|
|
$database = $dbFactory->get(MongoDb::DB_DEFAULT); |
48
|
|
|
|
49
|
|
|
$collectionName = $this->randomMachineName(); |
50
|
|
|
$collection = $database->selectCollection($collectionName); |
51
|
|
|
$collection->drop(); |
52
|
|
|
$documents = [ |
53
|
|
|
["foo" => "bar"], |
54
|
|
|
["foo" => "baz"], |
55
|
|
|
]; |
56
|
|
|
$docCount = count($documents); |
57
|
|
|
$collection->insertMany($documents); |
58
|
|
|
// Just a sanity check. |
59
|
|
|
$this->assertEquals($docCount, $collection->countDocuments()); |
60
|
|
|
|
61
|
|
|
$tools = $this->container->get(MongoDb::SERVICE_TOOLS); |
62
|
|
|
|
63
|
|
|
$expectations = [ |
64
|
|
|
[[], 2], |
65
|
|
|
[["foo" => "baz"], 1], |
66
|
|
|
[["foo" => "qux"], 0], |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
foreach ($expectations as $expectation) { |
70
|
|
|
// Current coding standards don't support foreach (foo as list()). |
71
|
|
|
list($selector, $count) = $expectation; |
72
|
|
|
|
73
|
|
|
$selectorString = json_encode($selector); |
74
|
|
|
$actual = $tools->find(MongoDb::DB_DEFAULT, $collectionName, $selectorString); |
75
|
|
|
$this->assertIsArray($actual); |
76
|
|
|
$this->assertEquals($count, count($actual)); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|