MailTracking::seeEmailEquals()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 3
c 3
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Spinen\MailAssertions;
4
5
use Illuminate\Container\Container;
6
use Illuminate\Contracts\Container\BindingResolutionException;
7
use Illuminate\Contracts\Mail\Mailer;
8
use Swift_Message;
9
10
/**
11
 * Trait MailTracking
12
 *
13
 * Trait to mixin to your test to allow for custom assertions when using PHPUnit with Laravel. This trait assumes
14
 * you are using it from the PHPUnit TestCase class (or a child class of it).
15
 *
16
 * This originally started out as a copy & paste from a video series that Jeffrey Way did on laracasts.com. If you do
17
 * not have an account on Laracasts, you should get one. It is an amazing resource to learn from. We used that
18
 * example & converted it to a package so that it would be easy to install. We have also expanded on the initial
19
 * assertions.
20
 *
21
 * I WANT IT CLEAR THAT THIS WOULD NOT HAVE HAPPENED WITHOUT THE INITIAL WORK OF JEFFREY WAY. WE ARE NOT CLAIMING TO
22
 * BE THE CREATORS OF THE CONCEPT.
23
 *
24
 * @package Spinen\MailAssertions
25
 * @see     https://gist.github.com/JeffreyWay/b501c53d958b07b8a332
26
 * @tutorial https://laracasts.com/series/phpunit-testing-in-laravel/episodes/12
27
 */
