From 086b849cc11b81d5f6b6efa118aa60196e1ac83d Mon Sep 17 00:00:00 2001 From: Koen Eelen Date: Tue, 20 Aug 2024 14:40:23 +0200 Subject: [PATCH] add test --- tests/Mails/MailManagerTest.php | 164 ++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 tests/Mails/MailManagerTest.php diff --git a/tests/Mails/MailManagerTest.php b/tests/Mails/MailManagerTest.php new file mode 100644 index 000000000..de5633082 --- /dev/null +++ b/tests/Mails/MailManagerTest.php @@ -0,0 +1,164 @@ +mailer = $this->createMock(Mailer::class); + $integrationRepository = $this->createMock(IntegrationRepository::class); + + $this->mailManager = new MailManager( + $this->mailer, + $integrationRepository, + self::TEMPLATE_ACTIVATED_ID, + self::TEMPLATE_BLOCKED_ID, + 'http://www.example.com/' + ); + + // @todo Let add all types of contacts here, it might be the case that we only sent the email to specific type of contacts + $this->contacts = [ + new Contact( + Uuid::uuid4(), + Uuid::fromString(self::INTEGRATION_ID), + 'grote.smurf@publiq.be', + ContactType::Technical, + 'Grote', + 'Smurf' + ), + new Contact( + Uuid::uuid4(), + Uuid::fromString(self::INTEGRATION_ID), + 'brilsmurf@publiq.be', + ContactType::Functional, + 'Bril', + 'Smurf' + ), + new Contact( + Uuid::uuid4(), + Uuid::fromString(self::INTEGRATION_ID), + 'knutselsmurf@publiq.be', + ContactType::Contributor, + 'Knutsel', + 'Smurf' + ), + ]; + + $integration = (new Integration( + Uuid::fromString(self::INTEGRATION_ID), + IntegrationType::SearchApi, + 'Mock Integration', + 'Mock description', + Uuid::uuid4(), + IntegrationStatus::Active, + IntegrationPartnerStatus::THIRD_PARTY, + )) + ->withContacts(...$this->contacts); + + $integrationRepository + ->expects($this->once()) + ->method('getById') + ->with(self::INTEGRATION_ID) + ->willReturn($integration); + } + + + /** + * @dataProvider mailDataProvider + */ + public function testSendMail( + object $event, + string $method, + int $templateId, + string $subject, + array $expectedParameters + ): void { + $this->mailer + ->expects($this->once()) + ->method('send') + ->with( + new Address(config('mail.from.address'), config('mail.from.name')), + $this->callback(function (Addresses $addresses) { + foreach ($addresses as $i => $address) { + if ($address->getAddress() !== $this->contacts[$i]->email) { + return false; + } + + if ($address->getName() !== $this->contacts[$i]->firstName . ' ' . $this->contacts[$i]->lastName) { + return false; + } + } + + return true; + }), + $templateId, + $subject, + $this->callback(function ($parameters) use ($expectedParameters) { + $this->assertEquals($expectedParameters, $parameters); + + return true; + }) + ); + + $this->mailManager->$method($event); + } + + public static function mailDataProvider(): array + { + return [ + 'IntegrationActivated' => [ + 'event' => new IntegrationActivated(Uuid::fromString(self::INTEGRATION_ID)), + 'method' => 'sendIntegrationActivatedMail', + 'templateId' => self::TEMPLATE_ACTIVATED_ID, + 'subject' => 'Publiq platform - Integration activated', + 'expectedParameters' => [ + 'firstName' => 'Publiq platform', + 'url' => 'http://www.example.com//nl/integraties/' . self::INTEGRATION_ID, + 'integrationName' => 'Mock Integration', + 'type' => 'search-api', + ], + ], + 'IntegrationBlocked' => [ + 'event' => new IntegrationBlocked(Uuid::fromString(self::INTEGRATION_ID)), + 'method' => 'sendIntegrationBlockedMail', + 'templateId' => self::TEMPLATE_BLOCKED_ID, + 'subject' => 'Publiq platform - Integration blocked', + 'expectedParameters' => [ + 'firstName' => 'Publiq platform', + 'integrationName' => 'Mock Integration', + ], + ], + ]; + } +}