Add PHPUnit tests with coverage for all src classes
- 21 test files covering controllers, services, entities, enums, messages - CI: add test job with Xdebug coverage (clover + text) - SonarQube: configure coverage report path and test sources Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
77
tests/Service/MailerServiceTest.php
Normal file
77
tests/Service/MailerServiceTest.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Service;
|
||||
|
||||
use App\Service\MailerService;
|
||||
use App\Service\UnsubscribeManager;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
class MailerServiceTest extends TestCase
|
||||
{
|
||||
private MessageBusInterface $bus;
|
||||
private UnsubscribeManager $unsubscribeManager;
|
||||
private EntityManagerInterface $em;
|
||||
private UrlGeneratorInterface $urlGenerator;
|
||||
private MailerService $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->bus = $this->createMock(MessageBusInterface::class);
|
||||
$this->unsubscribeManager = $this->createMock(UnsubscribeManager::class);
|
||||
$this->em = $this->createMock(EntityManagerInterface::class);
|
||||
$this->urlGenerator = $this->createMock(UrlGeneratorInterface::class);
|
||||
|
||||
$this->service = new MailerService(
|
||||
$this->bus,
|
||||
sys_get_temp_dir(),
|
||||
'passphrase',
|
||||
$this->urlGenerator,
|
||||
$this->unsubscribeManager,
|
||||
$this->em,
|
||||
);
|
||||
}
|
||||
|
||||
public function testSendEmailSkipsUnsubscribedRecipient(): void
|
||||
{
|
||||
$this->unsubscribeManager->method('isUnsubscribed')->willReturn(true);
|
||||
$this->bus->expects(self::never())->method('dispatch');
|
||||
|
||||
$this->service->sendEmail('user@example.com', 'Subject', '<p>Body</p>');
|
||||
}
|
||||
|
||||
public function testSendEmailDoesNotSkipWhitelistedAddress(): void
|
||||
{
|
||||
$this->unsubscribeManager->method('isUnsubscribed')->willReturn(true);
|
||||
$this->urlGenerator->method('generate')->willReturn('https://example.com/track/abc');
|
||||
$this->em->expects(self::once())->method('persist');
|
||||
$this->em->expects(self::once())->method('flush');
|
||||
$this->bus->expects(self::once())->method('dispatch')->willReturn(new Envelope(new \stdClass()));
|
||||
|
||||
$this->service->sendEmail('contact@e-cosplay.fr', 'Subject', '<p>Body</p>');
|
||||
}
|
||||
|
||||
public function testSendEmailDispatchesForNonUnsubscribedUser(): void
|
||||
{
|
||||
$this->unsubscribeManager->method('isUnsubscribed')->willReturn(false);
|
||||
$this->unsubscribeManager->method('generateToken')->willReturn('token123');
|
||||
$this->urlGenerator->method('generate')->willReturn('https://example.com/url');
|
||||
$this->em->expects(self::once())->method('persist');
|
||||
$this->em->expects(self::once())->method('flush');
|
||||
$this->bus->expects(self::once())->method('dispatch')->willReturn(new Envelope(new \stdClass()));
|
||||
|
||||
$this->service->sendEmail('user@example.com', 'Test', '<p>Content</p>');
|
||||
}
|
||||
|
||||
public function testSendEmailWithoutUnsubscribeHeaders(): void
|
||||
{
|
||||
$this->urlGenerator->method('generate')->willReturn('https://example.com/url');
|
||||
$this->em->expects(self::once())->method('persist');
|
||||
$this->bus->expects(self::once())->method('dispatch')->willReturn(new Envelope(new \stdClass()));
|
||||
|
||||
$this->service->sendEmail('user@example.com', 'Test', '<p>Content</p>', withUnsubscribe: false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user