Completed
Push — 8.x-2.x ( 4496d4...575826 )
by Frédéric G.
02:50
created

MongoDbTestBase::getTestDatabaseName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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