| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Doctrine\Bundle\DoctrineBundle\Tests\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Console\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Tester\CommandTester; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class DropDatabaseDoctrineTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     public function testExecute() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         $connectionName = 'default'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         $dbName         = 'test'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $params         = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |             'url' => 'sqlite:///' . sys_get_temp_dir() . '/test.db', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             'path' => sys_get_temp_dir() . '/' . $dbName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             'driver' => 'pdo_sqlite', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $application = new Application(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $application->add(new DropDatabaseDoctrineCommand()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $command = $application->find('doctrine:database:drop'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $command->setContainer($this->getMockContainer($connectionName, $params)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $commandTester = new CommandTester($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $commandTester->execute( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             array_merge(['command' => $command->getName(), '--force' => true]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->assertContains( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 'Dropped database %s for connection named %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 sys_get_temp_dir() . '/' . $dbName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 $connectionName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $commandTester->getDisplay() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     public function testExecuteWithoutOptionForceWillFailWithAttentionMessage() | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $connectionName = 'default'; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $dbName         = 'test'; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $params         = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             'path' => sys_get_temp_dir() . '/' . $dbName, | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             'driver' => 'pdo_sqlite', | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $application = new Application(); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         $application->add(new DropDatabaseDoctrineCommand()); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $command = $application->find('doctrine:database:drop'); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $command->setContainer($this->getMockContainer($connectionName, $params)); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $commandTester = new CommandTester($command); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $commandTester->execute( | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             array_merge(['command' => $command->getName()]) | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         $this->assertContains( | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                 'Would drop the database %s for connection named %s.', | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                 sys_get_temp_dir() . '/' . $dbName, | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                 $connectionName | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             ), | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             $commandTester->getDisplay() | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         $this->assertContains('Please run the operation with --force to execute', $commandTester->getDisplay()); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param string     $connectionName Connection name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param array|null $params         Connection parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @return \PHPUnit_Framework_MockObject_MockObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 79 |  | View Code Duplication |     private function getMockContainer($connectionName, $params = null) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         // Mock the container and everything you'll need here | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $mockDoctrine = $this->getMockBuilder('Doctrine\Common\Persistence\ConnectionRegistry') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $mockDoctrine->expects($this->any()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             ->method('getDefaultConnectionName') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             ->withAnyParameters() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             ->willReturn($connectionName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $mockConnection = $this->getMockBuilder('Doctrine\DBAL\Connection') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             ->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             ->setMethods(['getParams']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             ->getMockForAbstractClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $mockConnection->expects($this->any()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             ->method('getParams') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             ->withAnyParameters() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             ->willReturn($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $mockDoctrine->expects($this->any()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             ->method('getConnection') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             ->withAnyParameters() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             ->willReturn($mockConnection); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $mockContainer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             ->setMethods(['get']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $mockContainer->expects($this->any()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             ->method('get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             ->with('doctrine') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             ->willReturn($mockDoctrine); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         return $mockContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |  | 
            
                        
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.