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 | * Called my PHPUnit before each test it run. It registers the MailRecorder "plugin" with Swift, so that we can |
||
45 | * 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) |
||
152 | |||
153 | /** |
||
154 | * Assert that the last email's body equals the given text. |
||
155 | * |
||
156 | * @param string $body |
||
157 | * @param Swift_Message|null $message |
||
158 | * |
||
159 | * @return PHPUnit_Framework_TestCase $this |
||
160 | */ |
||
161 | protected function seeEmailEquals($body, Swift_Message $message = null) |
||
168 | |||
169 | /** |
||
170 | * Assert that the last email was delivered by the given address. |
||
171 | * |
||
172 | * @param string $sender |
||
173 | * @param Swift_Message|null $message |
||
174 | * |
||
175 | * @return PHPUnit_Framework_TestCase $this |
||
176 | */ |
||
177 | protected function seeEmailFrom($sender, Swift_Message $message = null) |
||
185 | |||
186 | /** |
||
187 | * Assert that the last email was set to reply to the given address. |
||
188 | * |
||
189 | * @param string $reply_to |
||
190 | * @param Swift_Message|null $message |
||
191 | * |
||
192 | * @return PHPUnit_Framework_TestCase $this |
||
193 | */ |
||
194 | protected function seeEmailReplyTo($reply_to, Swift_Message $message = null) |
||
201 | |||
202 | /** |
||
203 | * Assert that the given number of emails were sent. |
||
204 | * |
||
205 | * @param integer $count |
||
206 | * |
||
207 | * @return PHPUnit_Framework_TestCase $this |
||
208 | */ |
||
209 | protected function seeEmailsSent($count) |
||
217 | |||
218 | /** |
||
219 | * Assert that the last email's subject matches the given string. |
||
220 | * |
||
221 | * @param string $subject |
||
222 | * @param Swift_Message|null $message |
||
223 | * |
||
224 | * @return PHPUnit_Framework_TestCase $this |
||
225 | */ |
||
226 | protected function seeEmailSubject($subject, Swift_Message $message = null) |
||
234 | |||
235 | /** |
||
236 | * Assert that the last email was sent to the given recipient. |
||
237 | * |
||
238 | * @param string $recipient |
||
239 | * @param Swift_Message|null $message |
||
240 | * |
||
241 | * @return PHPUnit_Framework_TestCase $this |
||
242 | */ |
||
243 | protected function seeEmailTo($recipient, Swift_Message $message = null) |
||
250 | |||
251 | /** |
||
252 | * Assert that no emails were sent. |
||
253 | * |
||
254 | * @return PHPUnit_Framework_TestCase $this |
||
255 | */ |
||
256 | protected function seeEmailWasNotSent() |
||
262 | |||
263 | /** |
||
264 | * Assert that at least one email was sent. |
||
265 | * |
||
266 | * @return PHPUnit_Framework_TestCase $this |
||
267 | */ |
||
268 | protected function seeEmailWasSent() |
||
274 | } |
||
275 |
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.