1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Mailer\Test\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\AssertionFailedError; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
7
|
|
|
use Zenstruck\Mailer\Test\InteractsWithMailer; |
8
|
|
|
use Zenstruck\Mailer\Test\TestEmail; |
9
|
|
|
use Zenstruck\Mailer\Test\Tests\Fixture\Email1; |
10
|
|
|
use Zenstruck\Mailer\Test\Tests\Fixture\Kernel; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Kevin Bond <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
final class InteractsWithMailerTest extends KernelTestCase |
16
|
|
|
{ |
17
|
|
|
use InteractsWithMailer; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @test |
21
|
|
|
* @dataProvider environmentProvider |
22
|
|
|
*/ |
23
|
|
|
public function can_assert_no_email_sent(string $environment): void |
24
|
|
|
{ |
25
|
|
|
self::bootKernel(['environment' => $environment]); |
26
|
|
|
|
27
|
|
|
$this->mailer()->assertNoEmailSent(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @test |
32
|
|
|
* @dataProvider environmentProvider |
33
|
|
|
*/ |
34
|
|
|
public function cannot_assert_email_if_none_sent(string $environment): void |
35
|
|
|
{ |
36
|
|
|
self::bootKernel(['environment' => $environment]); |
37
|
|
|
|
38
|
|
|
$this->expectException(AssertionFailedError::class); |
39
|
|
|
$this->expectExceptionMessage('No emails have been sent'); |
40
|
|
|
|
41
|
|
|
$this->mailer()->assertEmailSentTo('[email protected]', 'email subject'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @test |
46
|
|
|
* @dataProvider environmentProvider |
47
|
|
|
*/ |
48
|
|
|
public function can_assert_email_sent(string $environment): void |
49
|
|
|
{ |
50
|
|
|
self::bootKernel(['environment' => $environment]); |
51
|
|
|
|
52
|
|
|
self::$container->get('mailer')->send(new Email1()); |
53
|
|
|
|
54
|
|
|
$this->mailer() |
55
|
|
|
->assertSentEmailCount(1) |
56
|
|
|
->assertEmailSentTo('[email protected]', 'email subject') |
57
|
|
|
->assertEmailSentTo('[email protected]', function(TestEmail $email) { |
58
|
|
|
$email |
59
|
|
|
->assertTo('[email protected]', 'Kevin') |
60
|
|
|
->assertFrom('[email protected]') |
61
|
|
|
->assertCc('[email protected]') |
62
|
|
|
->assertBcc('[email protected]') |
63
|
|
|
->assertReplyTo('[email protected]') |
64
|
|
|
->assertHtmlContains('html body') |
65
|
|
|
->assertTextContains('text body') |
66
|
|
|
->assertContains('body') |
67
|
|
|
->assertHasFile('attachment.txt', 'text/plain', "attachment contents\n") |
68
|
|
|
; |
69
|
|
|
|
70
|
|
|
// TestEmail can call underlying Symfony\Component\Mime\Email methods |
71
|
|
|
$this->assertSame('Kevin', $email->getTo()[0]->getName()); |
|
|
|
|
72
|
|
|
}) |
73
|
|
|
; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @test |
78
|
|
|
* @dataProvider environmentProvider |
79
|
|
|
*/ |
80
|
|
|
public function assert_email_sent_to_fail(string $environment): void |
81
|
|
|
{ |
82
|
|
|
self::bootKernel(['environment' => $environment]); |
83
|
|
|
|
84
|
|
|
self::$container->get('mailer')->send(new Email1()); |
85
|
|
|
|
86
|
|
|
$this->expectException(AssertionFailedError::class); |
87
|
|
|
$this->expectExceptionMessage('Email sent, but "[email protected]" is not among to-addresses: [email protected]'); |
88
|
|
|
|
89
|
|
|
$this->mailer()->assertEmailSentTo('[email protected]', 'subject'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public static function environmentProvider(): iterable |
93
|
|
|
{ |
94
|
|
|
yield ['test']; |
95
|
|
|
yield ['bus_sync']; |
96
|
|
|
yield ['bus_async']; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @test |
101
|
|
|
*/ |
102
|
|
|
public function kernel_must_be_booted(): void |
103
|
|
|
{ |
104
|
|
|
$this->expectException(\LogicException::class); |
105
|
|
|
$this->expectExceptionMessage('kernel must be booted'); |
106
|
|
|
|
107
|
|
|
$this->mailer(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @test |
112
|
|
|
*/ |
113
|
|
|
public function mailer_must_be_enabled(): void |
114
|
|
|
{ |
115
|
|
|
self::bootKernel(['environment' => 'no_mailer']); |
116
|
|
|
|
117
|
|
|
$this->expectException(\LogicException::class); |
118
|
|
|
$this->expectExceptionMessage('Mailer and/or profiling not enabled'); |
119
|
|
|
|
120
|
|
|
$this->mailer(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @test |
125
|
|
|
*/ |
126
|
|
|
public function profiler_must_be_enabled_after_5_2(): void |
127
|
|
|
{ |
128
|
|
|
if (Kernel::VERSION_ID < 50200) { |
129
|
|
|
// profile needs to be enabled in 5.2+ |
130
|
|
|
$this->markTestSkipped(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
self::bootKernel(['environment' => 'no_profiler']); |
134
|
|
|
|
135
|
|
|
$this->expectException(\LogicException::class); |
136
|
|
|
$this->expectExceptionMessage('Mailer and/or profiling not enabled'); |
137
|
|
|
|
138
|
|
|
$this->mailer(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @test |
143
|
|
|
*/ |
144
|
|
|
public function profiler_does_not_need_to_enabled_prior_to_5_2(): void |
145
|
|
|
{ |
146
|
|
|
if (Kernel::VERSION_ID >= 50200) { |
147
|
|
|
// profile does not need to be enabled in <5.2 |
148
|
|
|
$this->markTestSkipped(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
self::bootKernel(['environment' => 'no_profiler']); |
152
|
|
|
|
153
|
|
|
self::$container->get('mailer')->send(new Email1()); |
154
|
|
|
|
155
|
|
|
$this->mailer()->assertSentEmailCount(1); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|