@@ -73,7 +73,7 @@ |
||
| 73 | 73 | * Reference: https://jtreminio.com/2013/03/unit-testing-tutorial-part-3-testing-protected-private-methods-coverage-reports-and-crap/ |
| 74 | 74 | * Call protected/private method of a class. |
| 75 | 75 | * |
| 76 | - * @param object &$object Instantiated object that we will run method on. |
|
| 76 | + * @param object DatabaseConnection Instantiated object that we will run method on. |
|
| 77 | 77 | * @param string $methodName Method name to call |
| 78 | 78 | * @param array $parameters Array of parameters to pass into method. |
| 79 | 79 | * |
@@ -5,86 +5,86 @@ |
||
| 5 | 5 | |
| 6 | 6 | class DatabaseConnectionTest extends PHPUnit_Framework_TestCase |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * instance of DatabaseConnection used in test. |
|
| 10 | - */ |
|
| 11 | - protected $databaseConnection; |
|
| 8 | + /** |
|
| 9 | + * instance of DatabaseConnection used in test. |
|
| 10 | + */ |
|
| 11 | + protected $databaseConnection; |
|
| 12 | 12 | |
| 13 | 13 | |
| 14 | 14 | |
| 15 | - public function setUp(){ |
|
| 16 | - $databaseConnectionStringFactory = |
|
| 15 | + public function setUp(){ |
|
| 16 | + $databaseConnectionStringFactory = |
|
| 17 | 17 | m::mock('Pyjac\ORM\DatabaseConnectionStringFactoryInterface'); |
| 18 | 18 | $databaseConnectionStringFactory->shouldReceive('createDatabaseSourceString') |
| 19 | - ->with(['DRIVER' => 'sqlite', 'HOSTNAME' => '127.0.0.1', 'USERNAME' => '', 'PASSWORD' => '', 'DBNAME' => 'potatoORM', 'PORT' => '54320'])->once()->andReturn('sqlite::memory:'); |
|
| 19 | + ->with(['DRIVER' => 'sqlite', 'HOSTNAME' => '127.0.0.1', 'USERNAME' => '', 'PASSWORD' => '', 'DBNAME' => 'potatoORM', 'PORT' => '54320'])->once()->andReturn('sqlite::memory:'); |
|
| 20 | 20 | |
| 21 | 21 | $this->databaseConnection = new DatabaseConnection($databaseConnectionStringFactory); |
| 22 | - } |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - public function testCreateConnectionReturnsDatabaseConnection() |
|
| 25 | - { |
|
| 26 | - $dbInstance = $this->databaseConnection->createConnection('sqlite::memory:'); |
|
| 24 | + public function testCreateConnectionReturnsDatabaseConnection() |
|
| 25 | + { |
|
| 26 | + $dbInstance = $this->databaseConnection->createConnection('sqlite::memory:'); |
|
| 27 | 27 | |
| 28 | - $this->assertInstanceOf('PDO', $dbInstance); |
|
| 28 | + $this->assertInstanceOf('PDO', $dbInstance); |
|
| 29 | 29 | |
| 30 | - } |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - public function testGetInstanceReturnsCorrectInstance() |
|
| 33 | - { |
|
| 34 | - $dbInstance = DatabaseConnection::getInstance(); |
|
| 32 | + public function testGetInstanceReturnsCorrectInstance() |
|
| 33 | + { |
|
| 34 | + $dbInstance = DatabaseConnection::getInstance(); |
|
| 35 | 35 | |
| 36 | - $this->assertInstanceOf('Pyjac\ORM\DatabaseConnection', $dbInstance); |
|
| 36 | + $this->assertInstanceOf('Pyjac\ORM\DatabaseConnection', $dbInstance); |
|
| 37 | 37 | |
| 38 | - } |
|
| 38 | + } |
|
| 39 | 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); |
|
| 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 | 50 | |
| 51 | - $this->assertEquals($this->databaseConnection->getDefaultOptions(), $options); |
|
| 52 | - } |
|
| 51 | + $this->assertEquals($this->databaseConnection->getDefaultOptions(), $options); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - public function testTryAgainIfCausedByLostConnectionCreateNewConnectionWhenReasonForExceptionIsConnectionLoss() |
|
| 55 | - { |
|
| 56 | - $e = new \Exception("Error while sending"); |
|
| 57 | - $result = $this->invokeMethod($this->databaseConnection, 'tryAgainIfCausedByLostConnection', [$e, 'sqlite::memory:', '', '', []]); |
|
| 54 | + public function testTryAgainIfCausedByLostConnectionCreateNewConnectionWhenReasonForExceptionIsConnectionLoss() |
|
| 55 | + { |
|
| 56 | + $e = new \Exception("Error while sending"); |
|
| 57 | + $result = $this->invokeMethod($this->databaseConnection, 'tryAgainIfCausedByLostConnection', [$e, 'sqlite::memory:', '', '', []]); |
|
| 58 | 58 | |
| 59 | - $this->assertInstanceOf('PDO', $result); |
|
| 60 | - } |
|
| 59 | + $this->assertInstanceOf('PDO', $result); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 62 | + /** |
|
| 63 | 63 | * @expectedException \Exception |
| 64 | 64 | */ |
| 65 | - public function testTryAgainIfCausedByLostConnectionThrowsExceptionWhenReasonForExceptionIsNotConnectionLoss() |
|
| 66 | - { |
|
| 67 | - $e = new \Exception("PHP Rocks !!!"); |
|
| 68 | - $result = $this->invokeMethod($this->databaseConnection, 'tryAgainIfCausedByLostConnection', [$e, 'sqlite::memory:', '', '', []]); |
|
| 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 | - } |
|
| 65 | + public function testTryAgainIfCausedByLostConnectionThrowsExceptionWhenReasonForExceptionIsNotConnectionLoss() |
|
| 66 | + { |
|
| 67 | + $e = new \Exception("PHP Rocks !!!"); |
|
| 68 | + $result = $this->invokeMethod($this->databaseConnection, 'tryAgainIfCausedByLostConnection', [$e, 'sqlite::memory:', '', '', []]); |
|
| 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 | 90 | } |
| 91 | 91 | \ No newline at end of file |