added PHPUnit constraints and assertions for the Mailer

This commit is contained in:
Fabien Potencier 2019-08-04 15:04:10 +02:00
parent fdaf0e0cf4
commit 23f237b92f
22 changed files with 677 additions and 11 deletions

View File

@ -4,6 +4,7 @@ CHANGELOG
4.4.0
-----
* Added `MailerAssertionsTrait`
* Deprecated support for `templating` engine in `TemplateController`, use Twig instead
* Deprecated the `$parser` argument of `ControllerResolver::__construct()` and `DelegatingLoader::__construct()`
* Deprecated the `controller_name_converter` and `resolve_controller_name_subscriber` services

View File

@ -553,10 +553,6 @@ class FrameworkExtension extends Extension
$loader->load('messenger_debug.xml');
}
if (class_exists(Mailer::class)) {
$loader->load('mailer_debug.xml');
}
$container->setParameter('profiler_listener.only_exceptions', $config['only_exceptions']);
$container->setParameter('profiler_listener.only_master_requests', $config['only_master_requests']);
@ -1969,6 +1965,9 @@ class FrameworkExtension extends Extension
}
$loader->load('mailer.xml');
if ($container->getParameter('kernel.debug')) {
$loader->load('mailer_debug.xml');
}
$loader->load('mailer_transports.xml');
$container->getDefinition('mailer.default_transport')->setArgument(0, $config['dsn']);

View File

@ -0,0 +1,126 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Test;
use PHPUnit\Framework\Constraint\LogicalNot;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mailer\Event\MessageEvents;
use Symfony\Component\Mailer\Test\Constraint as MailerConstraint;
use Symfony\Component\Mime\RawMessage;
use Symfony\Component\Mime\Test\Constraint as MimeConstraint;
trait MailerAssertionsTrait
{
public static function assertEmailCount(int $count, string $transport = null, string $message = ''): void
{
self::assertThat(self::getMessageMailerEvents(), new MailerConstraint\EmailCount($count, $transport), $message);
}
public static function assertEmailIsQueued(MessageEvent $event, string $message = ''): void
{
self::assertThat($event, new MailerConstraint\EmailIsQueued(), $message);
}
public static function assertEmailIsNotQueued(MessageEvent $event, string $message = ''): void
{
self::assertThat($event, new LogicalNot(new MailerConstraint\EmailIsQueued()), $message);
}
public static function assertEmailAttachementCount(RawMessage $email, int $count, string $message = ''): void
{
self::assertThat($email, new MimeConstraint\EmailAttachmentCount($count), $message);
}
public static function assertEmailTextBodyContains(RawMessage $email, string $text, string $message = ''): void
{
self::assertThat($email, new MimeConstraint\EmailTextBodyContains($text), $message);
}
public static function assertEmailTextBodyNotContains(RawMessage $email, string $text, string $message = ''): void
{
self::assertThat($email, new LogicalNot(new MimeConstraint\EmailTextBodyContains($text)), $message);
}
public static function assertEmailHtmlBodyContains(RawMessage $email, string $text, string $message = ''): void
{
self::assertThat($email, new MimeConstraint\EmailHtmlBodyContains($text), $message);
}
public static function assertEmailHtmlBodyNotContains(RawMessage $email, string $text, string $message = ''): void
{
self::assertThat($email, new LogicalNot(new MimeConstraint\EmailHtmlBodyContains($text)), $message);
}
public static function assertEmailHasHeader(RawMessage $email, string $headerName, string $message = ''): void
{
self::assertThat($email, new MimeConstraint\EmailHasHeader($headerName), $message);
}
public static function assertEmailNotHasHeader(RawMessage $email, string $headerName, string $message = ''): void
{
self::assertThat($email, new LogicalNot(new MimeConstraint\EmailHasHeader($headerName)), $message);
}
public static function assertEmailHeaderSame(RawMessage $email, string $headerName, string $expectedValue, string $message = ''): void
{
self::assertThat($email, new MimeConstraint\EmailHeaderSame($headerName, $expectedValue), $message);
}
public static function assertEmailHeaderNotSame(RawMessage $email, string $headerName, string $expectedValue, string $message = ''): void
{
self::assertThat($email, new LogicalNot(new MimeConstraint\EmailHeaderSame($headerName, $expectedValue)), $message);
}
public static function assertEmailAddressContains(RawMessage $email, string $headerName, string $expectedValue, string $message = ''): void
{
self::assertThat($email, new MimeConstraint\EmailAddressContains($headerName, $expectedValue), $message);
}
/**
* @return MessageEvents[]
*/
public static function getMailerEvents(string $transport = null): array
{
return self::getMessageMailerEvents()->getEvents($transport);
}
public static function getMailerEvent(int $index = 0, string $transport = null): ?MessageEvent
{
return self::getMailerEvents($transport)[$index] ?? null;
}
/**
* @return RawMessage[]
*/
public static function getMailerMessages(string $transport = null): array
{
return self::getMessageMailerEvents()->getMessages($transport);
}
public static function getMailerMessage(int $index = 0, string $transport = null): ?RawMessage
{
return self::getMailerMessages($transport)[$index] ?? null;
}
private static function getMessageMailerEvents(): MessageEvents
{
if (!self::getClient()->getRequest()) {
static::fail('Unable to make email assertions. Did you forget to make an HTTP request?');
}
if (!$logger = self::$container->get('mailer.logger_message_listener')) {
static::fail('A client must have Mailer enabled to make email assertions. Did you forget to require symfony/mailer?');
}
return $logger->getEvents();
}
}

