| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use kalanis\EmailApi\Basics; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use kalanis\EmailApi\Exceptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use kalanis\EmailApi\Interfaces; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use kalanis\EmailApi\LocalInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use kalanis\EmailApi\Sending; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class SendingStopBeforeProcess extends LocalInfo\DefaultInfo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     public function beforeProcess(Interfaces\IContent $content, Interfaces\IEmailUser $to, ?Interfaces\IEmailUser $from = null): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         parent::beforeProcess($content, $to, $from); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         throw new Exceptions\EmailException('Catch on before process'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class SendingStopBeforeSend extends LocalInfo\DefaultInfo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function beforeSend(Interfaces\ISending $service, Interfaces\IContent $content): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         parent::beforeSend($service, $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         throw new Exceptions\EmailException('Catch on before send'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | class SendingStopResultSuccess extends LocalInfo\DefaultInfo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function whenResultIsSuccessful(Interfaces\ISending $service, Basics\Result $result): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         parent::whenResultIsSuccessful($service, $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         throw new Exceptions\EmailException('Catch on success send'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function whenSendFails(Interfaces\ISending $service, Exceptions\EmailException $ex): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         parent::whenSendFails($service, $ex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         throw $ex; // pass it through catch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | class SendingTest extends CommonTestClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function testCheck() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $lib = new Sending(new LocalInfo\DefaultInfo(), $this->mockServices(false)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->assertFalse($lib->canUseService(), 'There is no service by default'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->assertEquals(0, $lib->systemServiceId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $lib = new Sending(new LocalInfo\DefaultInfo(), $this->mockServices()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->assertTrue($lib->canUseService(), 'There is services'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @throws Exceptions\EmailException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function testSimple() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $lib = new Sending(new LocalInfo\DefaultInfo(), $this->mockServices()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $data = $lib->sendEmail($this->mockContent(), $this->mockUser()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->assertTrue($data->getStatus()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->assertEquals('Dummy service with check', $data->getData()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->assertNull($data->getRemoteId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @throws Exceptions\EmailException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function testProcessBefore() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $lib = new Sending(new SendingStopBeforeProcess(), $this->mockServices()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->expectException(Exceptions\EmailException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $lib->sendEmail($this->mockContent(), $this->mockUser()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $this->expectExceptionMessageMatches('Catch on before process'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @throws Exceptions\EmailException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function testBeforeSend() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $lib = new Sending(new SendingStopBeforeSend(), $this->mockServices()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $this->expectException(Exceptions\EmailException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $lib->sendEmail($this->mockContent(), $this->mockUser()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $this->expectExceptionMessageMatches('Catch on before send'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @throws Exceptions\EmailException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public function testProcessSuccess() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $lib = new Sending(new SendingStopResultSuccess(), $this->mockServices()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $this->expectException(Exceptions\EmailException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $lib->sendEmail($this->mockContent(), $this->mockUser()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $this->expectExceptionMessageMatches('Catch on success send'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     protected function mockServices(bool $withDummyService = true): LocalInfo\ServicesOrdering | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         $ordering = new LocalInfo\ServicesOrdering(); | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         if ($withDummyService) { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |             $ordering->addService(new DummyService()); | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         return $ordering; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |  |