| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Port\Tests\Reader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Port\Pdo\PdoReader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class PdoReaderTest extends \PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	public function testCount() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 		$this->assertEquals(100, $this->getReader()->count()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	public function testIterate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 		$i=1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 		foreach ($this->getReader() as $row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 			$this->assertInternalType('array', $row); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 			$this->assertEquals('user-'.$i, $row['username']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 			$i++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | 	public function testReaderRewindWorksCorrectly() | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | 		$reader = $this->getReader(); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | 		foreach ($reader as $row) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | 		foreach ($reader as $row) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	public function getConnection() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		$connection = new \PDO('sqlite::memory:'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		// Set error mode = exception for easy debugging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		$connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		// Build schema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		$connection->query('CREATE TABLE pdo_group ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 			id INTEGER PRIMARY KEY AUTOINCREMENT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 			name VARCHAR(45) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		$connection->query('CREATE TABLE pdo_user ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 			id INTEGER PRIMARY KEY AUTOINCREMENT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			username VARCHAR(32), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 			group_id INTEGER, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 			FOREIGN KEY(group_id) REFERENCES pdo_group(id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$connection->query('CREATE UNIQUE INDEX user_username ON pdo_user(username)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		return $connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	protected function getReader() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		$connection = $this->getConnection(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$group_insert = $connection->prepare('INSERT INTO pdo_group (name) VALUES (:name)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		$user_insert  = $connection->prepare('INSERT INTO pdo_user (username, group_id) VALUES (:username, :group)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		$counter = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		for ($i = 1; $i <= 10; $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			$group_insert->execute(array(':name' => "name {$i}")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			$id = $connection->lastInsertId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			for ($j = 1; $j <= 10; $j++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 				$user_insert->execute(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 					':username'  => "user-{$counter}", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 					':group' => $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 				)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 				$counter++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		return new PdoReader($connection, 'SELECT u.id, u.username, g.name FROM `pdo_user` u INNER JOIN `pdo_group` g ON u.group_id = g.id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  | 
            
                        
This check looks for
foreachloops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.Consider removing the loop.