Completed
Push — master ( 8834f5...166c4c )
by Jimmy
07:09 queued 04:44
created

MailTracking::seeEmailContentTypeEquals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace Spinen\MailAssertions;
4
5
use Illuminate\Support\Facades\Mail;
6
use PHPUnit_Framework_TestCase;
7
use Swift_Message;
8
9
/**
10
 * Class MailTracking
11
 *
12
 * Trait to mixin to your test to allow for custom assertions when using PHPUnit with Laravel.
13
 *
14
 * This originally started out as a copy & paste from a video series that Jeffrey Way did on laracasts.com. If you do
15
 * not have an account on Laracasts, you should get one. It is an amazing resource to learn from. We used that
16
 * example & converted it to a package so that it would be easy to install. We have also expanded on the initial
17
 * assertions.
18
 *
19
 * I WANT IT CLEAR THAT THIS WOULD NOT HAVE HAPPENED WITHOUT THE INITIAL WORK OF JEFFREY WAY. WE ARE NOT CLAIMING TO
20
 * BE THE CREATORS OF THE CONCEPT.
21
 *
22
 * @package Spinen\MailAssertions
23
 * @see     https://gist.github.com/JeffreyWay/b501c53d958b07b8a332
24
 * @tutorial https://laracasts.com/series/phpunit-testing-in-laravel/episodes/12
25
 */
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()
50
    {
51
        Mail::getSwiftMailer()
52
            ->registerPlugin(new MailRecorder($this));
53
    }
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)
63
    {
64
        $this->seeEmailWasSent();
65
66
        return $message ?: $this->lastEmail();
67
    }
68
69
    /**
70
     * Retrieve the mostly recently sent Swift message.
71
     */
72
    protected function lastEmail()
73
    {
74
        return end($this->emails);
75
    }
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)
85
    {
86
        $this->emails[] = $email;
87
    }
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)
98
    {
99
        $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?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
100
                                                   ->getBcc(), "The last email sent was not bcc'ed to $bcc.");
101
102
        return $this;
103
    }
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)
114
    {
115
        $this->assertArrayHasKey($cc, (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?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
116
                                                  ->getCc(), "The last email sent was not cc'ed to $cc.");
117
118
        return $this;
119
    }
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)
130
    {
131
        $this->assertContains($excerpt, $this->getEmail($message)
0 ignored issues
show
Bug introduced by
It seems like assertContains() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
132
                                             ->getBody(), "The last email sent did not contain the provided body.");
133
134
        return $this;
135
    }
136
137
    /**
138
     * Assert that the last email's content type equals the given text.
139
     * For example, "text/plain" and "text/html" are valid content types for an email.
140
     *
141
     * @param string             $content_type
142
     * @param Swift_Message|null $message
143
     *
144
     * @return PHPUnit_Framework_TestCase $this
145
     */
146
    protected function seeEmailContentTypeEquals($content_type, Swift_Message $message = null)
147
    {
148
        $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?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
149
                                                ->getContentType(),
150
            "The last email sent did not contain the provided body.");
151
152
        return $this;
153
    }
154
155
    /**
156
     * Assert that the last email's body does not contain the given text.
157
     *
158
     * @param string             $excerpt
159
     * @param Swift_Message|null $message
160
     *
161
     * @return PHPUnit_Framework_TestCase $this
162
     */
163
    protected function seeEmailDoesNotContain($excerpt, Swift_Message $message = null)
164
    {
165
        $this->assertNotContains($excerpt, $this->getEmail($message)
0 ignored issues
show
Bug introduced by
It seems like assertNotContains() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
166
                                                ->getBody(),
167
                                 "The last email sent contained the provided text in its body.");
168
169
        return $this;
170
    }
171
172
    /**
173
     * Assert that the last email's body equals the given text.
174
     *
175
     * @param string             $body
176
     * @param Swift_Message|null $message
177
     *
178
     * @return PHPUnit_Framework_TestCase $this
179
     */
180
    protected function seeEmailEquals($body, Swift_Message $message = null)
181
    {
182
        $this->assertEquals($body, $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?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
183
                                        ->getBody(), "The last email sent did not match the given email.");
184
185
        return $this;
186
    }
187
188
    /**
189
     * Assert that the last email was delivered by the given address.
190
     *
191
     * @param string             $sender
192
     * @param Swift_Message|null $message
193
     *
194
     * @return PHPUnit_Framework_TestCase $this
195
     */
196
    protected function seeEmailFrom($sender, Swift_Message $message = null)
