[Mailer] added a way to test the number of queued emails

This commit is contained in:
Fabien Potencier 2019-08-16 08:47:38 +02:00
parent 940eabb121
commit afb1c04c35
5 changed files with 43 additions and 9 deletions

View File

@ -25,6 +25,11 @@ trait MailerAssertionsTrait
self::assertThat(self::getMessageMailerEvents(), new MailerConstraint\EmailCount($count, $transport), $message);
}
public static function assertQueuedEmailCount(int $count, string $transport = null, string $message = ''): void
{
self::assertThat(self::getMessageMailerEvents(), new MailerConstraint\EmailCount($count, $transport, true), $message);
}
public static function assertEmailIsQueued(MessageEvent $event, string $message = ''): void
{
self::assertThat($event, new MailerConstraint\EmailIsQueued(), $message);

View File

@ -3,6 +3,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FullStack;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\SentMessage;
@ -71,9 +72,19 @@ class MailerTest extends AbstractWebTestCase
$client->request('GET', '/send_email');
$this->assertEmailCount(2);
$this->assertEmailIsQueued($this->getMailerEvent(0));
$first = 0;
$second = 1;
if (!class_exists(FullStack::class)) {
$this->assertQueuedEmailCount(2);
$first = 1;
$second = 3;
$this->assertEmailIsQueued($this->getMailerEvent(0));
$this->assertEmailIsQueued($this->getMailerEvent(2));
}
$this->assertEmailIsNotQueued($this->getMailerEvent($first));
$this->assertEmailIsNotQueued($this->getMailerEvent($second));
$email = $this->getMailerMessage(0);
$email = $this->getMailerMessage($first);
$this->assertEmailHasHeader($email, 'To');
$this->assertEmailHeaderSame($email, 'To', 'fabien@symfony.com');
$this->assertEmailHeaderNotSame($email, 'To', 'helene@symfony.com');
@ -83,7 +94,7 @@ class MailerTest extends AbstractWebTestCase
$this->assertEmailHtmlBodyNotContains($email, 'Bar');
$this->assertEmailAttachementCount($email, 1);
$email = $this->getMailerMessage(1);
$email = $this->getMailerMessage($second);
$this->assertEmailAddressContains($email, 'To', 'fabien@symfony.com');
$this->assertEmailAddressContains($email, 'To', 'thomas@symfony.com');
$this->assertEmailAddressContains($email, 'Reply-To', 'me@symfony.com');

View File

@ -54,7 +54,7 @@ class Mailer implements MailerInterface
throw new TransportException('Cannot send message without a valid envelope.', 0, $e);
}
}
$event = new MessageEvent($message, $envelope, $this->transport->getName());
$event = new MessageEvent($message, $envelope, $this->transport->getName(), true);
$this->dispatcher->dispatch($event);
}

View File

@ -18,11 +18,13 @@ final class EmailCount extends Constraint
{
private $expectedValue;
private $transport;
private $queued;
public function __construct(int $expectedValue, string $transport = null)
public function __construct(int $expectedValue, string $transport = null, bool $queued = false)
{
$this->expectedValue = $expectedValue;
$this->transport = $transport;
$this->queued = $queued;
}
/**
@ -30,7 +32,7 @@ final class EmailCount extends Constraint
*/
public function toString(): string
{
return sprintf('%shas sent "%d" emails', $this->transport ? $this->transport.' ' : '', $this->expectedValue);
return sprintf('%shas %s "%d" emails', $this->transport ? $this->transport.' ' : '', $this->queued ? 'queued' : 'sent', $this->expectedValue);
}
/**
@ -40,7 +42,7 @@ final class EmailCount extends Constraint
*/
protected function matches($events): bool
{
return $this->expectedValue === \count($events->getEvents($this->transport));
return $this->expectedValue === $this->countEmails($events);
}
/**
@ -50,6 +52,22 @@ final class EmailCount extends Constraint
*/
protected function failureDescription($events): string
{
return sprintf('the Transport %s (%d sent)', $this->toString(), \count($events->getEvents($this->transport)));
return sprintf('the Transport %s (%d %s)', $this->toString(), $this->countEmails($events), $this->queued ? 'queued' : 'sent');
}
private function countEmails(MessageEvents $events): int
{
$count = 0;
foreach ($events->getEvents($this->transport) as $event) {
if (
($this->queued && $event->isQueued())
||
(!$this->queued && !$event->isQueued())
) {
++$count;
}
}
return $count;
}
}

View File

@ -67,7 +67,7 @@ abstract class AbstractTransport implements TransportInterface
}
}
$event = new MessageEvent($message, $envelope, $this->getName(), true);
$event = new MessageEvent($message, $envelope, $this->getName());
$this->dispatcher->dispatch($event);
$envelope = $event->getEnvelope();
if (!$envelope->getRecipients()) {