Skip to content

Commit b68b38f

Browse files
committed
refactor code and added tests
1 parent 77be0fb commit b68b38f

12 files changed

+155
-28
lines changed

database/migrations/2017_12_14_151403_create_emails_table.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public function up()
2121
$table->increments('id');
2222
$table->string('label')->nullable();
2323
$table->binary('recipient');
24-
$table->binary('from')>nullable();
2524
$table->binary('cc')->nullable();
2625
$table->binary('bcc')->nullable();
2726
$table->binary('subject');

database/migrations/2017_12_14_151421_add_attachments_to_emails_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function up()
1818
}
1919

2020
Schema::table('emails', function (Blueprint $table) {
21-
$table->binary('attachments')->after('body');
21+
$table->binary('attachments')->nullable()->after('body');
2222
});
2323
}
2424

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddFromToEmailsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
if (Schema::hasColumn('emails', 'from')) {
17+
return;
18+
}
19+
20+
Schema::table('emails', function (Blueprint $table) {
21+
$table->binary('from')->after('body')->nullable();
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
//
33+
}
34+
}

src/Email.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,33 @@ public function getRecipient()
8888
/**
8989
* Get the e-mail from.
9090
*
91-
* @return string|null
91+
* @return array
9292
*/
9393
public function getFrom()
9494
{
9595
return $this->from;
9696
}
9797

98+
/**
99+
* Get the e-mail from address.
100+
*
101+
* @return string|null
102+
*/
103+
public function getFromAddress()
104+
{
105+
return $this->from['address'] ?? config('mail.from.address');
106+
}
107+
108+
/**
109+
* Get the e-mail from address.
110+
*
111+
* @return string|null
112+
*/
113+
public function getFromName()
114+
{
115+
return $this->from['name'] ?? config('mail.from.name');
116+
}
117+
98118
/**
99119
* Get the e-mail recipient(s) as string.
100120
*
@@ -249,7 +269,7 @@ public function getError()
249269
*/
250270
public function hasFrom()
251271
{
252-
return strlen($this->getOriginal('from')) > 0;
272+
return is_array($this->from) && count($this->from) > 0;
253273
}
254274

255275
/**

src/EmailComposer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,13 @@ public function label($label)
9595
/**
9696
* Set the e-mail from address and aname.
9797
*
98-
* @param array $from
98+
* @param array $address
99+
* @param array $name
99100
* @return static
100101
*/
101-
public function from($address, $name)
102+
public function from($address = null, $name = null)
102103
{
103-
return $this->setData('from', ['address' => $address, 'name' => $name]);
104+
return $this->setData('from', compact('address', 'name'));
104105
}
105106

106107
/**

src/MailableReader.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@ public function read(EmailComposer $composer)
1616
{
1717
$this->readRecipient($composer);
1818

19-
20-
2119
$this->readFrom($composer);
2220

23-
24-
2521
$this->readCc($composer);
2622

2723
$this->readBcc($composer);
@@ -67,11 +63,12 @@ private function readRecipient(EmailComposer $composer)
6763
*/
6864
private function readFrom(EmailComposer $composer)
6965
{
70-
$from = $this->convertMailableAddresses(
71-
$composer->getData('mailable')->from
72-
);
66+
$from = reset($composer->getData('mailable')->from);
7367

74-
$composer->from($from);
68+
$composer->from(
69+
$from['address'],
70+
$from['name']
71+
);
7572
}
7673

7774
/**

src/Preparer.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,8 @@ private function prepareRecipient(EmailComposer $composer)
7575
*/
7676
private function prepareFrom(EmailComposer $composer)
7777
{
78-
if (!$composer->hasData('from')) {
79-
return;
80-
}
81-
8278
$composer->getEmail()->fill([
83-
'from' => json_encode($composer->getData('from')),
79+
'from' => json_encode($composer->getData('from', '')),
8480
]);
8581
}
8682

