| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Azine\MailgunWebhooksBundle\Tests\Services; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author Dominik Businger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Azine\MailgunWebhooksBundle\Services\AzineMailgunService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class AzineMailgunServiceTest extends \PHPUnit\Framework\TestCase | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     public function testRemoveOldEventEntries() | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |         $ageLimit = new \DateTime('5 days ago'); | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |         $count = 23; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         $q = $this->getMockBuilder("Azine\MailgunWebhooksBundle\Tests\Services\AzineQueryMock")->disableOriginalConstructor()->getMock(); | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |         $q->expects($this->once())->method('execute')->will($this->returnValue($count)); | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         $qb = $this->getMockBuilder("Doctrine\ORM\QueryBuilder")->disableOriginalConstructor()->getMock(); | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         $qb->expects($this->once())->method('delete')->with("Azine\MailgunWebhooksBundle\Entity\MailgunEvent", 'e')->will($this->returnValue($qb)); | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         $qb->expects($this->once())->method('andWhere')->with('e.timestamp < :age')->will($this->returnValue($qb)); | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         $qb->expects($this->once())->method('setParameter')->with('age', $ageLimit->getTimestamp())->will($this->returnValue($qb)); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         $qb->expects($this->once())->method('getQuery')->will($this->returnValue($q)); | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         $em = $this->getMockBuilder("Doctrine\ORM\EntityManager")->disableOriginalConstructor()->getMock(); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         $em->expects($this->once())->method('createQueryBuilder')->will($this->returnValue($qb)); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $registry = $this->getMockBuilder("Doctrine\Common\Persistence\ManagerRegistry")->disableOriginalConstructor()->getMock(); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         $registry->expects($this->once())->method('getManager')->will($this->returnValue($em)); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $amgs = new AzineMailgunService($registry); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $deleteCount = $amgs->removeOldEventEntries($ageLimit); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $this->assertSame($count, $deleteCount, "Expected that $count entries are reported as deleted."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function testRemoveOldEventEntriesByTypeWithString() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $ageLimit = new \DateTime('5 days ago'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $type = 'bounced'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $count = 12; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $q = $this->getMockBuilder("Azine\MailgunWebhooksBundle\Tests\Services\AzineQueryMock")->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $q->expects($this->once())->method('execute')->will($this->returnValue($count)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $qb = $this->getMockBuilder("Doctrine\ORM\QueryBuilder")->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $qb->expects($this->once())->method('delete')->with("Azine\MailgunWebhooksBundle\Entity\MailgunEvent", 'e')->will($this->returnValue($qb)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $qb->expects($this->exactly(2))->method('andWhere')->with()->will($this->returnValueMap(array(array('e.timestamp < :age', $qb), array('e.event = :type', $qb)))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $qb->expects($this->exactly(2))->method('setParameter')->will($this->returnValueMap(array(array('age', $ageLimit->getTimestamp(), null, $qb), array('type', $type, null, $qb)))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $qb->expects($this->once())->method('getQuery')->will($this->returnValue($q)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $em = $this->getMockBuilder("Doctrine\ORM\EntityManager")->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $em->expects($this->once())->method('createQueryBuilder')->will($this->returnValue($qb)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $registry = $this->getMockBuilder("Doctrine\Common\Persistence\ManagerRegistry")->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $registry->expects($this->once())->method('getManager')->will($this->returnValue($em)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $amgs = new AzineMailgunService($registry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $deleteCount = $amgs->removeEvents($type, $ageLimit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->assertSame($count, $deleteCount, "Expected that $count entries are reported as deleted."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     public function testRemoveOldEventEntriesByTypeWithArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $ageLimit = new \DateTime('5 days ago'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $type = array('bounced', 'dropped'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $count = 12; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $q = $this->getMockBuilder("Azine\MailgunWebhooksBundle\Tests\Services\AzineQueryMock")->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $q->expects($this->once())->method('execute')->will($this->returnValue($count)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $qb = $this->getMockBuilder("Doctrine\ORM\QueryBuilder")->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $qb->expects($this->once())->method('delete')->with("Azine\MailgunWebhooksBundle\Entity\MailgunEvent", 'e')->will($this->returnValue($qb)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $qb->expects($this->exactly(2))->method('andWhere')->with()->will($this->returnValueMap(array(array('e.timestamp < :age', $qb), array('e.event in (:type)', $qb)))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $qb->expects($this->exactly(2))->method('setParameter')->will($this->returnValueMap(array(array('age', $ageLimit->getTimestamp(), null, $qb), array('type', $type, null, $qb)))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $qb->expects($this->once())->method('getQuery')->will($this->returnValue($q)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $em = $this->getMockBuilder("Doctrine\ORM\EntityManager")->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $em->expects($this->once())->method('createQueryBuilder')->will($this->returnValue($qb)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $registry = $this->getMockBuilder("Doctrine\Common\Persistence\ManagerRegistry")->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $registry->expects($this->once())->method('getManager')->will($this->returnValue($em)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $amgs = new AzineMailgunService($registry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $deleteCount = $amgs->removeEvents($type, $ageLimit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $this->assertSame($count, $deleteCount, "Expected that $count entries are reported as deleted."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  |