Merge branch '4.4'

* 4.4:
  added PHPUnit constraints and assertions for the Mailer
  [Mailer] added support for the profiler
This commit is contained in:
Fabien Potencier 2019-08-05 18:34:36 +02:00
commit 5140c33ec9
28 changed files with 970 additions and 9 deletions

View File

@ -22,6 +22,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

@ -528,10 +528,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']);
@ -1810,6 +1806,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

@ -8,5 +8,10 @@
<service id="mailer.logger_message_listener" class="Symfony\Component\Mailer\EventListener\MessageLoggerListener">
<tag name="kernel.event_subscriber"/>
</service>
<service id="mailer.data_collector" class="Symfony\Component\Mailer\DataCollector\MessageDataCollector">
<argument type="service" id="mailer.logger_message_listener" />
<tag name="data_collector" template="@WebProfiler/Collector/mailer.html.twig" id="mailer" />
</service>
</services>
</container>

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

@ -74,12 +74,22 @@
"symfony/form": "<4.4",
"symfony/lock": "<4.4",
"symfony/mailer": "<4.4",
<<<<<<< HEAD
"symfony/messenger": "<4.4",
"symfony/property-info": "<4.4",
"symfony/serializer": "<4.4",
"symfony/stopwatch": "<4.4",
"symfony/translation": "<4.4",
"symfony/twig-bridge": "<4.4",
=======
"symfony/messenger": "<4.3",
"symfony/mime": "<4.4",
"symfony/property-info": "<3.4",
"symfony/serializer": "<4.2",
"symfony/stopwatch": "<3.4",
"symfony/translation": "<4.3",
"symfony/twig-bridge": "<4.1.1",
>>>>>>> 4.4
"symfony/twig-bundle": "<4.4",
"symfony/validator": "<4.4",
"symfony/workflow": "<4.4"

View File

@ -10,6 +10,7 @@ CHANGELOG
4.4.0
-----
* added support for the Mailer component
* added button to clear the ajax request tab
* deprecated the `ExceptionController::templateExists()` method
* deprecated the `TemplateManager::templateExists()` method

View File

