|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DotTest\MailOutlook; |
| 6 | + |
| 7 | +use Dot\Mail\Email; |
| 8 | +use Dot\Mail\Options\MailOptions; |
| 9 | +use Dot\Mail\Service\LogService; |
| 10 | +use Dot\Mail\Service\MailService; |
| 11 | +use Dot\MailOutlook\Authenticator\XOauth2OutlookAuthenticator; |
| 12 | +use Dot\MailOutlook\DotMailServiceDelegator; |
| 13 | +use Dot\MailOutlook\Service\XOauthTokenProviderService; |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\MockObject\Exception; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Psr\Container\ContainerExceptionInterface; |
| 18 | +use Psr\Container\ContainerInterface; |
| 19 | +use Psr\Container\NotFoundExceptionInterface; |
| 20 | +use ReflectionClass; |
| 21 | +use Symfony\Component\Mailer\Transport\SendmailTransport; |
| 22 | +use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport; |
| 23 | + |
| 24 | +#[CoversClass(DotMailServiceDelegator::class)] |
| 25 | +class DotMailServiceDelegatorTest extends TestCase |
| 26 | +{ |
| 27 | + private EsmtpTransport $esmtpTransport; |
| 28 | + private MailService $mailService; |
| 29 | + |
| 30 | + protected function setUp(): void |
| 31 | + { |
| 32 | + $logService = $this->createMock(LogService::class); |
| 33 | + $emailMessage = $this->createMock(Email::class); |
| 34 | + $mailOptions = $this->createMock(MailOptions::class); |
| 35 | + $this->esmtpTransport = new EsmtpTransport(); |
| 36 | + $this->mailService = new MailService($logService, $emailMessage, $this->esmtpTransport, $mailOptions); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @throws ContainerExceptionInterface |
| 41 | + * @throws NotFoundExceptionInterface |
| 42 | + * @throws Exception |
| 43 | + */ |
| 44 | + public function testWillSetXOauthOutlookAuthenticatorInDotMailServiceInstance(): void |
| 45 | + { |
| 46 | + $tokenProvider = $this->createMock(XOauthTokenProviderService::class); |
| 47 | + $authenticator = new XOauth2OutlookAuthenticator($tokenProvider); |
| 48 | + $container = $this->createMock(ContainerInterface::class); |
| 49 | + |
| 50 | + $container->expects($this->once())->method('get')->willReturn($authenticator); |
| 51 | + |
| 52 | + $delegatorReturn = (new DotMailServiceDelegator())( |
| 53 | + $container, |
| 54 | + '', |
| 55 | + fn () => $this->mailService |
| 56 | + ); |
| 57 | + |
| 58 | + $this->assertInstanceOf(EsmtpTransport::class, $this->mailService->getTransport()); |
| 59 | + |
| 60 | + $reflection = new ReflectionClass($this->esmtpTransport); |
| 61 | + $reflectionProperty = $reflection->getProperty('authenticators')->getValue($this->esmtpTransport); |
| 62 | + |
| 63 | + $this->assertContainsOnlyInstancesOf(XOauth2OutlookAuthenticator::class, $reflectionProperty); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @throws ContainerExceptionInterface |
| 68 | + * @throws NotFoundExceptionInterface |
| 69 | + * @throws Exception |
| 70 | + */ |
| 71 | + public function testWillNotSetXOauthOutlookAuthenticatorOnInvalidTransport(): void |
| 72 | + { |
| 73 | + $tokenProvider = $this->createMock(XOauthTokenProviderService::class); |
| 74 | + $authenticator = new XOauth2OutlookAuthenticator($tokenProvider); |
| 75 | + $container = $this->createMock(ContainerInterface::class); |
| 76 | + |
| 77 | + $container->expects($this->once())->method('get')->willReturn($authenticator); |
| 78 | + |
| 79 | + $this->mailService->setTransport(new SendmailTransport()); |
| 80 | + |
| 81 | + $delegatorReturn = (new DotMailServiceDelegator())( |
| 82 | + $container, |
| 83 | + '', |
| 84 | + fn () => $this->mailService |
| 85 | + ); |
| 86 | + |
| 87 | + $this->assertInstanceOf(SendmailTransport::class, $this->mailService->getTransport()); |
| 88 | + |
| 89 | + $reflection = new ReflectionClass($this->esmtpTransport); |
| 90 | + $reflectionProperty = $reflection->getProperty('authenticators')->getValue($this->esmtpTransport); |
| 91 | + |
| 92 | + $this->assertNotContains(XOauth2OutlookAuthenticator::class, $reflectionProperty); |
| 93 | + } |
| 94 | +} |
0 commit comments