View File

@ -11,11 +11,6 @@
namespace Symfony\Bundle\FrameworkBundle\Test;
/**
* Ideas borrowed from Laravel Dusk's assertions.
*
* @see https://laravel.com/docs/5.7/dusk#available-assertions
*/
trait WebTestAssertionsTrait
{
use BrowserKitAssertionsTrait;

View File

@ -23,6 +23,7 @@ abstract class WebTestCase extends KernelTestCase
{
use ForwardCompatTestTrait;
use WebTestAssertionsTrait;
use MailerAssertionsTrait;
private function doTearDown()
{

View File

@ -0,0 +1,41 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\NamedAddress;
class EmailController
{
public function indexAction(MailerInterface $mailer)
{
$mailer->send((new Email())->to('fabien@symfony.com')->from('fabien@symfony.com')->subject('Foo')
->addReplyTo('me@symfony.com')
->addCc('cc@symfony.com')
->text('Bar!')
->html('<p>Foo</p>')
->attach(file_get_contents(__FILE__), 'foobar.php')
);
$mailer->send((new Email())->to('fabien@symfony.com', 'thomas@symfony.com')->from('fabien@symfony.com')->subject('Foo')
->addReplyTo(new NamedAddress('me@symfony.com', 'Fabien Potencier'))
->addCc('cc@symfony.com')
->text('Bar!')
->html('<p>Foo</p>')
->attach(file_get_contents(__FILE__), 'foobar.php')
);
return new Response();
}
}

View File

@ -52,3 +52,7 @@ fragment_inlined:
array_controller:
path: /array_controller
defaults: { _controller: [ArrayController, someAction] }
send_email:
path: /send_email
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\EmailController::indexAction }

View File

@ -64,4 +64,28 @@ class MailerTest extends AbstractWebTestCase
$mailer->send($message);
}
public function testMailerAssertions()
{
$client = $this->createClient(['test_case' => 'Mailer', 'root_config' => 'config.yml', 'debug' => true]);
$client->request('GET', '/send_email');
$this->assertEmailCount(2);
$this->assertEmailIsQueued($this->getMailerEvent(0));
$email = $this->getMailerMessage(0);
$this->assertEmailHasHeader($email, 'To');
$this->assertEmailHeaderSame($email, 'To', 'fabien@symfony.com');
$this->assertEmailHeaderNotSame($email, 'To', 'helene@symfony.com');
$this->assertEmailTextBodyContains($email, 'Bar');
$this->assertEmailTextBodyNotContains($email, 'Foo');
$this->assertEmailHtmlBodyContains($email, 'Foo');
$this->assertEmailHtmlBodyNotContains($email, 'Bar');
$this->assertEmailAttachementCount($email, 1);
$email = $this->getMailerMessage(1);
$this->assertEmailAddressContains($email, 'To', 'fabien@symfony.com');
$this->assertEmailAddressContains($email, 'To', 'thomas@symfony.com');
$this->assertEmailAddressContains($email, 'Reply-To', 'me@symfony.com');
}
}

View File

@ -1,8 +1,10 @@
imports:
- { resource: ../config/default.yml }
- { resource: services.yml }
framework:
mailer:
dsn: 'smtp://null'
envelope:
sender: sender@example.org
recipients:

View File

@ -0,0 +1,2 @@
_emailtest_bundle:
resource: '@TestBundle/Resources/config/routing.yml'

View File

@ -0,0 +1,6 @@
services:
_defaults:
public: true
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\EmailController:
tags: ['controller.service_arguments']

View File

