Completed
Pull Request — master (#4)
by Oyebanji Jacob
07:17 queued 05:04
created

testTryAgainIfCausedByLostConnectionCreateNewConnectionWhenReasonForExceptionIsConnectionLoss()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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