| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace EcodevTests\Felix\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Cake\Chronos\Chronos; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Doctrine\ORM\EntityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Ecodev\Felix\Model\Message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Ecodev\Felix\Model\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Ecodev\Felix\Repository\MessageRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Ecodev\Felix\Service\Mailer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use EcodevTests\Felix\Log\InMemoryTransport; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | final class MailerTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private function createMockMailer(): Mailer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         /** @var EntityManager $entityManager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $entityManager = $this->createMock(EntityManager::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $transport = new InMemoryTransport(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $messageRepository = new class() implements MessageRepository { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             public function getAllMessageToSend(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $mailer = new Mailer( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             $entityManager, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             $messageRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             $transport, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             '/user/bin/php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             '[email protected]', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             'Epicerio' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         return $mailer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function testSendMessage(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $mailer = $this->createMockMailer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $message = $this->createMockMessage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->expectOutputRegex('~email from noreply@example\.com sent to: john\.doe@example\.com~'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $mailer->sendMessage($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         self::assertNotNull($message->getDateSent()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     private function createMockMessage(): Message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return new class() implements Message { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             private ?Chronos $dateSent = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             public function getSubject(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 return 'my subject'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             public function getBody(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 return 'my body'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             public function setDateSent(?Chronos $dateSent): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 $this->dateSent = $dateSent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             public function getEmail(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 return '[email protected]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             public function getRecipient(): ?User | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             public function getId(): ?int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             public function setSubject(string $subject): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             public function setBody(string $body): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             public function getDateSent(): ?Chronos | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 return $this->dateSent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             public function setEmail(string $email): void | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             public function getType(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 return 'my_type'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  |