@ -42,9 +42,9 @@
"symfony/expression-language": "^3.4|^4.0|^5.0",
"symfony/http-client": "^4.3|^5.0",
"symfony/lock": "^4.4|^5.0",
"symfony/mailer": "^4.3|^5.0",
"symfony/mailer": "^4.4|^5.0",
"symfony/messenger": "^4.3|^5.0",
"symfony/mime": "^4.3|^5.0",
"symfony/mime": "^4.4|^5.0",
"symfony/process": "^3.4|^4.0|^5.0",
"symfony/security-csrf": "^3.4|^4.0|^5.0",
"symfony/security-http": "^3.4|^4.0|^5.0",
@ -76,6 +76,7 @@
"symfony/lock": "<4.4",
"symfony/mailer": "<4.4",
"symfony/messenger": "<4.3",
"symfony/mime": "<4.4",
"symfony/property-info": "<3.4",
"symfony/serializer": "<4.2",
"symfony/stopwatch": "<3.4",

View File

@ -4,6 +4,7 @@ CHANGELOG
4.4.0
-----
* Added PHPUnit constraints
* Added `MessageDataCollector`
* Added `MessageEvents` and `MessageLoggerListener` to allow collecting sent emails
* [BC BREAK] `TransportInterface` has a new `getName()` method

View File

@ -0,0 +1,55 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mailer\Test\Constraint;
use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\Mailer\Event\MessageEvents;
final class EmailCount extends Constraint
{
private $expectedValue;
private $transport;
public function __construct(int $expectedValue, string $transport = null)
{
$this->expectedValue = $expectedValue;
$this->transport = $transport;
}
/**
* {@inheritdoc}
*/
public function toString(): string
{
return sprintf('%shas sent "%d" emails', $this->transport ? $this->transport.' ' : '', $this->expectedValue);
}
/**
* @param MessageEvents $events
*
* {@inheritdoc}
*/
protected function matches($events): bool
{
return $this->expectedValue === \count($events->getEvents($this->transport));
}
/**
* @param MessageEvents $events
*
* {@inheritdoc}
*/
protected function failureDescription($events): string
{
return sprintf('the Transport %s (%d sent)', $this->toString(), \count($events->getEvents($this->transport)));
}
}

View File

@ -0,0 +1,46 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mailer\Test\Constraint;
use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\Mailer\Event\MessageEvent;
final class EmailIsQueued extends Constraint
{
/**
* {@inheritdoc}
*/
public function toString(): string
{
return 'is queued';
}
/**
* @param MessageEvent $event
*
* {@inheritdoc}
*/
protected function matches($event): bool
{
return $event->isQueued();
}
/**
* @param MessageEvent $event
*
* {@inheritdoc}
*/
protected function failureDescription($event): string
{
return 'the Email '.$this->toString();
}
}

View File

@ -4,6 +4,7 @@ CHANGELOG
4.4.0
-----
* Added PHPUnit constraints
* Added `AbstractPart::asDebugString()`
4.3.3

View File

@ -0,0 +1,74 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mime\Test\Constraint;
use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\Mime\Header\MailboxHeader;
use Symfony\Component\Mime\Header\MailboxListHeader;
use Symfony\Component\Mime\RawMessage;
final class EmailAddressContains extends Constraint
{
private $headerName;
private $expectedValue;
public function __construct(string $headerName, string $expectedValue)
{
$this->headerName = $headerName;
$this->expectedValue = $expectedValue;
}
/**
* {@inheritdoc}
*/
public function toString(): string
{
return sprintf('contains address "%s" with value "%s"', $this->headerName, $this->expectedValue);
}
/**
* @param RawMessage $message
*
* {@inheritdoc}
*/
protected function matches($message): bool
{
if (RawMessage::class === \get_class($message)) {
throw new \LogicException('Unable to test a message address on a RawMessage instance.');
}
$header = $message->getHeaders()->get($this->headerName);
if ($header instanceof MailboxHeader) {
return $this->expectedValue === $header->Address()->getAddress();
} elseif ($header instanceof MailboxListHeader) {
foreach ($header->getAddresses() as $address) {
if ($this->expectedValue === $address->getAddress()) {
return true;
}
}
return false;
}
throw new \LogicException(sprintf('Unable to test a message address on a non-address header.'));
}
/**
* @param RawMessage $message
*
* {@inheritdoc}
*/
protected function failureDescription($message): string
{
return sprintf('the Email %s (value is %s)', $this->toString(), $message->getHeaders()->get($this->headerName)->getBodyAsString());
}
}

View File

