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

DatabaseConnectionTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
use \Mockery as m;
4
use org\bovigo\vfs\vfsStream;
5
use Pyjac\ORM\DatabaseConnection;
6
7
class DatabaseConnectionTest extends PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * Instance of DatabaseConnection used in test.
11
     */
12
    protected $databaseConnection;
13
14
    /**
15
     * Instance of DatabaseConnectionStringFactory used in test.
16
     */
17
    protected $databaseConnectionStringFactory;
18
19
     /**
20
     * root directory
21
     *
22
     * @type  vfsStreamDirectory
23
     */
24
    protected $root;
25
26
    public function setUp()
27
    {
28
        $this->databaseConnectionStringFactory =
29
                        m::mock('Pyjac\ORM\DatabaseConnectionStringFactoryInterface');
30
        $this->databaseConnectionStringFactory->shouldReceive('createDatabaseSourceString')
31
                                             ->with(['DRIVER' => 'sqlite', 'HOSTNAME' => '127.0.0.1', 'USERNAME' => '', 'PASSWORD' => '', 'DBNAME' => 'potatoORM', 'PORT' => '54320'])->once()->andReturn('sqlite::memory:');
32
33
        $this->databaseConnection = new DatabaseConnection($this->databaseConnectionStringFactory);
34
    }
35
36
    public function testCreateConnectionReturnsDatabaseConnection()
37
    {
38
        $dbInstance = $this->databaseConnection->createConnection('sqlite::memory:');
39
40
        $this->assertInstanceOf('PDO', $dbInstance);
41
    }
42
43
    public function testGetInstanceReturnsCorrectInstance()
44
    {
45
        $dbInstance = DatabaseConnection::getInstance();
46
47
        $this->assertInstanceOf('Pyjac\ORM\DatabaseConnection', $dbInstance);
48
    }
49
50
    public function testSetOptionsAndGetOptionsReturnsCorrectValue()
51
    {
52
        $options = [
53
            PDO::ATTR_CASE              => PDO::CASE_NATURAL,
54
            PDO::ATTR_ERRMODE           => PDO::ERRMODE_EXCEPTION,
55
            PDO::ATTR_ORACLE_NULLS      => PDO::NULL_NATURAL,
56
            PDO::ATTR_STRINGIFY_FETCHES => false,
57
            PDO::ATTR_EMULATE_PREPARES  => false,
58
        ];
59
        $this->databaseConnection->setDefaultOptions($options);
60
61
        $this->assertEquals($this->databaseConnection->getDefaultOptions(), $options);
62
    }
63
64
    public function testTryAgainIfCausedByLostConnectionCreateNewConnectionWhenReasonForExceptionIsConnectionLoss()
65
    {
66
        $e = new \Exception('Error while sending');
67
        $result = $this->invokeMethod($this->databaseConnection, 'tryAgainIfCausedByLostConnection', [$e, 'sqlite::memory:', '', '', []]);
68
69
        $this->assertInstanceOf('PDO', $result);
70
71
        $e = new \Exception('is dead or not enabled');
72
        $result = $this->invokeMethod($this->databaseConnection, 'tryAgainIfCausedByLostConnection', [$e, 'sqlite::memory:', '', '', []]);
73
        $this->assertInstanceOf('PDO', $result);
74
75
        $e = new \Exception('SSL connection has been closed unexpectedly');
76
        $result = $this->invokeMethod($this->databaseConnection, 'tryAgainIfCausedByLostConnection', [$e, 'sqlite::memory:', '', '', []]);
77
        $this->assertInstanceOf('PDO', $result);
78
79
        $e = new \Exception('no connection to the server');
80
        $result = $this->invokeMethod($this->databaseConnection, 'tryAgainIfCausedByLostConnection', [$e, 'sqlite::memory:', '', '', []]);
81
        $this->assertInstanceOf('PDO', $result);
82
83
        $e = new \Exception('decryption failed or bad record mac');
84
        $result = $this->invokeMethod($this->databaseConnection, 'tryAgainIfCausedByLostConnection', [$e, 'sqlite::memory:', '', '', []]);
85
        $this->assertInstanceOf('PDO', $result);
86
    }
87
88
    /**
89
     * @expectedException \Exception
90
     */
91
    public function testTryAgainIfCausedByLostConnectionThrowsExceptionWhenReasonForExceptionIsNotConnectionLoss()
92
    {
93
        $e = new \Exception('PHP Rocks !!!');
94
        $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...
95
    }
96
97
98
99
    /**
100
     * Reference: https://jtreminio.com/2013/03/unit-testing-tutorial-part-3-testing-protected-private-methods-coverage-reports-and-crap/
101
     * Call protected/private method of a class.
102
     *
103
     * @param object &$object    Instantiated object that we will run method on.
104
     * @param string $methodName Method name to call
105
     * @param array  $parameters Array of parameters to pass into method.
106
     *
107
     * @return mixed Method return.
108
     */
109
    public function invokeMethod(&$object, $methodName, array $parameters = [])
110
    {
111
        $reflection = new \ReflectionClass(get_class($object));
112
        $method = $reflection->getMethod($methodName);
113
        $method->setAccessible(true);
114
115
        return $method->invokeArgs($object, $parameters);
116
    }
117
}
118