|
| 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 Jeffery 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 initial |
| 17 | + * assertions. |
| 18 | + * |
| 19 | + * I WANT IT CLEAR THAT THIS WOULD NOT HAVE HAPPENED WITHOUT THE INITIAL WORK OF JEFFERY 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 | + /** |
| 29 | + * Delivered emails. |
| 30 | + * |
| 31 | + * @var array |
| 32 | + */ |
| 33 | + protected $emails = []; |
| 34 | + |
| 35 | + /** |
| 36 | + * Register a listener for new emails. |
| 37 | + * |
| 38 | + * Called my PHPUnit before each test it run. It registers the MailRecorder "plugin" with Swift, so that we can |
| 39 | + * get a copy of each email that is sent during that test. |
| 40 | + * |
| 41 | + * @before |
| 42 | + */ |
| 43 | + public function setUpMailTracking() |
| 44 | + { |
| 45 | + Mail::getSwiftMailer() |
| 46 | + ->registerPlugin(new MailRecorder($this)); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Retrieve the appropriate swift message. |
| 51 | + * |
| 52 | + * @param Swift_Message $message |
| 53 | + * |
| 54 | + * @return Swift_Message |
| 55 | + */ |
| 56 | + protected function getEmail(Swift_Message $message = null) |
| 57 | + { |
| 58 | + $this->seeEmailWasSent(); |
| 59 | + |
| 60 | + return $message ?: $this->lastEmail(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Retrieve the mostly recently sent swift message. |
| 65 | + */ |
| 66 | + protected function lastEmail() |
| 67 | + { |
| 68 | + return end($this->emails); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Store a new swift message. |
| 73 | + * |
| 74 | + * Collection of emails that were received by the MailRecorder plugin during a test. |
| 75 | + * |
| 76 | + * @param Swift_Message $email |
| 77 | + */ |
| 78 | + public function recordMail(Swift_Message $email) |
| 79 | + { |
| 80 | + $this->emails[] = $email; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Assert that the last email's body contains the given text. |
| 85 | + * |
| 86 | + * @param string $excerpt |
| 87 | + * @param Swift_Message $message |
| 88 | + * |
| 89 | + * @return PHPUnit_Framework_TestCase $this |
| 90 | + */ |
| 91 | + protected function seeEmailContains($excerpt, Swift_Message $message = null) |
| 92 | + { |
| 93 | + $this->assertContains($excerpt, $this->getEmail($message) |
| 94 | + ->getBody(), "No email containing the provided body was found."); |
| 95 | + |
| 96 | + return $this; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Assert that the last email's body equals the given text. |
| 101 | + * |
| 102 | + * @param string $body |
| 103 | + * @param Swift_Message $message |
| 104 | + * |
| 105 | + * @return PHPUnit_Framework_TestCase $this |
| 106 | + */ |
| 107 | + protected function seeEmailEquals($body, Swift_Message $message = null) |
| 108 | + { |
| 109 | + $this->assertEquals($body, $this->getEmail($message) |
| 110 | + ->getBody(), "No email with the provided body was sent."); |
| 111 | + |
| 112 | + return $this; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Assert that the last email was delivered by the given address. |
| 117 | + * |
| 118 | + * @param string $sender |
| 119 | + * @param Swift_Message $message |
| 120 | + * |
| 121 | + * @return PHPUnit_Framework_TestCase $this |
| 122 | + */ |
| 123 | + protected function seeEmailFrom($sender, Swift_Message $message = null) |
| 124 | + { |
| 125 | + // TODO: Allow from to be an array to check email & name |
| 126 | + $this->assertArrayHasKey($sender, (array)$this->getEmail($message) |
| 127 | + ->getFrom(), "No email was sent from $sender."); |
| 128 | + |
| 129 | + return $this; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Assert that the given number of emails were sent. |
| 134 | + * |
| 135 | + * @param integer $count |
| 136 | + * |
| 137 | + * @return PHPUnit_Framework_TestCase $this |
| 138 | + */ |
| 139 | + protected function seeEmailsSent($count) |
| 140 | + { |
| 141 | + $emailsSent = count($this->emails); |
| 142 | + |
| 143 | + $this->assertCount($count, $this->emails, "Expected $count emails to have been sent, but $emailsSent were."); |
| 144 | + |
| 145 | + return $this; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Assert that the last email's subject matches the given string. |
| 150 | + * |
| 151 | + * @param string $subject |
| 152 | + * @param Swift_Message $message |
| 153 | + * |
| 154 | + * @return PHPUnit_Framework_TestCase $this |
| 155 | + */ |
| 156 | + protected function seeEmailSubject($subject, Swift_Message $message = null) |
| 157 | + { |
| 158 | + // TODO: Consider a subject contains like the message contains |
| 159 | + $this->assertEquals($subject, $this->getEmail($message) |
| 160 | + ->getSubject(), "No email with a subject of $subject was found."); |
| 161 | + |
| 162 | + return $this; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Assert that the last email was sent to the given recipient. |
| 167 | + * |
| 168 | + * @param string $recipient |
| 169 | + * @param Swift_Message $message |
| 170 | + * |
| 171 | + * @return PHPUnit_Framework_TestCase $this |
| 172 | + */ |
| 173 | + protected function seeEmailTo($recipient, Swift_Message $message = null) |
| 174 | + { |
| 175 | + $this->assertArrayHasKey($recipient, (array)$this->getEmail($message) |
| 176 | + ->getTo(), "No email was sent to $recipient."); |
| 177 | + |
| 178 | + return $this; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Assert that no emails were sent. |
| 183 | + * |
| 184 | + * @return PHPUnit_Framework_TestCase $this |
| 185 | + */ |
| 186 | + protected function seeEmailWasNotSent() |
| 187 | + { |
| 188 | + $this->assertEmpty($this->emails, 'Did not expect any emails to have been sent.'); |
| 189 | + |
| 190 | + return $this; |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Assert that at least one email was sent. |
| 195 | + * |
| 196 | + * @return PHPUnit_Framework_TestCase $this |
| 197 | + */ |
| 198 | + protected function seeEmailWasSent() |
| 199 | + { |
| 200 | + $this->assertNotEmpty($this->emails, 'No emails have been sent.'); |
| 201 | + |
| 202 | + return $this; |
| 203 | + } |
| 204 | +} |
0 commit comments