@ -0,0 +1,59 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mime\Test\Constraint;
use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\Mime\RawMessage;
final class EmailAttachmentCount extends Constraint
{
private $expectedValue;
private $transport;
public function __construct(int $expectedValue, string $transport = null)
{
$this->expectedValue = $expectedValue;
$this->transport = $transport;
}
/**
* {@inheritdoc}
*/
public function toString(): string
{
return sprintf('has sent "%d" attachment(s)', $this->expectedValue);
}
/**
* @param RawMessage $message
*
* {@inheritdoc}
*/
protected function matches($message): bool
{
if (RawMessage::class === \get_class($message) || Message::class === \get_class($message)) {
throw new \LogicException('Unable to test a message attachment on a RawMessage or Message instance.');
}
return $this->expectedValue === \count($message->getAttachments());
}
/**
* @param RawMessage $message
*
* {@inheritdoc}
*/
protected function failureDescription($message): string
{
return 'the Email '.$this->toString();
}
}

View File

@ -0,0 +1,57 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mime\Test\Constraint;
use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\Mime\RawMessage;
final class EmailHasHeader extends Constraint
{
private $headerName;
public function __construct(string $headerName)
{
$this->headerName = $headerName;
}
/**
* {@inheritdoc}
*/
public function toString(): string
{
return sprintf('has header "%s"', $this->headerName);
}
/**
* @param RawMessage $message
*
* {@inheritdoc}
*/
protected function matches($message): bool
{
if (RawMessage::class === \get_class($message)) {
throw new \LogicException('Unable to test a message header on a RawMessage instance.');
}
return $message->getHeaders()->has($this->headerName);
}
/**
* @param RawMessage $message
*
* {@inheritdoc}
*/
protected function failureDescription($message): string
{
return 'the Email '.$this->toString();
}
}

View File

@ -0,0 +1,59 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mime\Test\Constraint;
use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\Mime\RawMessage;
final class EmailHeaderSame extends Constraint
{
private $headerName;
private $expectedValue;
public function __construct(string $headerName, string $expectedValue)
{
$this->headerName = $headerName;
$this->expectedValue = $expectedValue;
}
/**
* {@inheritdoc}
*/
public function toString(): string
{
return sprintf('has header "%s" with value "%s"', $this->headerName, $this->expectedValue);
}
/**
* @param RawMessage $message
*
* {@inheritdoc}
*/
protected function matches($message): bool
{
if (RawMessage::class === \get_class($message)) {
throw new \LogicException('Unable to test a message header on a RawMessage instance.');
}
return $this->expectedValue === $message->getHeaders()->get($this->headerName)->getBodyAsString();
}
/**
* @param RawMessage $message
*
* {@inheritdoc}
*/
protected function failureDescription($message): string
{
return sprintf('the Email %s (value is %s)', $this->toString(), $message->getHeaders()->get($this->headerName)->getBodyAsString());
}
}

View File

@ -0,0 +1,56 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mime\Test\Constraint;
use PHPUnit\Framework\Constraint\Constraint;
final class EmailHtmlBodyContains extends Constraint
{
private $expectedText;
public function __construct(string $expectedText)
{
$this->expectedText = $expectedText;
}
/**
* {@inheritdoc}
*/
public function toString(): string
{
return sprintf('contains "%s"', $this->expectedText);
}
/**
* @param RawMessage $message
*
* {@inheritdoc}
*/
protected function matches($message): bool
{
if (RawMessage::class === \get_class($message) || Message::class === \get_class($message)) {
throw new \LogicException('Unable to test a message HTML body on a RawMessage or Message instance.');
}
return false !== mb_strpos($message->getHtmlBody(), $this->expectedText);
}
/**
* @param RawMessage $message
*
* {@inheritdoc}
*/
protected function failureDescription($email): string
{
return 'the Email HTML body '.$this->toString();
}
}

View File

@ -0,0 +1,56 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mime\Test\Constraint;
use PHPUnit\Framework\Constraint\Constraint;
final class EmailTextBodyContains extends Constraint
{
private $expectedText;
public function __construct(string $expectedText)
{
$this->expectedText = $expectedText;
}
/**
* {@inheritdoc}
*/
public function toString(): string
{
return sprintf('contains "%s"', $this->expectedText);
}
/**
* @param RawMessage $message
*
* {@inheritdoc}
*/
protected function matches($message): bool
{
if (RawMessage::class === \get_class($message) || Message::class === \get_class($message)) {
throw new \LogicException('Unable to test a message text body on a RawMessage or Message instance.');
}
return false !== mb_strpos($message->getTextBody(), $this->expectedText);
}
/**
* @param RawMessage $message
*
* {@inheritdoc}
*/
protected function failureDescription($email): string
{
return 'the Email text body '.$this->toString();
}
}