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