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

Skip to content

Commit fbee460

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

File tree

11 files changed

+523
-5
lines changed

11 files changed

+523
-5
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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\LogicalAnd;
15+
use PHPUnit\Framework\Constraint\LogicalNot;
16+
use Symfony\Component\Mailer\Test\Constraint as MailerConstraint;
17+
use Symfony\Component\Mailer\Event\MessageEvent;
18+
use Symfony\Component\Mailer\Event\MessageEvents;
19+
use Symfony\Component\Mime\RawMessage;
20+
use Symfony\Component\Mime\Test\Constraint as MimeConstraint;
21+
22+
trait MailerAssertionsTrait
23+
{
24+
public static function assertEmailCount(int $count, string $transport = null, string $message = ''): void
25+
{
26+
self::assertThat(self::getMessageMailerE 579F vents(), new MailerConstraint\EmailCount($count, $transport), $message);
27+
}
28+
29+
public static function assertEmailIsQueued(MessageEvent $event, string $message = ''): void
30+
{
31+
self::assertThat($event, new MailerConstraint\EmailIsQueued(), $message);
32+
}
33+
34+
public static function assertEmailAttachementCount(RawMessage $email, int $count, string $message = ''): void
35+
{
36+
self::assertThat($email, new MimeConstraint\EmailAttachmentCount($count), $message);
37+
}
38+
39+
public static function assertEmailTextBodyContains(RawMessage $email, string $text, string $message = ''): void
40+
{
41+
self::assertThat($email, new MimeConstraint\EmailTextBodyContains($text), $message);
42+
}
43+
44+
public static function assertEmailTextBodyNotContains(RawMessage $email, string $text, string $message = ''): void
45+
{
46+
self::assertThat($email, new LogicalNot(new MimeConstraint\EmailTextBodyContains($text)), $message);
47+
}
48+
49+
public static function assertEmailHtmlBodyContains(RawMessage $email, string $text, string $message = ''): void
50+
{
51+
self::assertThat($email, new MimeConstraint\EmailHtmlBodyContains($text), $message);
52+
}
53+
54+
public static function assertEmailHtmlBodyNotContains(RawMessage $email, string $text, string $message = ''): void
55+
{
56+
self::assertThat($email, new LogicalNot(new MimeConstraint\EmailHtmlBodyContains($text)), $message);
57+
}
58+
59+
public static function assertEmailHasHeader(RawMessage $email, string $headerName, string $message = ''): void
60+
{
61+
self::assertThat($email, new MimeConstraint\EmailHasHeader($headerName), $message);
62+
}
63+
64+
public static function assertEmailNotHasHeader(RawMessage $email, string $headerName, string $message = ''): void
65+
{
66+
self::assertThat($email, new LogicalNot(new MimeConstraint\EmailHasHeader($headerName)), $message);
67+
}
68+
69+
public static function assertEmailHeaderSame(RawMessage $email, string $headerName, string $expectedValue, string $message = ''): void
70+
{
71+
self::assertThat($email, new MimeConstraint\EmailHeaderSame($headerName, $expectedValue), $message);
72+
}
73+
74+
public static function assertEmailHeaderNotSame(RawMessage $email, string $headerName, string $expectedValue, string $message = ''): void
75+
{
76+
self::assertThat($email, new LogicalNot(new MimeConstraint\EmailHeaderSame($headerName, $expectedValue)), $message);
77+
}
78+
79+
/**
80+
* @return MessageEvents[]
81+
*/
82+
public static function getMailerEvents(string $transport = null): array
83+
{
84+
return self::getMessageMailerEvents()->getEvents($transport);
85+
}
86+
87+
public static function getMailerEvent(int $index = 0, string $transport = null): ?MessageEvent
88+
{
89+
return self::getMailerEvents($transport)[$index];
90+
}
91+
92+
/**
93+
* @return RawMessage[]
94+
*/
95+
public static function getMailerMessages(string $transport = null): array
96+
{
97+
return self::getMessageMailerEvents()->getMessages($transport);
98+
}
99+
100+
public static function getMailerMessage(int $index = 0, string $transport = null): ?RawMessage
101+
{
102+
return self::getMailerMessages($transport)[$index] ?? null;
103+
}
104+
105+
private static function getMessageMailerEvents(): MessageEvents
106+
{
107+
if (!self::getClient()->getRequest()) {
108+
static::fail('Unable to make email assertions. Did you forget to make an HTTP request?');
109+
}
110+
111+
if (!$logger = self::$container->get('mailer.logger_message_listener')) {
112+
static::fail('A client must have Mailer enabled to make email assertions. Did you forget to require symfony/mailer?');
113+
}
114+
115+
return $logger->getEvents();
116+
}
117+
}

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/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+
}
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+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 EmailHasHeader extends Constraint
18+
{
19+
private $headerName;
20+
21+
public function __construct(string $headerName)
22+
{
23+
$this->headerName = $headerName;
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function toString(): string
30+
{
31+
return sprintf('has header "%s"', $this->headerName);
32+
}
33+
34+
/**
35+
* @param RawMessage $message
36+
*
37+
* {@inheritdoc}
38+
*/
39+
protected function matches($message): bool
40+
{
41+
if (RawMessage::class === get_class($message)) {
42+
throw new \LogicException('Unable to test a message header on a RawMessage instance.');
43+
}
44+
45+
return $message->getHeaders()->has($this->headerName);
46+
}
47+
48+
/**
49+
* @param RawMessage $message
50+
*
51+
* {@inheritdoc}
52+
*/
53+
protected function failureDescription($message): string
54+
{
55+
return 'the Email '.$this->toString();
56+
}
57+
}

0 commit comments

Comments
 (0)
0