src/Sender.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,11 @@ private function getMailerInstance()
4545
*/
4646
private function buildMessage(Message $message, Email $email)
4747
{
48-
49-
$from = $email->hasFrom() ? $email->getFrom() : ['address' => config('mail.from.address'), 'name' => config('mail.from.name')];
50-
foreach ($from as $key => $val) {
51-
$from[$key] = !empty($val) ? $val : config('mail.from.' . $key);
52-
}
53-
5448
$message->to($email->getRecipient())
5549
->cc($email->hasCc() ? $email->getCc() : [])
5650
->bcc($email->hasBcc() ? $email->getBcc() : [])
5751
->subject($email->getSubject())
58-
->from($from['address'], $from['name'])
52+
->from($email->getFromAddress(), $email->getFromName())
5953
->setBody($email->getBody(), 'text/html');
6054

6155
$attachmentMap = [

tests/DatabaseInteractionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ function the_body_should_be_saved_correctly()
101101
$this->assertSame($expectedBody, $email->getBody());
102102
}
103103

104+
/** @test */
105+
function from_should_be_saved_correctly()
106+
{
107+
$email = $this->composeEmail()->send();
108+
109+
$this->assertFalse($email->hasFrom());
110+
$this->assertEquals(config('mail.from.address'), $email->getFromAddress());
111+
$this->assertEquals(config('mail.from.name'), $email->getFromName());
112+
113+
$email = $this->composeEmail()->from('marick@dolphiq.nl', 'Marick')->send();
114+
115+
$this->assertTrue($email->hasFrom());
116+
$this->assertEquals('marick@dolphiq.nl', $email->getFromAddress());
117+
$this->assertEquals('Marick', $email->getFromName());
118+
}
119+
104120
/** @test */
105121
function variables_should_be_saved_correctly()
106122
{

tests/EncryptionTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function the_variables_should_be_encrypted_and_decrypted()
6262
$email = $this->sendEmail(['variables' => ['name' => 'Jane Doe']]);
6363

6464
$this->assertEquals(
65-
['name'=> 'Jane Doe'],
65+
['name' => 'Jane Doe'],
6666
decrypt($email->getOriginal('variables'))
6767
);
6868

@@ -83,4 +83,18 @@ function the_body_should_be_encrypted_and_decrypted()
8383

8484
$this->assertEquals($expectedBody, $email->getBody());
8585
}
86+
87+
/** @test */
88+
function from_should_be_encrypted_and_decrypted()
89+
{
90+
$email = $this->composeEmail()->from('marick@dolphiq.nl', 'Marick')->send();
91+
92+
$expect = [
93+
'address' => 'marick@dolphiq.nl',
94+
'name' => 'Marick',
95+
];
96+
97+
$this->assertEquals($expect, decrypt($email->getOriginal('from')));
98+
$this->assertEquals($expect, $email->getFrom());
99+
}
86100
}

tests/MailableReaderTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,37 @@ function it_extracts_attachments()
7171
$this->assertEquals('order.html', $attachments[1]['attachment']['name']);
7272
$this->assertEquals('<p>Thanks for your oder</p>', $attachments[1]['attachment']['data']);
7373
}
74+
75+
/** @test */
76+
function it_extracts_the_from_address_and_or_name()
77+
{
78+
$email = Email::compose()->mailable(
79+
(new TestMailable())
80+
->from('marick@dolphiq.nl', 'Marick')
81+
)->send();
82+
83+
$this->assertTrue($email->hasFrom());
84+
$this->assertEquals('marick@dolphiq.nl', $email->getFromAddress());
85+
$this->assertEquals('Marick', $email->getFromName());
86+
87+
$email = Email::compose()->mailable(
88+
(new TestMailable())
89+
->from('marick@dolphiq.nl')
90+
)->send();
91+
92+
$this->assertTrue($email->hasFrom());
93+
$this->assertEquals('marick@dolphiq.nl', $email->getFromAddress());
94+
$this->assertEquals(config('mail.from.name'), $email->getFromName());
95+
96+
$email = Email::compose()->mailable(
97+
(new TestMailable())
98+
->from(null, 'Marick')
99+
)->send();
100+
101+
$this->assertTrue($email->hasFrom());
102+
$this->assertEquals(config('mail.from.address'), $email->getFromAddress());
103+
$this->assertEquals('Marick', $email->getFromName());
104+
}
74105
}
75106

76107
class TestMailable extends Mailable

tests/SenderTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,31 @@ function the_email_has_a_correct_from_email_and_from_name()
4343

4444
$this->assertEquals('testfromaddress@gmail.com', key($from));
4545
$this->assertEquals('From CI test', $from[key($from)]);
46+
47+
// custom from...
48+
$this->sent = [];
49+
50+
$this->composeEmail()->from('marick@dolphiq.nl', 'Marick')->send();
51+
$this->artisan('email:send');
52+
$from = reset($this->sent)->getMessage()->getFrom();
53+
$this->assertEquals('marick@dolphiq.nl', key($from));
54+
$this->assertEquals('Marick', $from[key($from)]);
55+
56+
// only address
57+
$this->sent = [];
58+
$this->composeEmail()->from('marick@dolphiq.nl')->send();
59+
$this->artisan('email:send');
60+
$from = reset($this->sent)->getMessage()->getFrom();
61+
$this->assertEquals('marick@dolphiq.nl', key($from));
62+
$this->assertEquals(config('mail.from.name'), $from[key($from)]);
63+
64+
// only name
65+
$this->sent = [];
66+
$this->composeEmail()->from(null, 'Marick')->send();
67+
$this->artisan('email:send');
68+
$from = reset($this->sent)->getMessage()->getFrom();
69+
$this->assertEquals(config('mail.from.address'), key($from));
70+
$this->assertEquals('Marick', $from[key($from)]);
4671
}
4772

4873
/** @test */

0 commit comments

Comments
 (0)