Completed
Push — 2762893-mongodb_tests ( 476c0a...a0e627 )
by Frédéric G.
03:02
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 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 14
rs 9.4285
1
<?php
2
3
namespace Drupal\mongodb\Tests;
4
5
use Doctrine\Common\Util\Debug;
6
use Drupal\Component\Render\FormattableMarkup;
7
use Drupal\KernelTests\KernelTestBase;
8
use Drupal\mongodb\ClientFactory;
9
use Drupal\mongodb\DatabaseFactory;
10
use MongoDB\Driver\Exception\InvalidArgumentException;
11
12
/**
13
 * Class DatabaseFactoryTest
14
 *
15
 * @group MongoDB
16
 */
17
class DatabaseFactoryTest extends MongoDbTestBase {
18
19
  public static $modules = ['mongodb'];
20
21
  /**
0 ignored issues
show
introduced by
Missing short description in doc comment
Loading history...
22
   * @var \Drupal\mongodb\ClientFactory
23
   */
24
  protected $clientFactory;
25
26
  /**
0 ignored issues
show
introduced by
Missing short description in doc comment
Loading history...
27
   * @var \Drupal\mongodb\DatabaseFactory
28
   */
29
  protected $databaseFactory;
30
31
  public function setUp() {
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
32
    parent::setUp();
33
    $this->clientFactory = new ClientFactory($this->settings);
34
    $this->databaseFactory = new DatabaseFactory($this->clientFactory, $this->settings);
35
  }
36
37
  public function testGetHappy() {
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
38
    $drupal = $this->databaseFactory->get(static::DB_DEFAULT_ALIAS);
39
    $this->assertInstanceOf('MongoDB\Database', $drupal, 'get() returns a valid database instance.');
40
  }
41
42
  public function testGetSadUnsetAlias() {
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
43
    try {
44
      $this->databaseFactory->get(static::DB_UNSET_ALIAS);
45
      $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...
46
    }
47
    catch (\InvalidArgumentException $e) {
48
      $this->assertTrue(TRUE, 'Throws expected exception for unset database alias.');
49
    }
50
    catch (\Exception $e) {
51
      $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...
52
        '@exception' => $e->getMessage(),
53
      ]);
54
    }
55
  }
56
57
  public function testGetSadAliasForBadDatabase() {
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
58
    $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...
59
    $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...
60
  }
61
62
}
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...
63