@ -0,0 +1,202 @@
{% extends '@WebProfiler/Profiler/layout.html.twig' %}
{% block toolbar %}
{% set events = collector.events %}
{% if events.messages|length %}
{% set icon %}
{% include('@WebProfiler/Icon/mailer.svg') %}
<span class="sf-toolbar-value">{{ events.messages|length }}</span>
{% endset %}
{% set text %}
<div class="sf-toolbar-info-piece">
<b>Sent messages</b>
<span class="sf-toolbar-status">{{ events.messages|length }}</span>
</div>
{% for transport in events.transports %}
<div class="sf-toolbar-info-piece">
<b>{{ transport }}</b>
<span class="sf-toolbar-status">{{ events.messages(transport)|length }}</span>
</div>
{% endfor %}
{% endset %}
{{ include('@WebProfiler/Profiler/toolbar_item.html.twig', { 'link': profiler_url }) }}
{% endif %}
{% endblock %}
{% block head %}
{{ parent() }}
<style type="text/css">
/* utility classes */
.m-t-0 { margin-top: 0 !important; }
.m-t-10 { margin-top: 10px !important; }
/* basic grid */
.row {
display: flex;
flex-wrap: wrap;
margin-right: -15px;
margin-left: -15px;
}
.col {
flex-basis: 0;
flex-grow: 1;
max-width: 100%;
position: relative;
width: 100%;
min-height: 1px;
padding-right: 15px;
padding-left: 15px;
}
.col-4 {
flex: 0 0 33.333333%;
max-width: 33.333333%;
}
/* small tabs */
.sf-tabs-sm .tab-navigation li {
font-size: 14px;
padding: .3em .5em;
}
</style>
{% endblock %}
{% block menu %}
{% set events = collector.events %}
<span class="label {{ events.messages|length ? '' : 'disabled' }}">
<span class="icon">{{ include('@WebProfiler/Icon/mailer.svg') }}</span>
<strong>E-mails</strong>
{% if events.messages|length > 0 %}
<span class="count">
<span>{{ events.messages|length }}</span>
</span>
{% endif %}
</span>
{% endblock %}
{% block panel %}
{% set events = collector.events %}
<h2>Emails</h2>
{% if not events.messages|length %}
<div class="empty">
<p>No emails were sent.</p>
</div>
{% endif %}
<div class="metrics">
{% for transport in events.transports %}
<div class="metric">
<span class="value">{{ events.messages(transport)|length }}</span>
<span class="label">{{ events.messages(transport)|length == 1 ? 'message' : 'messages' }}</label>
</div>
{% endfor %}
</div>
{% for transport in events.transports %}
<h3>{{ transport }}</h3>
<div class="card-block">
<div class="sf-tabs sf-tabs-sm">
{% for event in events.events(transport) %}
{% set message = event.message %}
<div class="tab">
<h3 class="tab-title">Email #{{ loop.index }} <small>({{ event.isQueued() ? 'queued' : 'sent' }})</small></h3>
<div class="tab-content">
<div class="card">
{% if message.headers is not defined %}
{# RawMessage instance #}
<div class="card-block">
<pre class="prewrap" style="max-height: 600px">{{ message.toString() }}</pre>
</div>
{% else %}
{# Message instance #}
<div class="card-block">
<span class="label">Subject</span>
<h2 class="m-t-10">{{ message.headers.get('subject').bodyAsString() ?? '(empty)' }}</h2>
</div>
<div class="card-block">
<div class="row">
<div class="col col-4">
<span class="label">From</span>
<pre class="prewrap">{{ (message.headers.get('from').bodyAsString() ?? '(empty)')|replace({'From:': ''}) }}</pre>
<span class="label">To</span>
<pre class="prewrap">{{ (message.headers.get('to').bodyAsString() ?? '(empty)')|replace({'To:': ''}) }}</pre>
</div>
<div class="col">
<span class="label">Headers</span>
<pre class="prewrap">{% for header in message.headers.all|filter(header => (header.name ?? '') not in ['Subject', 'From', 'To']) %}
{{- header.toString }}
{%~ endfor %}</pre>
</div>
</div>
</div>
<div class="card-block">
{% if message.htmlBody is defined %}
{# Email instance #}
<div class="sf-tabs sf-tabs-sm">
<div class="tab">
<h3 class="tab-title">HTML Content</h3>
<div class="tab-content">
<pre class="prewrap" style="max-height: 600px">
{%- if message.htmlCharset() %}
{{- message.htmlBody()|convert_encoding('UTF-8', message.htmlCharset()) }}
{%- else %}
{{- message.htmlBody() }}
{%- endif -%}
</pre>
</div>
</div>
<div class="tab">
<h3 class="tab-title">Text Content</h3>
<div class="tab-content">
<pre class="prewrap" style="max-height: 600px">
{%- if message.textCharset() %}
{{- message.textBody()|convert_encoding('UTF-8', message.textCharset()) }}
{%- else %}
{{- message.textBody() }}
{%- endif -%}
</pre>
</div>
</div>
{% for attachment in message.attachments %}
<div class="tab">
<h3 class="tab-title">Attachment #{{ loop.index }}</h3>
<div class="tab-content">
<pre class="prewrap" style="max-height: 600px">{{ attachment.toString() }}</pre>
</div>
</div>
{% endfor %}
{% endif %}
<div class="tab">
<h3 class="tab-title">Parts Hierarchy</h3>
<div class="tab-content">
<pre class="prewrap" style="max-height: 600px">{{ message.body().asDebugString() }}</pre>
</div>
</div>
<div class="tab">
<h3 class="tab-title">Raw</h3>
<div class="tab-content">
<pre class="prewrap" style="max-height: 600px">{{ message.toString() }}</pre>
</div>
</div>
</div>
</div>
{% endif %}
</div>
</div>
</div>
{% endfor %}
</div>
</div>
{% endfor %}
{% endblock %}

View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="#AAAAAA" d="M22,4.9C22,3.9,21.1,3,20.1,3H3.9C2.9,3,2,3.9,2,4.9v13.1C2,19.1,2.9,20,3.9,20h16.1c1.1,0,1.9-0.9,1.9-1.9V4.9z M8.3,14.1l-3.1,3.1c-0.2,0.2-0.5,0.3-0.7,0.3S4,17.4,3.8,17.2c-0.4-0.4-0.4-1,0-1.4l3.1-3.1c0.4-0.4,1-0.4,1.4,0S8.7,13.7,8.3,14.1z M20.4,17.2c-0.2,0.2-0.5,0.3-0.7,0.3s-0.5-0.1-0.7-0.3l-3.1-3.1c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l3.1,3.1C20.8,16.2,20.8,16.8,20.4,17.2z M20.4,7.2l-7.6,7.6c-0.2,0.2-0.5,0.3-0.7,0.3s-0.5-0.1-0.7-0.3L3.8,7.2c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6.9,6.9L19,5.8c0.4-0.4,1-0.4,1.4,0S20.8,6.8,20.4,7.2z"/></svg>

After

Width:  |  Height:  |  Size: 643 B

View File

@ -4,6 +4,8 @@ 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
* [BC BREAK] Classes `AbstractApiTransport` and `AbstractHttpTransport` moved under `Transport` sub-namespace.

View File

@ -0,0 +1,60 @@
<?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\DataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\Mailer\Event\MessageEvents;
use Symfony\Component\Mailer\EventListener\MessageLoggerListener;
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class MessageDataCollector extends DataCollector
{
private $events;
public function __construct(MessageLoggerListener $logger)
{
$this->events = $logger->getEvents();
}
/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$this->data['events'] = $this->events;
}
public function getEvents(): MessageEvents
{
return $this->data['events'];
}
/**
* {@inheritdoc}
*/
public function reset()
{
$this->data = [];
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'mailer';
}
}

View File

@ -11,6 +11,8 @@
namespace Symfony\Component\Mailer\Event;
use Symfony\Component\Mime\RawMessage;
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
@ -48,4 +50,18 @@ class MessageEvents
return $events;
}
/**
* @return RawMessage[]
*/
public function getMessages(string $name = null): array
{
$events = $this->getEvents($name);
$messages = [];
foreach ($events as $event) {
$messages[] = $event->getMessage();
}
return $messages;
}
}

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();
}
}