8000 added PHPUnit constraints and assertions for the Mailer · symfony/symfony@05c1ae4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 05c1ae4

Browse files
committed
added PHPUnit constraints and assertions for the Mailer
1 parent aa93f0b commit 05c1ae4

14 files changed

+525
-5
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.4.0
55
-----
66

7+
* Added `MailerAssertionsTrait`
78
* Deprecated support for `templating` engine in `TemplateController`, use Twig instead
89
* Deprecated the `$parser` argument of `ControllerResolver::__construct()` and `DelegatingLoader::__construct()`
910
* Deprecated the `controller_name_converter` and `resolve_controller_name_subscriber` services
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Test;
13+
14+
use PHPUnit\Framework\Constraint\LogicalNot;
15+
use Symfony\Component\Mailer\Event\MessageEvent;
16+
use Symfony\Component\Mailer\Event\MessageEvents;
17+
use Symfony\Component\Mailer\Test\Constraint as MailerConstraint;
18+
use Symfony\Component\Mime\RawMessage;
19+
use Symfony\Component\Mime\Test\Constraint as MimeConstraint;
20+
21+
trait MailerAssertionsTrait
22+
{
23+
public static function assertEmailCount(int $count, string $transport = null, string $message = ''): void
24+
{
25+
self::assertThat(self::getMessageMailerEvents(), new MailerConstraint\EmailCount($count, $transport), $message);
26+
}
27+
28+
public static function assertEmailIsQueued(MessageEvent $event, string $message = ''): void
29+
{
30+
self::assertThat($event, new MailerConstraint\EmailIsQueued(), $message);
31+
}
32+
33+
public static function assertEmailAttachementCount(RawMessage $email, int $count, string $message = ''): void
34+
{
35+
self::assertThat($email, new MimeConstraint\EmailAttachmentCount($count), $message);
36+
}
37+
38+
public static function assertEmailTextBodyContains(RawMessage $email, string $text, string $message = ''): void
39+
{
40+
self::assertThat($email, new MimeConstraint\EmailTextBodyContains($text), $message);
41+
}
42+
43+
public static function assertEmailTextBodyNotContains(RawMessage $email, string $text, string $message = ''): void
44+
{
45+
self::assertThat($email, new LogicalNot(new MimeConstraint\EmailTextBodyContains($text)), $message);
46+
}
47+
48+
public static function assertEmailHtmlBodyContains(RawMessage $email, string $text, string $message = ''): void
49+
{
50+
self::assertThat($email, new MimeConstraint\EmailHtmlBodyContains($text), $message);
51+
}
52+
53+
public static function assertEmailHtmlBodyNotContains(RawMessage $email, string $text, string $message = ''): void
54+
{
55+
self::assertThat($email, new LogicalNot(new MimeConstraint\EmailHtmlBodyContains($text)), $message);
56+
}
57+
58+
public static function assertEmailHasHeader(RawMessage $email, string $headerName, string $message = ''): void
59+
{
60+
self::assertThat($email, new MimeConstraint\EmailHasHeader($headerName), $message);
61+
}
62+
63+
public static function assertEmailNotHasHeader(RawMessage $email, string $headerName, string $message = ''): void
64+
{
65+
self::assertThat($email, new LogicalNot(new MimeConstraint\EmailHasHeader($headerName)), $message);
66+
}
67+
68+
public static function assertEmailHeaderSame(RawMessage $email, string $headerName, string $expectedValue, string $message = ''): void
69+
{
70+
self::assertThat($email, new MimeConstraint\EmailHeaderSame($headerName, $expectedValue), $message);
71+
}
72+
73+
public static function assertEmailHeaderNotSame(RawMessage $email, string $headerName, string $expectedValue, string $message = ''): void
74+< 10000 /span>
{
75+
self::assertThat($email, new LogicalNot(new MimeConstraint\EmailHeaderSame($headerName, $expectedValue)), $message);
76+
}
77+
78+
/**
79+
* @return MessageEvents[]
80+
*/
81+
public static function getMailerEvents(string $transport = null): array
82+
{
83+
return self::getMessageMailerEvents()->getEvents($transport);
84+
}
85+
86+
public static function getMailerEvent(int $index = 0, string $transport = null): ?MessageEvent
87+
{
88+
return self::getMailerEvents($transport)[$index];
89+
}
90+
91+
/**
92+
* @return RawMessage[]
93+
*/
94+
public static function getMailerMessages(string $transport = null): array
95+
{
96+
return self::getMessageMailerEvents()->getMessages($transport);
97+
}
98+
99+
public static function getMailerMessage(int $index = 0, string $transport = null): ?RawMessage
100+
{
101+
return self::getMailerMessages($transport)[$index] ?? null;
102+
}
103+
104+
private static function getMessageMailerEvents(): MessageEvents
105+
{
106+
if (!self::getClient()->getRequest()) {
107+
static::fail('Unable to make email assertions. Did you forget to make an HTTP request?');
108+
}
109+
110+
if (!$logger = self::$container->get('mailer.logger_message_listener')) {
111+
static::fail('A client must have Mailer enabled to make email assertions. Did you forget to require symfony/mailer?');
112+
}
113+
114+
return $logger->getEvents();
115+
}
116+
}

