Skip to content

Commit 8b76c76

Browse files
committed
added end to end tests
1 parent 6d1abc9 commit 8b76c76

File tree

4 files changed

+154
-1
lines changed

4 files changed

+154
-1
lines changed

src/Email.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ public function getRecipient()
8989
return $this->recipient;
9090
}
9191

92+
/**
93+
* Get the e-mail recipient(s) as string.
94+
*
95+
* @return string
96+
*/
97+
public function getRecipientsAsString()
98+
{
99+
$glue = ',';
100+
101+
return implode($glue, (array) $this->recipient);
102+
}
103+
92104
/**
93105
* Get the e-mail CC addresses.
94106
*

src/SendEmailsCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected function result($emails)
9797
$this->table($headers, $emails->map(function (Email $email) {
9898
return [
9999
$email->getId(),
100-
$email->getRecipient(),
100+
$email->getRecipientsAsString(),
101101
$email->getSubject(),
102102
$email->hasFailed() ? 'Failed' : 'OK',
103103
];

tests/SendEmailsCommandTest.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,21 @@
66
use Carbon\Carbon;
77
use Illuminate\Support\Facades\DB;
88
use Illuminate\Support\Facades\Event;
9+
use Illuminate\Support\Facades\Mail;
10+
use Swift_Events_SendEvent;
911

1012
class SendEmailsCommandTest extends TestCase
1113
{
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+
1224
/** @test */
1325
function an_email_should_be_marked_as_sent()
1426
{
@@ -142,4 +154,112 @@ function the_failed_status_and_error_is_cleared_if_a_previously_failed_email_is_
142154
$this->assertFalse($email->fresh()->hasFailed());
143155
$this->assertEmpty($email->fresh()->getError());
144156
}
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+
}
145265
}

tests/TestingMailEventListener.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Swift_Events_EventListener;
6+
7+
class TestingMailEventListener implements Swift_Events_EventListener
8+
{
9+
/** @var SendEmailsCommandTest */
10+
private $test;
11+
12+
public function __construct(TestCase $test)
13+
{
14+
$this->test = $test;
15+
}
16+
17+
public function beforeSendPerformed($event)
18+
{
19+
$this->test->sent[] = $event;
20+
}
21+
}

0 commit comments

Comments
 (0)