feature #33203 [Mailer] Add support for the queued flag in the EmailCount assertion (fabpot)
This PR was merged into the 4.4 branch.
Discussion
----------
[Mailer] Add support for the queued flag in the EmailCount assertion
| Q | A
| ------------- | ---
| Branch? | 4.4
| Bug fix? | yes
| New feature? | yes
| BC breaks? | no <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass? | yes <!-- please add some, will be required by reviewers -->
| Fixed tickets | n/a
| License | MIT
| Doc PR | -
<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.
Additionally (see https://symfony.com/roadmap):
- Bug fixes must be submitted against the lowest maintained branch where they apply
(lowest branches are regularly merged to upper ones so they get the fixes too).
- Features and deprecations must be submitted against branch 4.4.
- Legacy code removals go to the master branch.
-->
Commits
-------
afb1c04c35
[Mailer] added a way to test the number of queued emails
This commit is contained in:
commit
3522590290
@ -25,6 +25,11 @@ trait MailerAssertionsTrait
|
|||||||
self::assertThat(self::getMessageMailerEvents(), new MailerConstraint\EmailCount($count, $transport), $message);
|
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
|
public static function assertEmailIsQueued(MessageEvent $event, string $message = ''): void
|
||||||
{
|
{
|
||||||
self::assertThat($event, new MailerConstraint\EmailIsQueued(), $message);
|
self::assertThat($event, new MailerConstraint\EmailIsQueued(), $message);
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
|
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
|
||||||
|
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Symfony\Bundle\FullStack;
|
||||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||||
use Symfony\Component\Mailer\Mailer;
|
use Symfony\Component\Mailer\Mailer;
|
||||||
use Symfony\Component\Mailer\SentMessage;
|
use Symfony\Component\Mailer\SentMessage;
|
||||||
@ -71,9 +72,19 @@ class MailerTest extends AbstractWebTestCase
|
|||||||
$client->request('GET', '/send_email');
|
$client->request('GET', '/send_email');
|
||||||
|
|
||||||
$this->assertEmailCount(2);
|
$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->assertEmailHasHeader($email, 'To');
|
||||||
$this->assertEmailHeaderSame($email, 'To', 'fabien@symfony.com');
|
$this->assertEmailHeaderSame($email, 'To', 'fabien@symfony.com');
|
||||||
$this->assertEmailHeaderNotSame($email, 'To', 'helene@symfony.com');
|
$this->assertEmailHeaderNotSame($email, 'To', 'helene@symfony.com');
|
||||||
@ -83,7 +94,7 @@ class MailerTest extends AbstractWebTestCase
|
|||||||
$this->assertEmailHtmlBodyNotContains($email, 'Bar');
|
$this->assertEmailHtmlBodyNotContains($email, 'Bar');
|
||||||
$this->assertEmailAttachementCount($email, 1);
|
$this->assertEmailAttachementCount($email, 1);
|
||||||
|
|
||||||
$email = $this->getMailerMessage(1);
|
$email = $this->getMailerMessage($second);
|
||||||
$this->assertEmailAddressContains($email, 'To', 'fabien@symfony.com');
|
$this->assertEmailAddressContains($email, 'To', 'fabien@symfony.com');
|
||||||
$this->assertEmailAddressContains($email, 'To', 'thomas@symfony.com');
|
$this->assertEmailAddressContains($email, 'To', 'thomas@symfony.com');
|
||||||
$this->assertEmailAddressContains($email, 'Reply-To', 'me@symfony.com');
|
$this->assertEmailAddressContains($email, 'Reply-To', 'me@symfony.com');
|
||||||
|
@ -54,7 +54,7 @@ class Mailer implements MailerInterface
|
|||||||
throw new TransportException('Cannot send message without a valid envelope.', 0, $e);
|
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);
|
$this->dispatcher->dispatch($event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,11 +18,13 @@ final class EmailCount extends Constraint
|
|||||||
{
|
{
|
||||||
private $expectedValue;
|
private $expectedValue;
|
||||||
private $transport;
|
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->expectedValue = $expectedValue;
|
||||||
$this->transport = $transport;
|
$this->transport = $transport;
|
||||||
|
$this->queued = $queued;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -30,7 +32,7 @@ final class EmailCount extends Constraint
|
|||||||
*/
|
*/
|
||||||
public function toString(): string
|
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
|
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
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
$this->dispatcher->dispatch($event);
|
||||||
$envelope = $event->getEnvelope();
|
$envelope = $event->getEnvelope();
|
||||||
if (!$envelope->getRecipients()) {
|
if (!$envelope->getRecipients()) {
|
||||||
|
Reference in New Issue
Block a user