1 | <?php |
||
26 | trait MailTracking |
||
27 | { |
||
28 | // TODO: Add check for attachments (number of & name) |
||
29 | // TODO: Add check for header |
||
30 | // TODO: Add check for message type |
||
31 | // TODO: Add check for Priority |
||
32 | // TODO: Allow checking specific message not just most recent one |
||
33 | |||
34 | /** |
||
35 | * Delivered emails. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $emails = []; |
||
40 | |||
41 | /** |
||
42 | * Register a listener for new emails. |
||
43 | * |
||
44 | * This calls my PHPUnit before each test it runs. It registers the MailRecorder "plugin" with Swift, so that we |
||
45 | * can get a copy of each email that is sent during that test. |
||
46 | * |
||
47 | * @before |
||
48 | */ |
||
49 | public function setUpMailTracking() |
||
54 | |||
55 | /** |
||
56 | * Retrieve the appropriate Swift message. |
||
57 | * |
||
58 | * @param Swift_Message|null $message |
||
59 | * |
||
60 | * @return Swift_Message |
||
61 | */ |
||
62 | protected function getEmail(Swift_Message $message = null) |
||
68 | |||
69 | /** |
||
70 | * Retrieve the mostly recently sent Swift message. |
||
71 | */ |
||
72 | protected function lastEmail() |
||
76 | |||
77 | /** |
||
78 | * Store a new Swift message. |
||
79 | * |
||
80 | * Collection of emails that were received by the MailRecorder plugin during a test. |
||
81 | * |
||
82 | * @param Swift_Message $email |
||
83 | */ |
||
84 | public function recordMail(Swift_Message $email) |
||
88 | |||
89 | /** |
||
90 | * Assert that the last email was bcc'ed to the given address. |
||
91 | * |
||
92 | * @param string $bcc |
||
93 | * @param Swift_Message|null $message |
||
94 | * |
||
95 | * @return PHPUnit_Framework_TestCase $this |
||
96 | */ |
||
97 | protected function seeEmailBcc($bcc, Swift_Message $message = null) |
||
104 | |||
105 | /** |
||
106 | * Assert that the last email was cc'ed to the given address. |
||
107 | * |
||
108 | * @param string $cc |
||
109 | * @param Swift_Message|null $message |
||
110 | * |
||
111 | * @return PHPUnit_Framework_TestCase $this |
||
112 | */ |
||
113 | protected function seeEmailCc($cc, Swift_Message $message = null) |
||
120 | |||
121 | /** |
||
122 | * Assert that the last email's body contains the given text. |
||
123 | * |
||
124 | * @param string $excerpt |
||
125 | * @param Swift_Message|null $message |
||
126 | * |
||
127 | * @return PHPUnit_Framework_TestCase $this |
||
128 | */ |
||
129 | protected function seeEmailContains($excerpt, Swift_Message $message = null) |
||
136 | |||
137 | /** |
||
138 | * Assert that the last email's body does not contain the given text. |
||
139 | * |
||
140 | * @param string $excerpt |
||
141 | * @param Swift_Message|null $message |
||
142 | * |
||
143 | * @return PHPUnit_Framework_TestCase $this |
||
144 | */ |
||
145 | protected function seeEmailDoesNotContain($excerpt, Swift_Message $message = null) |
||
153 | |||
154 | /** |
||
155 | * Assert that the last email's body equals the given text. |
||
156 | * |
||
157 | * @param string $body |
||
158 | * @param Swift_Message|null $message |
||
159 | * |
||
160 | * @return PHPUnit_Framework_TestCase $this |
||
161 | */ |
||
162 | protected function seeEmailEquals($body, Swift_Message $message = null) |
||
169 | |||
170 | /** |
||
171 | * Assert that the last email was delivered by the given address. |
||
172 | * |
||
173 | * @param string $sender |
||
174 | * @param Swift_Message|null $message |
||
175 | * |
||
176 | * @return PHPUnit_Framework_TestCase $this |
||
177 | */ |
||
178 | protected function seeEmailFrom($sender, Swift_Message $message = null) |
||
186 | |||
187 | /** |
||
188 | * Assert that the last email was set to reply to the given address. |
||
189 | * |
||
190 | * @param string $reply_to |
||
191 | * @param Swift_Message|null $message |
||
192 | * |
||
193 | * @return PHPUnit_Framework_TestCase $this |
||
194 | */ |
||
195 | protected function seeEmailReplyTo($reply_to, Swift_Message $message = null) |
||
203 | |||
204 | /** |
||
205 | * Assert that the given number of emails were sent. |
||
206 | * |
||
207 | * @param integer $count |
||
208 | * |
||
209 | * @return PHPUnit_Framework_TestCase $this |
||
210 | */ |
||
211 | protected function seeEmailsSent($count) |
||
219 | |||
220 | /** |
||
221 | * Assert that the last email's subject matches the given string. |
||
222 | * |
||
223 | * @param string $subject |
||
224 | * @param Swift_Message|null $message |
||
225 | * |
||
226 | * @return PHPUnit_Framework_TestCase $this |
||
227 | */ |
||
228 | protected function seeEmailSubject($subject, Swift_Message $message = null) |
||
236 | |||
237 | /** |
||
238 | * Assert that the last email's subject contains the given string. |
||
239 | * |
||
240 | * @param string $excerpt |
||
241 | * @param Swift_Message|null $message |
||
242 | * |
||
243 | * @return PHPUnit_Framework_TestCase $this |
||
244 | */ |
||
245 | protected function seeEmailSubjectContains($excerpt, Swift_Message $message = null) |
||
253 | |||
254 | /** |
||
255 | * Assert that the last email's subject does not contain the given string. |
||
256 | * |
||
257 | * @param string $excerpt |
||
258 | * @param Swift_Message|null $message |
||
259 | * |
||
260 | * @return PHPUnit_Framework_TestCase $this |
||
261 | */ |
||
262 | protected function seeEmailSubjectDoesNotContain($excerpt, Swift_Message $message = null) |
||
270 | |||
271 | /** |
||
272 | * Assert that the last email was sent to the given recipient. |
||
273 | * |
||
274 | * @param string $recipient |
||
275 | * @param Swift_Message|null $message |
||
276 | * |
||
277 | * @return PHPUnit_Framework_TestCase $this |
||
278 | */ |
||
279 | protected function seeEmailTo($recipient, Swift_Message $message = null) |
||
286 | |||
287 | /** |
||
288 | * Assert that no emails were sent. |
||
289 | * |
||
290 | * @return PHPUnit_Framework_TestCase $this |
||
291 | */ |
||
292 | protected function seeEmailWasNotSent() |
||
300 | |||
301 | /** |
||
302 | * Assert that at least one email was sent. |
||
303 | * |
||
304 | * @return PHPUnit_Framework_TestCase $this |
||
305 | */ |
||
306 | protected function seeEmailWasSent() |
||
312 | } |
||
313 |
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.