|
6 | 6 | use Carbon\Carbon; |
7 | 7 | use Illuminate\Support\Facades\DB; |
8 | 8 | use Illuminate\Support\Facades\Event; |
| 9 | +use Illuminate\Support\Facades\Mail; |
| 10 | +use Swift_Events_SendEvent; |
9 | 11 |
|
10 | 12 | class SendEmailsCommandTest extends TestCase |
11 | 13 | { |
| 14 | + /** @var Swift_Events_SendEvent[] */ |
| 15 | + public $sent = []; |
| 16 | + |
| 17 | + function setUp() |
| 18 | + { |
| 19 | + parent::setUp(); |
| 20 | + |
| 21 | + Mail::getSwiftMailer()->registerPlugin(new TestingMailEventListener($this)); |
| 22 | + } |
| 23 | + |
12 | 24 | /** @test */ |
13 | 25 | function an_email_should_be_marked_as_sent() |
14 | 26 | { |
@@ -142,4 +154,112 @@ function the_failed_status_and_error_is_cleared_if_a_previously_failed_email_is_ |
142 | 154 | $this->assertFalse($email->fresh()->hasFailed()); |
143 | 155 | $this->assertEmpty($email->fresh()->getError()); |
144 | 156 | } |
| 157 | + |
| 158 | + /** @test */ |
| 159 | + function it_sends_an_email() |
| 160 | + { |
| 161 | + $this->sendEmail(); |
| 162 | + |
| 163 | + Mail::shouldReceive('send') |
| 164 | + ->once(); |
| 165 | + |
| 166 | + $this->artisan('email:send'); |
| 167 | + } |
| 168 | + |
| 169 | + /** @test */ |
| 170 | + function the_email_has_a_correct_from_email_and_from_name() |
| 171 | + { |
| 172 | + $this->app['config']->set('mail.from.address', 'testfromaddress@gmail.com'); |
| 173 | + $this->app['config']->set('mail.from.name', 'From CI test'); |
| 174 | + |
| 175 | + $this->sendEmail(); |
| 176 | + |
| 177 | + $this->artisan('email:send'); |
| 178 | + |
| 179 | + $from = reset($this->sent)->getMessage()->getFrom(); |
| 180 | + |
| 181 | + $this->assertEquals('testfromaddress@gmail.com', key($from)); |
| 182 | + $this->assertEquals('From CI test', $from[key($from)]); |
| 183 | + } |
| 184 | + |
| 185 | + /** @test */ |
| 186 | + function it_sends_emails_to_the_correct_recipients() |
| 187 | + { |
| 188 | + $this->sendEmail(['recipient' => 'john@doe.com']); |
| 189 | + $this->artisan('email:send'); |
| 190 | + $to = reset($this->sent)->getMessage()->getTo(); |
| 191 | + $this->assertCount(1, $to); |
| 192 | + $this->assertArrayHasKey('john@doe.com', $to); |
| 193 | + |
| 194 | + $this->sent = []; |
| 195 | + $this->sendEmail(['recipient' => ['john@doe.com', 'john+2@doe.com']]); |
| 196 | + $this->artisan('email:send'); |
| 197 | + $to = reset($this->sent)->getMessage()->getTo(); |
| 198 | + $this->assertCount(2, $to); |
| 199 | + $this->assertArrayHasKey('john@doe.com', $to); |
| 200 | + $this->assertArrayHasKey('john+2@doe.com', $to); |
| 201 | + } |
| 202 | + |
| 203 | + /** @test */ |
| 204 | + function it_adds_the_cc_addresses() |
| 205 | + { |
| 206 | + $this->sendEmail(['cc' => 'cc@test.com']); |
| 207 | + $this->artisan('email:send'); |
| 208 | + $cc = reset($this->sent)->getMessage()->getCc(); |
| 209 | + $this->assertCount(1, $cc); |
| 210 | + $this->assertArrayHasKey('cc@test.com', $cc); |
| 211 | + |
| 212 | + $this->sent = []; |
| 213 | + $this->sendEmail(['cc' => ['cc@test.com', 'cc+2@test.com']]); |
| 214 | + $this->artisan('email:send'); |
| 215 | + $cc = reset($this->sent)->getMessage()->getCc(); |
| 216 | + $this->assertCount(2, $cc); |
| 217 | + $this->assertArrayHasKey('cc@test.com', $cc); |
| 218 | + $this->assertArrayHasKey('cc+2@test.com', $cc); |
| 219 | + } |
| 220 | + |
| 221 | + /** @test */ |
| 222 | + function it_adds_the_bcc_addresses() |
| 223 | + { |
| 224 | + $this->sendEmail(['bcc' => 'bcc@test.com']); |
| 225 | + $this->artisan('email:send'); |
| 226 | + $bcc = reset($this->sent)->getMessage()->getBcc(); |
| 227 | + $this->assertCount(1, $bcc); |
| 228 | + $this->assertArrayHasKey('bcc@test.com', $bcc); |
| 229 | + |
| 230 | + $this->sent = []; |
| 231 | + $this->sendEmail(['bcc' => ['bcc@test.com', 'bcc+2@test.com']]); |
| 232 | + $this->artisan('email:send'); |
| 233 | + $bcc = reset($this->sent)->getMessage()->getBcc(); |
| 234 | + $this->assertCount(2, $bcc); |
| 235 | + $this->assertArrayHasKey('bcc@test.com', $bcc); |
| 236 | + $this->assertArrayHasKey('bcc+2@test.com', $bcc); |
| 237 | + } |
| 238 | + |
| 239 | + /** @test */ |
| 240 | + function the_email_has_the_correct_subject() |
| 241 | + { |
| 242 | + $this->sendEmail(['subject' => 'Hello World']); |
| 243 | + |
| 244 | + $this->artisan('email:send'); |
| 245 | + |
| 246 | + $subject = reset($this->sent)->getMessage()->getSubject(); |
| 247 | + |
| 248 | + $this->assertEquals('Hello World', $subject); |
| 249 | + } |
| 250 | + |
| 251 | + /** @test */ |
| 252 | + function the_email_has_the_correct_body() |
| 253 | + { |
| 254 | + $this->sendEmail(['variables' => ['name' => 'John Doe']]); |
| 255 | + $this->artisan('email:send'); |
| 256 | + $body = reset($this->sent)->getMessage()->getBody(); |
| 257 | + $this->assertEquals(view('tests::dummy', ['name' => 'John Doe']), $body); |
| 258 | + |
| 259 | + $this->sent = []; |
| 260 | + $this->sendEmail(['variables' => []]); |
| 261 | + $this->artisan('email:send'); |
| 262 | + $body = reset($this->sent)->getMessage()->getBody(); |
| 263 | + $this->assertEquals(view('tests::dummy'), $body); |
| 264 | + } |
145 | 265 | } |
0 commit comments