Completed
Pull Request — master (#4)
by Oyebanji Jacob
02:18
created

DatabaseConnectionTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A testCreateConnectionReturnsDatabaseConnection() 0 6 1
A testGetInstanceReturnsCorrectInstance() 0 6 1
A testSetOptionsAndGetOptionsReturnsCorrectValue() 0 13 1
A testTryAgainIfCausedByLostConnectionCreateNewConnectionWhenReasonForExceptionIsConnectionLoss() 0 7 1
A testTryAgainIfCausedByLostConnectionThrowsExceptionWhenReasonForExceptionIsNotConnectionLoss() 0 5 1
A invokeMethod() 0 8 1
1
<?php
2
3
use \Mockery as m;
4
use Pyjac\ORM\DatabaseConnection;
5
6
class DatabaseConnectionTest extends PHPUnit_Framework_TestCase
7
{
8
    /**
9
     * Instance of DatabaseConnection used in test.
10
     */
11
    protected $databaseConnection;
12
13
    public function setUp()
14
    {
15
        $databaseConnectionStringFactory =
16
                        m::mock('Pyjac\ORM\DatabaseConnectionStringFactoryInterface');
17
        $databaseConnectionStringFactory->shouldReceive('createDatabaseSourceString')
18
                                             ->with(['DRIVER' => 'sqlite', 'HOSTNAME' => '127.0.0.1', 'USERNAME' => '', 'PASSWORD' => '', 'DBNAME' => 'potatoORM', 'PORT' => '54320'])->once()->andReturn('sqlite::memory:');
19
20
        $this->databaseConnection = new DatabaseConnection($databaseConnectionStringFactory);
21
    }
22
23
    public function testCreateConnectionReturnsDatabaseConnection()
24
    {
25
        $dbInstance = $this->databaseConnection->createConnection('sqlite::memory:');
26
27
        $this->assertInstanceOf('PDO', $dbInstance);
28
    }
29
30
    public function testGetInstanceReturnsCorrectInstance()
31
    {
32
        $dbInstance = DatabaseConnection::getInstance();
33
34
        $this->assertInstanceOf('Pyjac\ORM\DatabaseConnection', $dbInstance);
35
    }
36
37
    public function testSetOptionsAndGetOptionsReturnsCorrectValue()
38
    {
39
        $options = [
40
            PDO::ATTR_CASE              => PDO::CASE_NATURAL,
41
            PDO::ATTR_ERRMODE           => PDO::ERRMODE_EXCEPTION,
42
            PDO::ATTR_ORACLE_NULLS      => PDO::NULL_NATURAL,
43
            PDO::ATTR_STRINGIFY_FETCHES => false,
44
            PDO::ATTR_EMULATE_PREPARES  => false,
45
        ];
46
        $this->databaseConnection->setDefaultOptions($options);
47
48
        $this->assertEquals($this->databaseConnection->getDefaultOptions(), $options);
49
    }
50
51
    public function testTryAgainIfCausedByLostConnectionCreateNewConnectionWhenReasonForExceptionIsConnectionLoss()
52
    {
53
        $e = new \Exception('Error while sending');
54
        $result = $this->invokeMethod($this->databaseConnection, 'tryAgainIfCausedByLostConnection', [$e, 'sqlite::memory:', '', '', []]);
55
56
        $this->assertInstanceOf('PDO', $result);
57
    }
58
59
    /**
60
     * @expectedException \Exception
61
     */
62
    public function testTryAgainIfCausedByLostConnectionThrowsExceptionWhenReasonForExceptionIsNotConnectionLoss()
63
    {
64
        $e = new \Exception('PHP Rocks !!!');
65
        $result = $this->invokeMethod($this->databaseConnection, 'tryAgainIfCausedByLostConnection', [$e, 'sqlite::memory:', '', '', []]);
0 ignored issues
show
Unused Code introduced by
$result 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...
66
    }
67
68
    /**
69
     * Reference: https://jtreminio.com/2013/03/unit-testing-tutorial-part-3-testing-protected-private-methods-coverage-reports-and-crap/
70
     * Call protected/private method of a class.
71
     *
72
     * @param object &$object    Instantiated object that we will run method on.
73
     * @param string $methodName Method name to call
74
     * @param array  $parameters Array of parameters to pass into method.
75
     *
76
     * @return mixed Method return.
77
     */
78
    public function invokeMethod(&$object, $methodName, array $parameters = [])
79
    {
80
        $reflection = new \ReflectionClass(get_class($object));
81
        $method = $reflection->getMethod($methodName);
82
        $method->setAccessible(true);
83
84
        return $method->invokeArgs($object, $parameters);
85
    }
86
}
87