| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Azine\EmailBundle\Tests\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Azine\EmailBundle\Command\SendNewsLetterCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Console\Tester\CommandTester; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Console\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Process\Process; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @author dominik | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 12 |  | View Code Duplication | class SendNewsLetterCommandTest extends \PHPUnit_Framework_TestCase | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public function testHelpInfo() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $command = $this->getCommand(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         $display = $command->getHelp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $this->assertContains("Depending on you Swiftmailer-Configuration the email will be send directly or will be written to the spool.", $display); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function testSend() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $command = $this->getCommand(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $tester = new CommandTester($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $tester->execute(array('')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $display = $tester->getDisplay(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->assertContains(AzineNotifierServiceMock::EMAIL_COUNT." newsletter emails have been sent.", $display); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function testSendFail() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $command = $this->getCommand(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $tester = new CommandTester($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $tester->execute(array('')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $display = $tester->getDisplay(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->assertContains((AzineNotifierServiceMock::EMAIL_COUNT-1)." newsletter emails have been sent.", $display); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->assertContains(AzineNotifierServiceMock::FAILED_ADDRESS, $display); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @return SendNewsLetterCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     private function getCommand($fail = false){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $application = new Application(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $application->add(new SendNewsLetterCommand()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $command = $application->find('emails:sendNewsletter'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $command->setContainer($this->getMockSetup($fail)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         return $command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     private function getMockSetup($fail = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $containerMock = $this->getMockBuilder("Symfony\Component\DependencyInjection\ContainerInterface")->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $notifierServiceMock = new AzineNotifierServiceMock($fail); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $containerMock->expects($this->any())->method('get')->with('azine_email_notifier_service')->will($this->returnValue($notifierServiceMock)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return $containerMock; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |     public function testLockingFunctionality() | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         $commandName = $this->getCommand()->getName(); | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |         $reflector = new \ReflectionClass(\AppKernel::class); | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |         $appDirectory = dirname($reflector->getFileName()); | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |         // start commands in a separate processes | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         $process1 = new Process("php $appDirectory/console $commandName --env=test"); | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |         $process2 = new Process("php $appDirectory/console $commandName --env=test"); | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |         $process1->start(); | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |         $process2->start(); | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |         // wait until both processes have terminated | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |         while(!$process1->isTerminated() || !$process2->isTerminated()){ | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |             usleep(10); | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |         $this->assertContains('The command is already running in another process.', $process2->getOutput().$process1->getOutput()); | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |  | 
            
                        
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.