DatabaseConnectionStringFactoryTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 6
c 4
b 1
f 2
lcom 1
cbo 2
dl 0
loc 60
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testCreateDatabaseSourceStringReturnsCorrectPostgresDatabaseSourceString() 0 6 1
A testCreateDatabaseSourceStringReturnsCorrectMYSqlDatabaseSourceString() 0 6 1
A testCreateDatabaseSourceStringReturnsCorrectMYSqlDatabaseSourceStringWithPort() 0 7 1
A testCreateDatabaseSourceStringReturnsCorrectSQLiteDatabaseSourceString() 0 6 1
A testCreateDatabaseSourceStringThrowsDatabaseDriverNotSupportedExceptionWhenUnknownDriverIsPassed() 0 5 1
1
<?php
2
3
4
use Pyjac\ORM\DatabaseConnectionStringFactory;
5
6
class DatabaseConnectionStringFactoryTest extends PHPUnit_Framework_TestCase
7
{
8
    /**
9
     * The instance of DatabaseConnectionStringFactory used in the test.
10
     *
11
     * @var Pyjac\ORM\DatabaseConnectionStringFactory
12
     */
13
    protected $databaseConnectionStringFactory;
14
15
    /**
16
     * Configuration array.
17
     *
18
     * @var array
19
     */
20
    protected $config;
21
22
    protected function setUp()
23
    {
24
        $this->config = ['DBNAME' => 'Pyjac', 'DRIVER' => '', 'PASSWORD' => 'secret', 'HOSTNAME' => 'localhost', 'USERNAME' => 'pyjac'];
25
        $this->databaseConnectionStringFactory = new DatabaseConnectionStringFactory();
26
    }
27
28
    public function testCreateDatabaseSourceStringReturnsCorrectPostgresDatabaseSourceString()
29
    {
30
        $this->config['DRIVER'] = 'postgres';
31
        $dsn = $this->databaseConnectionStringFactory->createDatabaseSourceString($this->config);
32
        $this->assertEquals('pgsql:host=localhost;dbname=Pyjac', $dsn);
33
    }
34
35
    public function testCreateDatabaseSourceStringReturnsCorrectMYSqlDatabaseSourceString()
36
    {
37
        $this->config['DRIVER'] = 'mysql';
38
        $dsn = $this->databaseConnectionStringFactory->createDatabaseSourceString($this->config);
39
        $this->assertEquals('mysql:host=localhost;dbname=Pyjac', $dsn);
40
    }
41
42
    public function testCreateDatabaseSourceStringReturnsCorrectMYSqlDatabaseSourceStringWithPort()
43
    {
44
        $this->config['DRIVER'] = 'mysql';
45
        $this->config['PORT'] = '3306';
46
        $dsn = $this->databaseConnectionStringFactory->createDatabaseSourceString($this->config);
47
        $this->assertEquals('mysql:host=localhost;dbname=Pyjac;port=3306', $dsn);
48
    }
49
50
    public function testCreateDatabaseSourceStringReturnsCorrectSQLiteDatabaseSourceString()
51
    {
52
        $this->config['DRIVER'] = 'sqlite';
53
        $dsn = $this->databaseConnectionStringFactory->createDatabaseSourceString($this->config);
54
        $this->assertEquals('sqlite::memory:', $dsn);
55
    }
56
57
    /**
58
     * @expectedException Pyjac\ORM\Exception\DatabaseDriverNotSupportedException
59
     */
60
    public function testCreateDatabaseSourceStringThrowsDatabaseDriverNotSupportedExceptionWhenUnknownDriverIsPassed()
61
    {
62
        $this->config['DRIVER'] = 'pyjac';
63
        $dsn = $this->databaseConnectionStringFactory->createDatabaseSourceString($this->config);
0 ignored issues
show
Unused Code introduced by
$dsn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
    }
65
}
66