Skip to content

[Mailer] Add support for the queued flag in the EmailCount assertion #33203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public static function assertEmailCount(int $count, string $transport = null, st
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);
Expand Down
19 changes: 15 additions & 4 deletions src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,9 +72,19 @@ public function testMailerAssertions()
$client->request('GET', '/send_email');

$this->assertEmailCount(2);
$this->assertEmailIsQueued($this->getMailerEvent(0));

$email = $this->getMailerMessage(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($first);
$this->assertEmailHasHeader($email, 'To');
$this->assertEmailHeaderSame($email, 'To', 'fabien@symfony.com');
$this->assertEmailHeaderNotSame($email, 'To', 'helene@symfony.com');
Expand All @@ -83,7 +94,7 @@ public function testMailerAssertions()
$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');
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Mailer/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): void
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);
}

Expand Down
26 changes: 22 additions & 4 deletions src/Symfony/Component/Mailer/Test/Constraint/EmailCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@ 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;
}

/**
* {@inheritdoc}
*/
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);
}

/**
Expand All @@ -40,7 +42,7 @@ public function toString(): string
*/
protected function matches($events): bool
{
return $this->expectedValue === \count($events->getEvents($this->transport));
return $this->expectedValue === $this->countEmails($events);
}

/**
Expand All @@ -50,6 +52,22 @@ protected function matches($events): bool
*/
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM
}
}

$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()) {
Expand Down