Completed
Pull Request — 8.x-2.x (#27)
by Frédéric G.
07:33
created

MongoDbTestBase::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\mongodb\Tests\Kernel;
4
5
use Drupal\Core\Site\Settings;
6
use Drupal\KernelTests\KernelTestBase;
7
use Drupal\mongodb\ClientFactory;
8
use Drupal\mongodb\DatabaseFactory;
9
use Drupal\mongodb\MongoDb;
10
11
/**
12
 * Class MongoDbTestBase provides basic setUp()/tearDown() for MongoDB.
13
 *
14
 * @group MongoDB
15
 */
16
abstract class MongoDbTestBase extends KernelTestBase {
17
  const DEFAULT_URI = 'mongodb://localhost:27017';
18
  const CLIENT_BAD_ALIAS = 'bad';
19
  const CLIENT_TEST_ALIAS = 'test';
20
21
  const DB_BAD_CLIENT_ALIAS = 'bad';
22
  const DB_INVALID_ALIAS = 'invalid';
23
  const DB_DEFAULT_ALIAS = 'default';
24
  const DB_UNSET_ALIAS = 'unset';
25
26
  public static $modules = [MongoDb::MODULE];
27
28
  /**
29
   * A test-specific instance of Settings.
30
   *
31
   * @var \Drupal\Core\Site\Settings
32
   */
33
  protected $settings;
34
35
  protected $uri;
36
37
  /**
38
   * Obtain the name of a per-test database.
39
   *
40
   * @param string $postfix
41
   *   The way for the caller to differentiate this database from others.
42
   *
43
   * @return string
44
   *   The name of the per-test database, like 'simpletest1234_foo'.
45
   */
46
  public function getTestDatabaseName($postfix) {
47
    return $this->getDatabasePrefix() . '_' . $postfix;
48
  }
49
50
  /**
51
   * Prepare the Settings from a base set of MongoDB settings.
52
   *
53
   * @return array
54
   *   A settings array only containing MongoDB-related settings.
55
   */
56
  protected function getSettingsArray(): array {
0 ignored issues
show
introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
57
    return [
58
      MongoDb::MODULE => [
59
        'clients' => [
60
          static::CLIENT_BAD_ALIAS => [
61
            'uri' => 'mongodb://localhost:80',
62
            'uriOptions' => [],
63
            'driverOptions' => [],
64
          ],
65
          static::CLIENT_TEST_ALIAS => [
66
            'uri' => $this->uri,
67
            'uriOptions' => [],
68
            'driverOptions' => [],
69
          ],
70
        ],
71
        'databases' => [
72
          static::DB_DEFAULT_ALIAS => [static::CLIENT_TEST_ALIAS, $this->getDatabasePrefix()],
73
          static::DB_INVALID_ALIAS => [static::CLIENT_TEST_ALIAS, ''],
74
          static::DB_BAD_CLIENT_ALIAS => [static::CLIENT_BAD_ALIAS, $this->getDatabasePrefix()],
75
        ],
76
      ],
77
    ];
78
  }
79
80
  /**
81
   * {@inheritdoc}
82
   */
83
  public function setUp() {
84
    parent::setUp();
85
    // $_ENV if it comes from phpunit.xml <env>
86
    // $_SERVER if it comes from the phpunit command line environment.
87
    $this->uri = $_ENV['MONGODB_URI'] ?? $_SERVER['MONGODB_URI'] ?? static::DEFAULT_URI;
88
89
    $this->settings = new Settings($this->getSettingsArray());
90
  }
91
92
  /**
93
   * {@inheritdoc}
94
   */
95
  public function tearDown() {
96
    $clientFactory = new ClientFactory($this->settings);
97
    $databaseFactory = new DatabaseFactory($clientFactory, $this->settings);
98
    $databaseFactory->get(static::DB_DEFAULT_ALIAS)
99
      ->drop();
100
101
    parent::tearDown();
102
  }
103
104
}
105