28
trait MailTracking
29
{
30
    // TODO: Add check for attachments (number of & name)
31
    // TODO: Add check for header
32
    // TODO: Add check for message type
33
    // TODO: Allow checking specific message not just most recent one
34
35
    /**
36
     * Delivered emails.
37
     *
38
     * @var array
39
     */
40
    protected $emails = [];
41
42
    /**
43
     * Register a listener for new emails.
44
     *
45
     * This calls our PHPUnit before each test it runs. It registers the MailRecorder "plugin" with Swift, so that we
46
     * can get a copy of each email that is sent during that test.
47
     *
48
     * @before
49
     */
50
    public function setUpMailTracking()
51
    {
52
        $register_plugin = function () {
53
            $this->resolveMailer()
54
                 ->getSwiftMailer()
55
                 ->registerPlugin(new MailRecorder($this));
56
        };
57
58
        $this->afterApplicationCreated(function () use ($register_plugin) {
0 ignored issues
show
Bug introduced by
It seems like afterApplicationCreated() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        $this->/** @scrutinizer ignore-call */ 
59
               afterApplicationCreated(function () use ($register_plugin) {
Loading history...
59
            $register_plugin();
60
        });
61
    }
62
63
    /**
64
     * Resolve the mailer from the IoC
65
     *
66
     * We are staying away from the Mail facade, so that we can support PHP 7.4 with Laravel 5.x
67
     *
68
     * @return Mailer
69
     * @throws BindingResolutionException
70
     */
71
    protected function resolveMailer()
72
    {
73
        return Container::getInstance()
74
                        ->make(Mailer::class);
75
    }
76
77
    /**
78
     * Retrieve the appropriate Swift message.
79
     *
80
     * @param Swift_Message|null $message
81
     *
82
     * @return Swift_Message
83
     */
84 13
    protected function getEmail(Swift_Message $message = null)
85
    {
86 13
        $this->seeEmailWasSent();
87
88 13
        return $message ?: $this->lastEmail();
89
    }
90
91
    /**
92
     * Retrieve the mostly recently sent Swift message.
93
     */
94 13
    protected function lastEmail()
95
    {
96 13
        return end($this->emails);
97
    }
98
99
    /**
100
     * Store a new Swift message.
101
     *
102
     * Collection of emails that were received by the MailRecorder plugin during a test.
103
     *
104
     * @param Swift_Message $email
105
     */
106 16
    public function recordMail(Swift_Message $email)
107
    {
108 16
        $this->emails[] = $email;
109 16
    }
110
111
    /**
112
     * Assert that the last email was bcc'ed to the given address.
113
     *
114
     * @param string             $bcc
115
     * @param Swift_Message|null $message
116
     *
117
     * @return $this
118
     */
119 1
    protected function seeEmailBcc($bcc, Swift_Message $message = null)
120
    {
121 1
        $this->assertArrayHasKey($bcc, (array)$this->getEmail($message)
0 ignored issues
show
Bug introduced by
It seems like assertArrayHasKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
        $this->/** @scrutinizer ignore-call */ 
122
               assertArrayHasKey($bcc, (array)$this->getEmail($message)
Loading history...
122 1
                                                   ->getBcc(), "The last email sent was not bcc'ed to $bcc.");
123
124 1
        return $this;
125
    }
126
127
    /**
128
     * Assert that the last email was cc'ed to the given address.
129
     *
130
     * @param string             $cc
131
     * @param Swift_Message|null $message
132
     *
133
     * @return $this
134
     */
135 1
    protected function seeEmailCc($cc, Swift_Message $message = null)
136
    {
137 1
        $this->assertArrayHasKey($cc, (array)$this->getEmail($message)
138 1
                                                  ->getCc(), "The last email sent was not cc'ed to $cc.");
139
140 1
        return $this;
141
    }
142
143
    /**
144
     * Assert that the last email's body contains the given text.
145
     *
146
     * @param string             $excerpt
147
     * @param Swift_Message|null $message
148
     *
149
     * @return $this
150
     */
151 1
    protected function seeEmailContains($excerpt, Swift_Message $message = null)
152
    {
153 1
        $this->assertStringContainsString($excerpt, $this->getEmail($message)
0 ignored issues
show
Bug introduced by
It seems like assertStringContainsString() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
        $this->/** @scrutinizer ignore-call */ 
154
               assertStringContainsString($excerpt, $this->getEmail($message)
Loading history...
154 1
                                                         ->getBody(), "The last email sent did not contain the provided body.");
155
156 1
        return $this;
157
    }
158
159
    /**
160
     * Assert that the last email's content type equals the given text.
161
     * For example, "text/plain" and "text/html" are valid content types for an email.
162
     *
163
     * @param string             $content_type
164
     * @param Swift_Message|null $message
165
     *
166
     * @return $this
167
     */
168 1
    protected function seeEmailContentTypeEquals($content_type, Swift_Message $message = null)
169
    {
170 1
        $this->assertEquals($content_type, $this->getEmail($message)
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

170
        $this->/** @scrutinizer ignore-call */ 
171
               assertEquals($content_type, $this->getEmail($message)
Loading history...
171 1
                                                ->getContentType(),
172 1
            "The last email sent did not contain the provided body.");
173
174 1
        return $this;
175
    }
176
177
    /**
178
     * Assert that the last email's body does not contain the given text.
179
     *
180
     * @param string             $excerpt
181
     * @param Swift_Message|null $message
182
     *
183
     * @return $this
184
     */
185 1
    protected function seeEmailDoesNotContain($excerpt, Swift_Message $message = null)
186
    {
187 1
        $this->assertStringNotContainsString($excerpt, $this->getEmail($message)
0 ignored issues
show
Bug introduced by
It seems like assertStringNotContainsString() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

187
        $this->/** @scrutinizer ignore-call */ 
188
               assertStringNotContainsString($excerpt, $this->getEmail($message)
Loading history...
188 1
                                                            ->getBody(), "The last email sent contained the provided text in its body.");
189
190 1
        return $this;
191
    }
192
193
    /**
194
     * Assert that the last email's body equals the given text.
195
     *
196
     * @param string             $body
197
     * @param Swift_Message|null $message
198
     *
199
     * @return $this
200
     */
201 1
    protected function seeEmailEquals($body, Swift_Message $message = null)
202
    {
203 1
        $this->assertEquals($body, $this->getEmail($message)
204 1
                                        ->getBody(), "The last email sent did not match the given email.");
205
206 1
        return $this;
207
    }
208
209
    /**
210
     * Assert that the last email was delivered by the given address.
211
     *
212
     * @param string             $sender
213
     * @param Swift_Message|null $message
214
     *
215
     * @return $this
216
     */
217 1
    protected function seeEmailFrom($sender, Swift_Message $message = null)
218
    {
219
        // TODO: Allow from to be an array to check email & name
220 1
        $this->assertArrayHasKey($sender, (array)$this->getEmail($message)
221 1
                                                      ->getFrom(), "The last email sent was not sent from $sender.");
222
223 1
        return $this;
224
    }
225
226
    /**
227
     * Assert that the last email had the given priority level.
228
     * The value is an integer where 1 is the highest priority and 5 is the lowest.
229
     *
230
     * @param integer            $priority
231
     * @param Swift_Message|null $message
232
     *
233
     * @return $this
234
     */
235 1
    protected function seeEmailPriorityEquals($priority, Swift_Message $message = null)
236
    {
237 1
        $actual_priority = $this->getEmail($message)
238 1
                                ->getPriority();
239
240 1
        $this->assertEquals($priority, $actual_priority,
241 1
            "The last email sent had a priority of $actual_priority but expected $priority.");
242
243 1
        return $this;
244
    }
245
246
    /**
247
     * Assert that the last email was set to reply to the given address.
248
     *
249
     * @param string             $reply_to
250
     * @param Swift_Message|null $message
251
     *
252
     * @return $this
253
     */
254 1
    protected function seeEmailReplyTo($reply_to, Swift_Message $message = null)
255
    {
256 1
        $this->assertArrayHasKey($reply_to, (array)$this->getEmail($message)
257 1
                                                        ->getReplyTo(),
258 1
                                 "The last email sent was not set to reply to $reply_to.");
259
260 1
        return $this;
261
    }
262
263
    /**
264
     * Assert that the given number of emails were sent.
265
     *
266
     * @param integer $count
267
     *
268
     * @return MailTracking $this
269
     * @deprecated in favor of seeEmailCountEquals
270
     */
271 1
    protected function seeEmailsSent($count)
272
    {
273 1
        return $this->seeEmailCountEquals($count);
274
    }
275
276
    /**
277
     * Assert that the given number of emails were sent.
278
     *
279
     * @param integer $count
280
     *
281
     * @return $this
282
     */
283 1
    protected function seeEmailCountEquals($count)
284
    {
285 1
        $emailsSent = count($this->emails);
286
287 1
        $this->assertCount($count, $this->emails, "Expected $count emails to have been sent, but $emailsSent were.");
0 ignored issues
show
Bug introduced by
It seems like assertCount() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

287
        $this->/** @scrutinizer ignore-call */ 
288
               assertCount($count, $this->emails, "Expected $count emails to have been sent, but $emailsSent were.");
Loading history...
288
289 1
        return $this;
290
    }
291
292
    /**
293
     * Assert that the last email's subject matches the given string.
294
     *
295
     * @param string             $subject
296
     * @param Swift_Message|null $message
297
     *
298
     * @return MailTracking $this
299
     * @deprecated in favor of seeEmailSubjectEquals
300
     */
301
    protected function seeEmailSubject($subject, Swift_Message $message = null)
302
    {
303
        return $this->seeEmailSubjectEquals($subject, $message);
304
    }
305
306
    /**
307
     * Assert that the last email's subject contains the given string.
308
     *
309
     * @param string             $excerpt
310
     * @param Swift_Message|null $message
311
     *
312
     * @return $this
313
     */
314 1
    protected function seeEmailSubjectContains($excerpt, Swift_Message $message = null)
315
    {
316 1
        $this->assertStringContainsString($excerpt, $this->getEmail($message)
317 1
                                                         ->getSubject(), "The last email sent did not contain the provided subject.");
318
319 1
        return $this;
320
    }
321
322
    /**
323
     * Assert that the last email's subject does not contain the given string.
324
     *
325
     * @param string             $excerpt
326
     * @param Swift_Message|null $message
327
     *
328
     * @return $this
329
     */
330 1
    protected function seeEmailSubjectDoesNotContain($excerpt, Swift_Message $message = null)
331
    {
332 1
        $this->assertStringNotContainsString($excerpt, $this->getEmail($message)
333 1
                                                            ->getSubject(), "The last email sent contained the provided text in its subject.");
334
335 1
        return $this;
336
    }
337
338
    /**
339
     * Assert that the last email's subject matches the given string.
340
     *
341
     * @param string             $subject
342
     * @param Swift_Message|null $message
343
     *
344
     * @return $this
345
     */
346 1
    protected function seeEmailSubjectEquals($subject, Swift_Message $message = null)
347
    {
348 1
        $this->assertEquals($subject, $this->getEmail($message)
349 1
                                           ->getSubject(), "The last email sent did not contain a subject of $subject.");
350
351 1
        return $this;
352
    }
353
354
    /**
355
     * Assert that the last email was sent to the given recipient.
356
     *
357
     * @param string             $recipient
358
     * @param Swift_Message|null $message
359
     *
360
     * @return $this
361
     */
362 1
    protected function seeEmailTo($recipient, Swift_Message $message = null)
363
    {
364 1
        $this->assertArrayHasKey($recipient, (array)$this->getEmail($message)
365 1
                                                         ->getTo(), "The last email sent was not sent to $recipient.");
366
367 1
        return $this;
368
    }
369
370
    /**
371
     * Assert that no emails were sent.
372
     *
373
     * @return $this
374
     */
375 1
    protected function seeEmailWasNotSent()
376
    {
377 1
        $emailsSent = count($this->emails);
378
379 1
        $this->assertEmpty($this->emails, "Did not expect any emails to have been sent, but found $emailsSent");
0 ignored issues
show
Bug introduced by
It seems like assertEmpty() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

379
        $this->/** @scrutinizer ignore-call */ 
380
               assertEmpty($this->emails, "Did not expect any emails to have been sent, but found $emailsSent");
Loading history...
380
381 1
        return $this;
382
    }
383
384
    /**
385
     * Assert that at least one email was sent.
386
     *
387
     * @return $this
388
     */
389 14
    protected function seeEmailWasSent()
390
    {
391 14
        $this->assertNotEmpty($this->emails, 'Expected at least one email to be sent, but found none.');
0 ignored issues
show
Bug introduced by
It seems like assertNotEmpty() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

391
        $this->/** @scrutinizer ignore-call */ 
392
               assertNotEmpty($this->emails, 'Expected at least one email to be sent, but found none.');
Loading history...
392
393 14
        return $this;
394
    }
395
}
396