Completed
Push — develop ( 0ccfdd...5df781 )
by Oyebanji Jacob
02:23
created

testTryAgainIfCausedByLostConnectionThrowsExceptionWhenReasonForExceptionIsNotConnectionLoss()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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