| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Azine\MailgunWebhooksBundle\Tests\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Azine\MailgunWebhooksBundle\Command\DeleteOldEntriesCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Console\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Console\Tester\CommandTester; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class DeleteOldEntriesCommandTest extends \PHPUnit\Framework\TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     private $mailgunServiceMock; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     public function setUp() | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |         $this->mailgunServiceMock = $this->getMockBuilder("Azine\MailgunWebhooksBundle\Services\AzineMailgunService") | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |             ->disableOriginalConstructor() | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function testHelpInfo() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $application = new Application(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $application->add(new DeleteOldEntriesCommand($this->mailgunServiceMock)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $command = $this->getDeleteOldEntriesCommand($application); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $display = $command->getHelp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->assertContains('Mailgun accepted the request to send/forward the email and the message has been placed in queue.', $display); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function testDeleteOldEntriesWithoutParams() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $application = new Application(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $application->add(new DeleteOldEntriesCommand($this->mailgunServiceMock)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $command = $this->getDeleteOldEntriesCommand($application); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         self::$days = 60; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         self::$count = 14; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         self::$type = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->mailgunServiceMock->expects($this->once())->method('removeEvents')->will($this->returnCallback(array($this, 'removeEventsCallback'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $tester = new CommandTester($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $tester->execute(array('')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $display = $tester->getDisplay(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->assertContains('deleting entries of any type.', $display); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->assertContains("using default age-limit of '60 days ago'.", $display); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->assertContains('All MailgunEvents (& their CustomVariables & Attachments) older than', $display); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->assertContains('of any type have been deleted (14).', $display); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public static $days; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public static $count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public static $type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public static function removeEventsCallback($type, $date) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $checkDate = new \DateTime(self::$days.' days ago'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         self::assertSame(self::$type, $type, 'type null expected.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         self::assertSame($checkDate->format('Y-m-d H:i'), $date->format('Y-m-d H:i'), 'wrong date.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return self::$count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param Application $application | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return DeleteOldEntriesCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     private function getDeleteOldEntriesCommand($application) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return $application->find('mailgun:delete-events'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function testDeleteOldEntriesWithDate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $application = new Application(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $application->add(new DeleteOldEntriesCommand($this->mailgunServiceMock)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $command = $this->getDeleteOldEntriesCommand($application); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         self::$days = 21; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         self::$count = 11; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         self::$type = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->mailgunServiceMock->expects($this->once())->method('removeEvents')->will($this->returnCallback(array($this, 'removeEventsCallback'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $tester = new CommandTester($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $tester->execute(array('date' => '21 days ago')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $display = $tester->getDisplay(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $this->assertContains('deleting entries of any type.', $display); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $this->assertContains('All MailgunEvents (& their CustomVariables & Attachments) older than', $display); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $this->assertContains('of any type have been deleted (11).', $display); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public function testDeleteOldEntriesWithDateAndType() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $application = new Application(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $application->add(new DeleteOldEntriesCommand($this->mailgunServiceMock)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $command = $this->getDeleteOldEntriesCommand($application); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         self::$days = 33; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         self::$count = 77; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         self::$type = 'opened'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $this->mailgunServiceMock->expects($this->once())->method('removeEvents')->will($this->returnCallback(array($this, 'removeEventsCallback'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $tester = new CommandTester($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $tester->execute(array('date' => '33 days ago', 'type' => self::$type)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $display = $tester->getDisplay(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $this->assertContains('All MailgunEvents (& their CustomVariables & Attachments) older than', $display); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->assertContains("of type '".self::$type."' have been deleted (77).", $display); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @expectedException \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     public function testDeleteOldEntriesWithInvalidType() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $application = new Application(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $application->add(new DeleteOldEntriesCommand($this->mailgunServiceMock)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $command = $this->getDeleteOldEntriesCommand($application); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $tester = new CommandTester($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $tester->execute(array('date' => '33 days ago', 'type' => 'invalidType')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |  |