| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace TestsDb\AnyDataset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ByJG\AnyDataset\DbDriverInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | // backward compatibility | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | if (!class_exists('\PHPUnit\Framework\TestCase')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | abstract class BasePdo extends \PHPUnit\Framework\TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var DbDriverInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $dbDriver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->createInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->createDatabase(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $this->populateData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected function createInstance() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         throw new \Exception('Implement createInstance method'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected function populateData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         //insert some data... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $array = $this->allData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         foreach ($array as $param) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             $this->dbDriver->execute( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 "INSERT INTO Dogs (Breed, Name, Age) VALUES (:breed, :name, :age);", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 $param | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     abstract protected function createDatabase(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     abstract protected function deleteDatabase(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function tearDown() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->deleteDatabase(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     protected function allData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 'breed' => 'Mutt', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 'name' => 'Spyke', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 'age' => 8, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 'id' => 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 'breed' => 'Brazilian Terrier', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 'name' => 'Sandy', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 'age' => 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 'id' => 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 'breed' => 'Pinscher', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 'name' => 'Lola', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 'age' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 'id' => 3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function testGetIterator() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $array = $this->allData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         // Step 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $iterator = $this->dbDriver->getIterator('select * from Dogs'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->assertEquals($array, $iterator->toArray()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         // Step 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $iterator = $this->dbDriver->getIterator('select * from Dogs'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $i = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         foreach ($iterator as $singleRow) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $this->assertEquals($array[$i++], $singleRow->toArray()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         // Step 3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $iterator = $this->dbDriver->getIterator('select * from Dogs'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $i = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         while ($iterator->hasNext()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $singleRow = $iterator->moveNext(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $this->assertEquals($array[$i++], $singleRow->toArray()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     public function testExecuteAndGetId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $idInserted = $this->dbDriver->executeAndGetId( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             "INSERT INTO Dogs (Breed, Name, Age) VALUES ('Cat', 'Doris', 7);" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $this->assertEquals(4, $idInserted); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function testGetAllFields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $allFields = $this->dbDriver->getAllFields('Dogs'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 'id', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 'breed', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 'name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 'age' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $allFields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |     public function testMultipleRowset() | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |         if (!$this->dbDriver->isSupportMultRowset()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |             $this->markTestSkipped('This DbDriver does not have this method'); | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |         $sql = "INSERT INTO Dogs (Breed, Name, Age) VALUES ('Cat', 'Doris', 7); " . | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |             "INSERT INTO Dogs (Breed, Name, Age) VALUES ('Dog', 'Lolla', 1); "; | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |         $idInserted = $this->dbDriver->executeAndGetId($sql); | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |         $this->assertEquals(5, $idInserted); | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |         $this->assertEquals( | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |             'Doris', | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |             $this->dbDriver->getScalar('select name from Dogs where Id = :id', ['id' => 4]) | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |         $this->assertEquals( | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |             'Lolla', | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |             $this->dbDriver->getScalar('select name from Dogs where Id = :id', ['id' => 5]) | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     public function testParameterInsideQuotes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $sql = "INSERT INTO Dogs (Breed, Name, Age) VALUES ('Cat', 'a:Doris', 7); "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $id = $this->dbDriver->executeAndGetId($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $this->assertEquals(4, $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $sql = "select id from Dogs where name = 'a:Doris'"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $id = $this->dbDriver->getScalar($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $this->assertEquals(4, $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 160 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |  | 
            
                        
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.