Completed
Push — mkdocs ( 0b51ae...44be76 )
by Frédéric G.
10:49 queued 07:15
created

DatabaseFactoryTest::testGetSadUnsetAlias()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 9
c 1
b 0
f 1
nc 5
nop 0
dl 0
loc 14
rs 9.4285
1
<?php
2
3
namespace Drupal\mongodb\Tests;
4
5
use Drupal\mongodb\ClientFactory;
6
use Drupal\mongodb\DatabaseFactory;
7
8
/**
9
 * Class DatabaseFactoryTest.
10
 *
11
 * @group MongoDB
12
 */
13
class DatabaseFactoryTest extends MongoDbTestBase {
14
15
  public static $modules = ['mongodb'];
16
17
  /**
18
   * The mongodb.client_factory service.
19
   *
20
   * @var \Drupal\mongodb\ClientFactory
21
   */
22
  protected $clientFactory;
23
24
  /**
25
   * The mongodb.database_factory service.
26
   *
27
   * @var \Drupal\mongodb\DatabaseFactory
28
   */
29
  protected $databaseFactory;
30
31
  /**
32
   * {@inheritdoc}
33
   */
34
  public function setUp() {
35
    parent::setUp();
36
    $this->clientFactory = new ClientFactory($this->settings);
37
    $this->databaseFactory = new DatabaseFactory($this->clientFactory, $this->settings);
38
  }
39
40
  /**
41
   * Test normal case.
42
   */
43
  public function testGetHappy() {
44
    $drupal = $this->databaseFactory->get(static::DB_DEFAULT_ALIAS);
45
    $this->assertInstanceOf('MongoDB\Database', $drupal, 'get() returns a valid database instance.');
46
  }
47
48
  /**
49
   * Test referencing an alias not present in settings.
50
   */
51
  public function testGetSadUnsetAlias() {
52
    try {
53
      $this->databaseFactory->get(static::DB_UNSET_ALIAS);
54
      $this->fail("Should not have returned a value for an unset database alias.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Should not have returned...n unset database alias. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
55
    }
56
    catch (\InvalidArgumentException $e) {
57
      $this->assertTrue(TRUE, 'Throws expected exception for unset database alias.');
58
    }
59
    catch (\Exception $e) {
60
      $this->fail("Unexpected exception thrown for unset alias: @exception", [
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Unexpected exception thr...unset alias: @exception does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
61
        '@exception' => $e->getMessage(),
62
      ]);
63
    }
64
  }
65
66
  /**
67
   * Test referencing an alias pointing to an ill-formed (empty) database name.
68
   */
69
  public function testGetSadAliasForBadDatabase() {
70
    $db = $this->databaseFactory->get(static::DB_INVALID_ALIAS);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $db. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
71
    $this->assertNull($db, "Selecting an invalid alias returns a null database.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Selecting an invalid ali...eturns a null database. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
72
  }
73
74
}
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...
75