| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Port\Tests\Writer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Port\Pdo\PdoWriter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class PdoWriterTest extends \PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @var \PDO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $pdo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $this->pdo = new \PDO('sqlite::memory:'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); //important | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $this->pdo->exec('DROP TABLE IF EXISTS `example`'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $this->pdo->exec('CREATE TABLE `example` (a TEXT, b TEXT)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function testValidWriteItem() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $writer = new PdoWriter($this->pdo, 'example'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $writer->prepare(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $writer->writeItem(array('a' => 'foo', 'b' => 'bar')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $writer->finish(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $stmnt = $this->pdo->query('SELECT * FROM `example`'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             array(array('a'=>'foo', 'b'=>'bar')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             $stmnt->fetchAll(\PDO::FETCH_ASSOC), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             'database does not contain expected row' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     public function testValidWriteMultiple() | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $writer = new PdoWriter($this->pdo, 'example'); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $writer->prepare(); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $writer->writeItem(array('a' => 'foo', 'b' => 'bar')); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $writer->writeItem(array('a' => 'cat', 'b' => 'dog')); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $writer->writeItem(array('a' => 'ac', 'b' => 'dc')); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $writer->finish(); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $stmnt = $this->pdo->query('SELECT * FROM `example`'); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $this->assertEquals( | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             array(array('a'=>'foo', 'b'=>'bar'), array('a'=>'cat', 'b'=>'dog'), array('a'=>'ac', 'b'=>'dc')), | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             $stmnt->fetchAll(\PDO::FETCH_ASSOC), | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             'database does not contain all expected rows' | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @expectedException \Port\Exception\WriterException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 57 |  | View Code Duplication |     public function testWriteTooManyValues() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $writer = new PdoWriter($this->pdo, 'example'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $writer->prepare(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $writer->writeItem(array('foo', 'bar', 'baz')); //expects two | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $writer->finish(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @expectedException \Port\Exception\WriterException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 68 |  | View Code Duplication |     public function testWriteToNonexistentTable() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $writer = new PdoWriter($this->pdo, 'foobar'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $writer->prepare(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $writer->writeItem(array('foo', 'bar')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $writer->finish(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * Tests PDO instance with silent errors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @expectedException \Port\Exception\WriterException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     public function testStatementCreateFailureWithNoException() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $writer = new PdoWriter($this->pdo, 'foob`ar'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $writer->prepare(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $writer->writeItem(array('foo', 'bar')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $writer->finish(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * Tests PDO instance with silent errors. First inert prepares the statement, second creates an exception. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @expectedException \Port\Exception\WriterException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public function testWriteFailureWithNoException() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $writer = new PdoWriter($this->pdo, 'example'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $writer->prepare(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $writer->writeItem(array('foo', 'bar')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $writer->writeItem(array('foo', 'bar', 'baz')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $writer->finish(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.