Passed
Pull Request — 8.x-2.x (#58)
by Frédéric G.
03:34
created

ToolsTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testToolsService() 0 6 1
A testToolsSettings() 0 7 1
A testFind() 0 35 2
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->assertInternalType('array', $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
    $database = $dbFactory->get(MongoDb::DB_DEFAULT);
47
48
    $collectionName = $this->randomMachineName();
49
    $collection = $database->selectCollection($collectionName);
50
    $collection->drop();
51
    $documents = [
52
      ["foo" => "bar"],
53
      ["foo" => "baz"],
54
    ];
55
    $docCount = count($documents);
56
    $collection->insertMany($documents);
57
    // Just a sanity check.
58
    $this->assertEquals($docCount, MongoDb::countCollection($collection));
59
60
    $tools = $this->container->get(MongoDb::SERVICE_TOOLS);
61
62
    $expectations = [
63
      [[], 2],
64
      [["foo" => "baz"], 1],
65
      [["foo" => "qux"], 0],
66
    ];
67
68
    foreach ($expectations as $expectation) {
69
      // Current coding standards don't support foreach (foo as list()).
70
      list($selector, $count) = $expectation;
71
72
      $selectorString = json_encode($selector);
73
      $actual = $tools->find(MongoDb::DB_DEFAULT, $collectionName, $selectorString);
74
      $this->assertInternalType('array', $actual);
75
      $this->assertEquals($count, count($actual));
76
    }
77
  }
78
79
}
80