bus = $this->createStub(MessageBusInterface::class); $this->projectDir = sys_get_temp_dir() . '/mailer_test_' . uniqid(); mkdir($this->projectDir); $this->urlGenerator = $this->createStub(UrlGeneratorInterface::class); $this->unsubscribeManager = $this->createStub(UnsubscribeManager::class); $this->em = $this->createStub(EntityManagerInterface::class); $this->service = new MailerService( $this->bus, $this->projectDir, 'passphrase', 'admin@example.com', $this->urlGenerator, $this->unsubscribeManager, $this->em ); } protected function tearDown(): void { $this->removeDir($this->projectDir); } private function removeDir(string $dir): void { if (!is_dir($dir)) return; $files = array_diff(scandir($dir), ['.', '..']); foreach ($files as $file) { $path = $dir . '/' . $file; is_dir($path) ? $this->removeDir($path) : unlink($path); } rmdir($dir); } public function testGetAdminEmail(): void { $this->assertEquals('admin@example.com', $this->service->getAdminEmail()); } public function testGetAdminFrom(): void { $this->assertEquals('Association E-Cosplay ', $this->service->getAdminFrom()); } public function testSendEmail(): void { $this->urlGenerator->method('generate')->willReturn('http://track'); $this->unsubscribeManager->method('isUnsubscribed')->willReturn(false); $this->unsubscribeManager->method('generateToken')->willReturn('token'); $bus = $this->createMock(MessageBusInterface::class); $bus->expects($this->once()) ->method('dispatch') ->willReturn(new Envelope(new \stdClass())); $em = $this->createMock(EntityManagerInterface::class); $em->expects($this->once()) ->method('persist') ->with($this->isInstanceOf(EmailTracking::class)); $service = new MailerService($bus, $this->projectDir, 'p', 'admin@example.com', $this->urlGenerator, $this->unsubscribeManager, $em); $service->sendEmail('user@example.com', 'Subject', 'Content'); } public function testSendEmailWithAttachmentsAndReplyTo(): void { $filePath = $this->projectDir . '/test.txt'; file_put_contents($filePath, 'content'); $this->urlGenerator->method('generate')->willReturn('http://track'); $this->unsubscribeManager->method('isUnsubscribed')->willReturn(false); $bus = $this->createMock(MessageBusInterface::class); $bus->expects($this->once()) ->method('dispatch') ->willReturn(new Envelope(new \stdClass())); $service = new MailerService($bus, $this->projectDir, 'p', 'a@e.com', $this->urlGenerator, $this->unsubscribeManager, $this->em); $service->sendEmail( 'user@example.com', 'Subject', 'Content', null, 'reply@example.com', true, [['path' => $filePath, 'name' => 'custom.txt']] ); } public function testSendEmailToWhitelistedAdmin(): void { $this->urlGenerator->method('generate')->willReturn('http://track'); $bus = $this->createMock(MessageBusInterface::class); $bus->expects($this->once())->method('dispatch')->willReturn(new Envelope(new \stdClass())); $unsubscribeManager = $this->createMock(UnsubscribeManager::class); $unsubscribeManager->expects($this->never())->method('isUnsubscribed'); $service = new MailerService($bus, $this->projectDir, 'p', 'admin@example.com', $this->urlGenerator, $unsubscribeManager, $this->em); $service->sendEmail('admin@example.com', 'Subject', 'Content'); } public function testSendEmailDoesNotSendIfUnsubscribed(): void { $this->unsubscribeManager->method('isUnsubscribed')->willReturn(true); $bus = $this->createMock(MessageBusInterface::class); $bus->expects($this->never())->method('dispatch'); $service = new MailerService($bus, $this->projectDir, 'p', 'a@e.com', $this->urlGenerator, $this->unsubscribeManager, $this->em); $service->sendEmail('user@example.com', 'Subject', 'Content'); } public function testSendWithoutCertFiles(): void { $email = (new Email())->from('a@e.com')->to('b@e.com')->subject('S')->html('C'); $bus = $this->createMock(MessageBusInterface::class); $bus->expects($this->once())->method('dispatch')->willReturn(new Envelope(new \stdClass())); $service = new MailerService($bus, $this->projectDir, 'p', 'a@e.com', $this->urlGenerator, $this->unsubscribeManager, $this->em); $service->send($email); } public function testSendWithPublicKey(): void { touch($this->projectDir . '/key.asc'); $email = (new Email())->from('a@e.com')->to('b@e.com')->subject('S')->html('C'); $bus = $this->createMock(MessageBusInterface::class); $bus->expects($this->once())->method('dispatch')->willReturn(new Envelope(new \stdClass())); $service = new MailerService($bus, $this->projectDir, 'p', 'a@e.com', $this->urlGenerator, $this->unsubscribeManager, $this->em); $service->send($email); $this->assertCount(1, $email->getAttachments()); } // --- addUnsubscribeHeaders (exercised through sendEmail with non-admin, non-unsubscribed) --- public function testSendEmailAddsUnsubscribeHeadersForNonAdmin(): void { $this->urlGenerator->method('generate')->willReturn('http://track'); $this->unsubscribeManager->method('isUnsubscribed')->willReturn(false); $this->unsubscribeManager->method('generateToken')->willReturn('mytoken'); $bus = $this->createMock(MessageBusInterface::class); $bus->expects($this->once()) ->method('dispatch') ->willReturnCallback(function ($envelope) { return new Envelope(new \stdClass()); }); $service = new MailerService($bus, $this->projectDir, 'p', 'admin@e.com', $this->urlGenerator, $this->unsubscribeManager, $this->em); $service->sendEmail('other@example.com', 'Subject', 'Content'); // No assertion needed beyond no exception; dispatch was called once $this->addToAssertionCount(1); } // --- generateVcf (exercised through sendEmail — VCF attached) --- public function testSendEmailAttachesVcf(): void { $this->urlGenerator->method('generate')->willReturn('http://track'); $this->unsubscribeManager->method('isUnsubscribed')->willReturn(false); $this->unsubscribeManager->method('generateToken')->willReturn('token'); $capturedEmail = null; $bus = $this->createStub(MessageBusInterface::class); $bus->method('dispatch')->willReturnCallback(function ($msg) use (&$capturedEmail) { if ($msg instanceof \Symfony\Component\Mailer\Messenger\SendEmailMessage) { $capturedEmail = $msg->getMessage(); } return new Envelope(new \stdClass()); }); $service = new MailerService($bus, $this->projectDir, 'p', 'admin@e.com', $this->urlGenerator, $this->unsubscribeManager, $this->em); $service->sendEmail('other@example.com', 'Subject', 'Content'); // VCF was attached and then cleaned up; the email should have had it during dispatch $this->assertNotNull($capturedEmail); } // --- formatFileSize (exercised via injectAttachmentsList which is called when attachments present) --- public function testSendEmailFormatFileSizeBytes(): void { // A tiny attachment: < 1024 bytes => formatFileSize returns "X o" $filePath = $this->projectDir . '/tiny.txt'; file_put_contents($filePath, 'ab'); // 2 bytes $this->urlGenerator->method('generate')->willReturn('http://track'); $this->unsubscribeManager->method('isUnsubscribed')->willReturn(false); $html = 'footer'; $bus = $this->createMock(MessageBusInterface::class); $bus->expects($this->once())->method('dispatch')->willReturn(new Envelope(new \stdClass())); $service = new MailerService($bus, $this->projectDir, 'p', 'admin@e.com', $this->urlGenerator, $this->unsubscribeManager, $this->em); $service->sendEmail('other@example.com', 'Subject', $html, null, null, false, [['path' => $filePath, 'name' => 'tiny.txt']]); $this->addToAssertionCount(1); } public function testSendEmailFormatFileSizeKilobytes(): void { // > 1024 bytes => formatFileSize returns "X Ko" $filePath = $this->projectDir . '/medium.txt'; file_put_contents($filePath, str_repeat('a', 2048)); // 2 KB $this->urlGenerator->method('generate')->willReturn('http://track'); $this->unsubscribeManager->method('isUnsubscribed')->willReturn(false); $bus = $this->createMock(MessageBusInterface::class); $bus->expects($this->once())->method('dispatch')->willReturn(new Envelope(new \stdClass())); $service = new MailerService($bus, $this->projectDir, 'p', 'admin@e.com', $this->urlGenerator, $this->unsubscribeManager, $this->em); $service->sendEmail('other@example.com', 'Subject', 'C', null, null, false, [['path' => $filePath]]); $this->addToAssertionCount(1); } public function testSendEmailFormatFileSizeMegabytes(): void { // > 1048576 bytes => formatFileSize returns "X,X Mo" $filePath = $this->projectDir . '/large.txt'; file_put_contents($filePath, str_repeat('a', 1048577)); // just over 1MB $this->urlGenerator->method('generate')->willReturn('http://track'); $this->unsubscribeManager->method('isUnsubscribed')->willReturn(false); $bus = $this->createMock(MessageBusInterface::class); $bus->expects($this->once())->method('dispatch')->willReturn(new Envelope(new \stdClass())); $service = new MailerService($bus, $this->projectDir, 'p', 'admin@e.com', $this->urlGenerator, $this->unsubscribeManager, $this->em); $service->sendEmail('other@example.com', 'Subject', 'C', null, null, false, [['path' => $filePath]]); $this->addToAssertionCount(1); } public function testSendEmailExcludesAscAndSmimeAttachmentsFromList(): void { $ascPath = $this->projectDir . '/key.asc'; touch($ascPath); $this->urlGenerator->method('generate')->willReturn('http://track'); $this->unsubscribeManager->method('isUnsubscribed')->willReturn(false); $bus = $this->createMock(MessageBusInterface::class); $bus->expects($this->once())->method('dispatch')->willReturn(new Envelope(new \stdClass())); $service = new MailerService($bus, $this->projectDir, 'p', 'admin@e.com', $this->urlGenerator, $this->unsubscribeManager, $this->em); // Only the .asc file — filtered list will be empty, so HTML unchanged $service->sendEmail('other@example.com', 'Subject', 'C', null, null, false, [['path' => $ascPath, 'name' => 'key.asc']]); $this->addToAssertionCount(1); } public function testSendEmailInjectsAttachmentBeforeFooter(): void { $filePath = $this->projectDir . '/doc.pdf'; file_put_contents($filePath, '%PDF-test'); $this->urlGenerator->method('generate')->willReturn('http://track'); $this->unsubscribeManager->method('isUnsubscribed')->willReturn(false); // HTML with the footer dark marker $html = '
before
footer
'; $bus = $this->createMock(MessageBusInterface::class); $bus->expects($this->once())->method('dispatch')->willReturn(new Envelope(new \stdClass())); $service = new MailerService($bus, $this->projectDir, 'p', 'admin@e.com', $this->urlGenerator, $this->unsubscribeManager, $this->em); $service->sendEmail('other@example.com', 'Subject', $html, null, null, false, [['path' => $filePath, 'name' => 'doc.pdf']]); $this->addToAssertionCount(1); } }