src/Symfony/Bundle/FrameworkBundle/Test/WebTestAssertionsTrait.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Test;
1313

14-
/**
15-
* Ideas borrowed from Laravel Dusk's assertions.
16-
*
17-
* @see https://laravel.com/docs/5.7/dusk#available-assertions
18-
*/
1914
trait WebTestAssertionsTrait
2015
{
2116
use BrowserKitAssertionsTrait;
2217
use DomCrawlerAssertionsTrait;
18+
use MailerAssertionsTrait;
2319
}

src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ abstract class WebTestCase extends KernelTestCase
2323
{
2424
use ForwardCompatTestTrait;
2525
use WebTestAssertionsTrait;
26+
use MailerAssertionsTrait;
2627

2728
private function doTearDown()
2829
{

src/Symfony/Component/Mailer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.4.0
55
-----
66

7+
* Added PHPUnit constraints
78
* Added `MessageEvents` and `MessageLoggerListener` to allow collecting sent emails
89
* [BC BREAK] `TransportInterface` has a new `getName()` method
910
* [BC BREAK] Classes `AbstractApiTransport` and `AbstractHttpTransport` moved under `Transport` sub-namespace.

src/Symfony/Component/Mailer/Event/MessageEvents.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Mailer\Event;
1313

14+
use Symfony\Component\Mime\RawMessage;
15+
1416
/**
1517
* @author Fabien Potencier <fabien@symfony.com>
1618
*/
@@ -48,4 +50,18 @@ public function getEvents(string $name = null): array
4850

4951
return $events;
5052
}
53+
54+
/**
55+
* @return RawMessage[]
56+
*/
57+
public function getMessages(string $name = null): array
58+
{
59+
$events = $this->getEvents($name);
60+
$messages = [];
61+
foreach ($events as $event) {
62+
$messages[] = $event->getMessage();
63+
}
64+
65+
return $messages;
66+
}
5167
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\Mailer\Event\MessageEvents;
16+
17+
final class EmailCount extends Constraint
18+
{
19+
private $expectedValue;
20+
private $transport;
21+
22+
public function __construct(int $expectedValue, string $transport = null)
23+
{
24+
$this->expectedValue = $expectedValue;
25+
$this->transport = $transport;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function toString(): string
32+
{
33+
return sprintf('%shas sent "%d" emails', $this->transport ? $this->transport.' ' : '', $this->expectedValue);
34+
}
35+
36+
/**
37+
* @param MessageEvents $events
38+
*
39+
* {@inheritdoc}
40+
*/
41+
protected function matches($events): bool
42+
{
43+
return $this->expectedValue === \count($events->getEvents($this->transport));
44+
}
45+
46+
/**
47+
* @param MessageEvents $events
48+
*
49+
* {@inheritdoc}
50+
*/
51+
protected function failureDescription($events): string
52+
{
53+
return sprintf('the Transport %s (%d sent)', $this->toString(), \count($events->getEvents($this->transport)));
54+
}
55+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\Mailer\Event\MessageEvent;
16+
17+
final class EmailIsQueued extends Constraint
18+
{
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function toString(): string
23+
{
24+
return 'is queued';
25+
}
26+
27+
/**
28+
* @param MessageEvent $event
29+
*
30+
* {@inheritdoc}
31+
*/
32+
protected function matches($event): bool
33+
{
34+
return $event->isQueued();
35+
}
36+
37+
/**
38+
* @param MessageEvent $event
39+
*
40+
* {@inheritdoc}
41+
*/
42+
protected function failureDescription($event): string
43+
{
44+
return 'the Email '.$this->toString();
45+
}
46+
}

src/Symfony/Component/Mime/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.4.0
55
-----
66

7+
* Added PHPUnit constraints
78
* Added `AbstractPart::asDebugString()`
89

910
4.3.3
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mime\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\Mime\RawMessage;
16+
17+
final class EmailAttachmentCount extends Constraint
18+
{
19+
private $expectedValue;
20+
private $transport;
21+
22+
public function __construct(int $expectedValue, string $transport = null)
23+
{
24+
$this->expectedValue = $expectedValue;
25+
$this->transport = $transport;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function toString(): string
32+
{
33+
return sprintf('has sent "%d" attachment(s)', $this->expectedValue);
34+
}
35+
36+
/**
37+
* @param RawMessage $message
38+
*
39+
* {@inheritdoc}
40+
*/
41+
protected function matches($message): bool
42+
{
43+
if (RawMessage::class === \get_class($message) || Message::class === \get_class($message)) {
44+
throw new \LogicException('Unable to test a message header on a RawMessage or Message instance.');
45+
}
46+
47+
return $this->expectedValue === \count($message->getAttachments());
48+
}
49+
50+
/**
51+
* @param RawMessage $message
52+
*
53+
* {@inheritdoc}
54+
*/
55+
protected function failureDescription($message): string
56+
{
57+
return 'the Email '.$this->toString();
58+
}
59+
}

0 commit comments

Comments
 (0)
0