197
    {
198
        // TODO: Allow from to be an array to check email & name
199
        $this->assertArrayHasKey($sender, (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?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
200
                                                      ->getFrom(), "The last email sent was not sent from $sender.");
201
202
        return $this;
203
    }
204
205
    /**
206
     * Assert that the last email had the given priority level.
207
     * The value is an integer where 1 is the highest priority and 5 is the lowest.
208
     *
209
     * @param integer            $priority
210
     * @param Swift_Message|null $message
211
     *
212
     * @return PHPUnit_Framework_TestCase $this
213
     */
214
    protected function seeEmailPriorityEquals($priority, Swift_Message $message = null)
215
    {
216
        $actual_priority = $this->getEmail($message)
217
                                ->getPriority();
218
219
        $this->assertEquals($priority, $actual_priority,
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?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
220
            "The last email sent had a priority of $actual_priority but expected $priority.");
221
222
        return $this;
223
    }
224
225
    /**
226
     * Assert that the last email was set to reply to the given address.
227
     *
228
     * @param string             $reply_to
229
     * @param Swift_Message|null $message
230
     *
231
     * @return PHPUnit_Framework_TestCase $this
232
     */
233
    protected function seeEmailReplyTo($reply_to, Swift_Message $message = null)
234
    {
235
        $this->assertArrayHasKey($reply_to, (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?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
236
                                                        ->getReplyTo(),
237
                                 "The last email sent was not set to reply to $reply_to.");
238
239
        return $this;
240
    }
241
242
    /**
243
     * Assert that the given number of emails were sent.
244
     *
245
     * @param integer $count
246
     *
247
     * @return PHPUnit_Framework_TestCase $this
248
     * @deprecated in favor of seeEmailCountEquals
249
     */
250
    protected function seeEmailsSent($count)
251
    {
252
        return $this->seeEmailCountEquals($count);
253
    }
254
255
    /**
256
     * Assert that the given number of emails were sent.
257
     *
258
     * @param integer $count
259
     *
260
     * @return PHPUnit_Framework_TestCase $this
261
     */
262
    protected function seeEmailCountEquals($count)
263
    {
264
        $emailsSent = count($this->emails);
265
266
        $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?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
267
268
        return $this;
269
    }
270
271
    /**
272
     * Assert that the last email's subject matches the given string.
273
     *
274
     * @param string             $subject
275
     * @param Swift_Message|null $message
276
     *
277
     * @return PHPUnit_Framework_TestCase $this
278
     * @deprecated in favor of seeEmailSubjectEquals
279
     */
280
    protected function seeEmailSubject($subject, Swift_Message $message = null)
281
    {
282
        return $this->seeEmailSubjectEquals($subject, $message);
283
    }
284
285
    /**
286
     * Assert that the last email's subject contains the given string.
287
     *
288
     * @param string             $excerpt
289
     * @param Swift_Message|null $message
290
     *
291
     * @return PHPUnit_Framework_TestCase $this
292
     */
293
    protected function seeEmailSubjectContains($excerpt, Swift_Message $message = null)
294
    {
295
        $this->assertContains($excerpt, $this->getEmail($message)
0 ignored issues
show
Bug introduced by
It seems like assertContains() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
296
                                             ->getSubject(),
297
                              "The last email sent did not contain the provided subject.");
298
299
        return $this;
300
    }
301
302
    /**
303
     * Assert that the last email's subject does not contain the given string.
304
     *
305
     * @param string             $excerpt
306
     * @param Swift_Message|null $message
307
     *
308
     * @return PHPUnit_Framework_TestCase $this
309
     */
310
    protected function seeEmailSubjectDoesNotContain($excerpt, Swift_Message $message = null)
311
    {
312
        $this->assertNotContains($excerpt, $this->getEmail($message)
0 ignored issues
show
Bug introduced by
It seems like assertNotContains() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
313
                                                ->getSubject(),
314
                                 "The last email sent contained the provided text in its subject.");
315
316
        return $this;
317
    }
318
319
    /**
320
     * Assert that the last email's subject matches the given string.
321
     *
322
     * @param string             $subject
323
     * @param Swift_Message|null $message
324
     *
325
     * @return PHPUnit_Framework_TestCase $this
326
     */
327
    protected function seeEmailSubjectEquals($subject, Swift_Message $message = null)
328
    {
329
        $this->assertEquals($subject, $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?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
330
                                           ->getSubject(),
331
                            "The last email sent did not contain a subject of $subject.");
332
333
        return $this;
334
    }
335
336
    /**
337
     * Assert that the last email was sent to the given recipient.
338
     *
339
     * @param string             $recipient
340
     * @param Swift_Message|null $message
341
     *
342
     * @return PHPUnit_Framework_TestCase $this
343
     */
344
    protected function seeEmailTo($recipient, Swift_Message $message = null)
345
    {
346
        $this->assertArrayHasKey($recipient, (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?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
347
                                                         ->getTo(), "The last email sent was not sent to $recipient.");
348
349
        return $this;
350
    }
351
352
    /**
353
     * Assert that no emails were sent.
354
     *
355
     * @return PHPUnit_Framework_TestCase $this
356
     */
357
    protected function seeEmailWasNotSent()
358
    {
359
        $emailsSent = count($this->emails);
360
361
        $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?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
362
363
        return $this;
364
    }
365
366
    /**
367
     * Assert that at least one email was sent.
368
     *
369
     * @return PHPUnit_Framework_TestCase $this
370
     */
371
    protected function seeEmailWasSent()
372
    {
373
        $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?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
374
375
        return $this;
376
    }
377
}
378