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:', '', '', []]); |
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.