1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org) |
4
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
5
|
|
|
* |
6
|
|
|
* Licensed under The MIT License |
7
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
8
|
|
|
* Redistributions of files must retain the above copyright notice. |
9
|
|
|
* |
10
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
11
|
|
|
* @link https://cakephp.org CakePHP(tm) Project |
12
|
|
|
* @since 3.7.0 |
13
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
14
|
|
|
*/ |
15
|
|
|
namespace Cake\TestSuite; |
16
|
|
|
|
17
|
|
|
use Cake\TestSuite\Constraint\Email\MailContains; |
18
|
|
|
use Cake\TestSuite\Constraint\Email\MailContainsHtml; |
19
|
|
|
use Cake\TestSuite\Constraint\Email\MailContainsText; |
20
|
|
|
use Cake\TestSuite\Constraint\Email\MailCount; |
21
|
|
|
use Cake\TestSuite\Constraint\Email\MailSentFrom; |
22
|
|
|
use Cake\TestSuite\Constraint\Email\MailSentTo; |
23
|
|
|
use Cake\TestSuite\Constraint\Email\MailSentWith; |
24
|
|
|
use Cake\TestSuite\Constraint\Email\NoMailSent; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Make assertions on emails sent through the Cake\TestSuite\TestEmailTransport |
28
|
|
|
* |
29
|
|
|
* After adding the trait to your test case, all mail transports will be replaced |
30
|
|
|
* with TestEmailTransport which is used for making assertions and will *not* actually |
31
|
|
|
* send emails. |
32
|
|
|
*/ |
33
|
|
|
trait EmailTrait |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Replaces all transports with the test transport during test setup |
37
|
|
|
* |
38
|
|
|
* @before |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function setupTransports() |
42
|
|
|
{ |
43
|
|
|
TestEmailTransport::replaceAllTransports(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Resets transport state |
48
|
|
|
* |
49
|
|
|
* @after |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function cleanupEmailTrait() |
53
|
|
|
{ |
54
|
|
|
TestEmailTransport::clearEmails(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Asserts an expected number of emails were sent |
59
|
|
|
* |
60
|
|
|
* @param int $count Email count |
61
|
|
|
* @param string $message Message |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function assertMailCount($count, $message = null) |
65
|
|
|
{ |
66
|
|
|
$this->assertThat($count, new MailCount(), $message); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
/** |
69
|
|
|
* |
70
|
|
|
* Asserts that no emails were sent |
71
|
|
|
* |
72
|
|
|
* @param string $message Message |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function assertNoMailSent($message = null) |
76
|
|
|
{ |
77
|
|
|
$this->assertThat(null, new NoMailSent(), $message); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Asserts an email at a specific index was sent to an address |
82
|
|
|
* |
83
|
|
|
* @param int $at Email index |
84
|
|
|
* @param int $address Email address |
85
|
|
|
* @param string $message Message |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
public function assertMailSentToAt($at, $address, $message = null) |
89
|
|
|
{ |
90
|
|
|
$this->assertThat($address, new MailSentTo($at), $message); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Asserts an email at a specific index was sent from an address |
95
|
|
|
* |
96
|
|
|
* @param int $at Email index |
97
|
|
|
* @param int $address Email address |
98
|
|
|
* @param string $message Message |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
public function assertMailSentFromAt($at, $address, $message = null) |
102
|
|
|
{ |
103
|
|
|
$this->assertThat($address, new MailSentFrom($at), $message); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Asserts an email at a specific index contains expected contents |
108
|
|
|
* |
109
|
|
|
* @param int $at Email index |
110
|
|
|
* @param int $contents Contents |
111
|
|
|
* @param string $message Message |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function assertMailContainsAt($at, $contents, $message = null) |
115
|
|
|
{ |
116
|
|
|
$this->assertThat($contents, new MailContains($at), $message); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Asserts an email at a specific index contains expected html contents |
121
|
|
|
* |
122
|
|
|
* @param int $at Email index |
123
|
|
|
* @param int $contents Contents |
124
|
|
|
* @param string $message Message |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public function assertMailContainsHtmlAt($at, $contents, $message = null) |
128
|
|
|
{ |
129
|
|
|
$this->assertThat($contents, new MailContainsHtml($at), $message); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Asserts an email at a specific index contains expected text contents |
134
|
|
|
* |
135
|
|
|
* @param int $at Email index |
136
|
|
|
* @param int $contents Contents |
137
|
|
|
* @param string $message Message |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
public function assertMailContainsTextAt($at, $contents, $message = null) |
141
|
|
|
{ |
142
|
|
|
$this->assertThat($contents, new MailContainsText($at), $message); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Asserts an email at a specific index contains the expected value within an Email getter |
147
|
|
|
* |
148
|
|
|
* @param int $at Email index |
149
|
|
|
* @param int $expected Contents |
150
|
|
|
* @param int $parameter Email getter parameter (e.g. "cc", "subject") |
151
|
|
|
* @param string $message Message |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
public function assertMailSentWithAt($at, $expected, $parameter, $message = null) |
155
|
|
|
{ |
156
|
|
|
$this->assertThat($expected, new MailSentWith($at, $parameter), $message); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Asserts an email was sent to an address |
161
|
|
|
* |
162
|
|
|
* @param int $address Email address |
163
|
|
|
* @param string $message Message |
164
|
|
|
* @return void |
165
|
|
|
*/ |
166
|
|
|
public function assertMailSentTo($address, $message = null) |
167
|
|
|
{ |
168
|
|
|
$this->assertThat($address, new MailSentTo(), $message); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Asserts an email was sent from an address |
173
|
|
|
* |
174
|
|
|
* @param int $address Email address |
175
|
|
|
* @param string $message Message |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
public function assertMailSentFrom($address, $message = null) |
179
|
|
|
{ |
180
|
|
|
$this->assertThat($address, new MailSentFrom(), $message); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Asserts an email contains expected contents |
185
|
|
|
* |
186
|
|
|
* @param int $contents Contents |
187
|
|
|
* @param string $message Message |
188
|
|
|
* @return void |
189
|
|
|
*/ |
190
|
|
|
public function assertMailContains($contents, $message = null) |
191
|
|
|
{ |
192
|
|
|
$this->assertThat($contents, new MailContains(), $message); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Asserts an email contains expected html contents |
197
|
|
|
* |
198
|
|
|
* @param int $contents Contents |
199
|
|
|
* @param string $message Message |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
|
|
public function assertMailContainsHtml($contents, $message = null) |
203
|
|
|
{ |
204
|
|
|
$this->assertThat($contents, new MailContainsHtml(), $message); |
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Asserts an email contains expected text contents |
209
|
|
|
* |
210
|
|
|
* @param int $contents Contents |
211
|
|
|
* @param string $message Message |
212
|
|
|
* @return void |
213
|
|
|
*/ |
214
|
|
|
public function assertMailContainsText($contents, $message = null) |
215
|
|
|
{ |
216
|
|
|
$this->assertThat($contents, new MailContainsText(), $message); |
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Asserts an email contains the expected value within an Email getter |
221
|
|
|
* |
222
|
|
|
* @param int $expected Contents |
223
|
|
|
* @param int $parameter Email getter parameter (e.g. "cc", "subject") |
224
|
|
|
* @param string $message Message |
225
|
|
|
* @return void |
226
|
|
|
*/ |
227
|
|
|
public function assertMailSentWith($expected, $parameter, $message = null) |
228
|
|
|
{ |
229
|
|
|
$this->assertThat($expected, new MailSentWith(null, $parameter), $message); |
|
|
|
|
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.