Completed
Push — mkdocs ( 44be76...c8c54f )
by Frédéric G.
02:43
created

MongoDbTestBase::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 9.4285
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
   * {@inheritdoc}
38
   */
39
  public function setUp() {
40
    parent::setUp();
41
    // $_ENV if it comes from phpunit.xml <env>
42
    // $_SERVER if it comes from the phpunit command line environment.
43
    $this->uri = $_ENV['MONGODB_URL'] ?? $_SERVER['MONGODB_URI'] ?? static::DEFAULT_URI;
44
45
    $this->settings = new Settings([
46
      'mongodb' => [
47
        'clients' => [
48
          static::CLIENT_BAD_ALIAS => [
49
            'uri' => 'mongodb://localhost:80',
50
            'uriOptions' => [],
51
            'driverOptions' => [],
52
          ],
53
          static::CLIENT_TEST_ALIAS => [
54
            'uri' => $this->uri,
55
            'uriOptions' => [],
56
            'driverOptions' => [],
57
          ],
58
        ],
59
        'databases' => [
60
          static::DB_DEFAULT_ALIAS => [static::CLIENT_TEST_ALIAS, $this->getDatabasePrefix()],
61
          static::DB_INVALID_ALIAS => [static::CLIENT_TEST_ALIAS, ''],
62
          static::DB_BAD_CLIENT_ALIAS => [static::CLIENT_BAD_ALIAS, $this->getDatabasePrefix()],
63
        ],
64
      ],
65
    ]);
66
  }
67
68
  /**
69
   * {@inheritdoc}
70
   */
71
  public function tearDown() {
72
    $clientFactory = new ClientFactory($this->settings);
73
    $databaseFactory = new DatabaseFactory($clientFactory, $this->settings);
74
    $databaseFactory->get(static::DB_DEFAULT_ALIAS)
75
      ->drop();
76
77
    parent::tearDown();
78
  }
79
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
80