| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Port\Dbal\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Port\Dbal\DbalReader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Doctrine\DBAL\Configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Doctrine\DBAL\Connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Doctrine\DBAL\DriverManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Doctrine\DBAL\Platforms\SqlitePlatform; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Doctrine\DBAL\Schema\Schema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class DbalReaderTest extends \PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public function testCalculateRowCount() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $reader = $this->getReader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $reader->setRowCountCalculated(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $this->assertTrue($reader->isRowCountCalculated()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $reader->setRowCountCalculated(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->assertFalse($reader->isRowCountCalculated()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $reader->setRowCountCalculated(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->assertTrue($reader->isRowCountCalculated()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public function testCount() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->assertEquals(10, $this->getReader()->count()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function testCountInhibited() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $reader = $this->getReader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $reader->setRowCountCalculated(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->assertEquals(null, $reader->count()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function testSqlAndParamsAreMutable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $reader = $this->getReader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $reader->setSql('SELECT * FROM groups WHERE id = :id', array('id' => 2)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->assertAttributeEquals('SELECT * FROM groups WHERE id = :id', 'sql', $reader); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->assertAttributeEquals(array('id' => 2), 'params', $reader); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function testChangeSqlOrParamsClearsNumRowsAndStatement() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $reader = $this->getReader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $reader->count(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $reader->current(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->assertAttributeNotEmpty('rowCount', $reader); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->assertAttributeNotEmpty('stmt', $reader); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $reader->setSql('SELECT * FROM `user` WHERE id IN (:id)', array('id' => array())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->assertAttributeEmpty('rowCount', $reader); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->assertAttributeEmpty('stmt', $reader); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function testIterate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $i=31; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         foreach ($this->getReader() as $key => $row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $this->assertInternalType('array', $row); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $this->assertEquals('user-'.$i, $row['username']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $this->assertEquals($i - 31, $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $i++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->assertEquals(41, $i); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     public function testReaderRewindWorksCorrectly() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $reader = $this->getReader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         foreach ($reader as $row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             if (!isset($row['username'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 $this->fail('There should be a username'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             if ($row['username'] == 'user-35') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $reader->rewind(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->assertEquals(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             'id' => 31, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             'username' => 'user-31', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             'name' => 'name 4', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         ), $reader->current()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public function testCallingCurrentTwiceShouldNotAdvance() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $reader = $this->getReader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $expected = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             'id' => 31, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             'username' => 'user-31', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             'name' => 'name 4', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $this->assertEquals($expected, $reader->current()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->assertEquals($expected, $reader->current()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |     public function testEmptyResultDoesNotThrowException() | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |         $reader = $this->getReader(); | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |         $reader->setSql(null, array('name' => 'unknown group')); | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         $this->assertFalse($reader->current()); | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     public function testCallValidRewindsIfNeeded() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $reader = $this->getReader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $this->assertTrue($reader->valid()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $this->assertAttributeInternalType('array', 'data', $reader); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     public function getConnection() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $params = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             'driver' => 'pdo_sqlite', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             'memory' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $connection = DriverManager::getConnection($params, new Configuration()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $schema = new Schema(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $table = $schema->createTable('groups'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $table->addColumn('id', 'integer', ['autoincrement' => true]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $table->addColumn('name', 'string', array('length' => 45)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $table->setPrimaryKey(array('id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $myTable = $schema->createTable('user'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $myTable->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $myTable->addColumn('username', 'string', array('length' => 32)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $myTable->addColumn('group_id', 'integer'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $myTable->setPrimaryKey(array('id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $myTable->addUniqueIndex(array('username')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $myTable->addForeignKeyConstraint($table, array('group_id'), array('id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         foreach ($schema->toSql(new SqlitePlatform()) as $query) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             $connection->query($query); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         return $connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     protected function getReader() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         $connection = $this->getConnection(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         $this->loadFixtures($connection); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         return new DbalReader($connection, implode(' ', array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             'SELECT u.id, u.username, g.name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             'FROM `user` u INNER JOIN groups g ON u.group_id = g.id', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             'WHERE g.name LIKE :name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         )), array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             'name' => 'name 4', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      * @param Connection $connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     protected function loadFixtures($connection) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         $counter = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         for ($i = 1; $i <= 10; $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |             $connection->insert('groups', array('name' => "name {$i}")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             $id = $connection->lastInsertId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             for ($j = 1; $j <= 10; $j++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |                 $connection->insert('user', array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |                     'username' => "user-{$counter}", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |                     'group_id' => $id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |                 )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |                 $counter++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 193 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 194 |  |  |  | 
            
                        
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.