Skip to content

Commit 43b3c7d

Browse files
committed
Refactor code and added tests
1 parent 2e8ca4a commit 43b3c7d

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

src/EmailComposer.php

+14-10
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,16 @@ public function mailable(Mailable $mailable)
218218
*/
219219
public function attach($file, $options = [])
220220
{
221-
$attachments = null;
221+
$validFileName = (is_string($file) && strlen($file) > 0);
222222

223-
if (! empty($file) || ! is_null($file)) {
224-
$attachments = $this->hasData('attachments') ? $this->getData('attachments') : [];
225-
226-
$attachments[] = compact('file', 'options');
223+
if (!$validFileName) {
224+
return $this;
227225
}
228226

227+
$attachments = $this->hasData('attachments') ? $this->getData('attachments') : [];
228+
229+
$attachments[] = compact('file', 'options');
230+
229231
return $this->setData('attachments', $attachments);
230232
}
231233

@@ -239,14 +241,16 @@ public function attach($file, $options = [])
239241
*/
240242
public function attachData($data, $name, array $options = [])
241243
{
242-
$attachments = null;
244+
$validData = (is_string($data) && strlen($data) > 0);
243245

244-
if (! empty($data) || ! is_null($data)) {
245-
$attachments = $this->hasData('rawAttachments') ? $this->getData('rawAttachments') : [];
246-
247-
$attachments[] = compact('data', 'name', 'options');
246+
if (!$validData) {
247+
return $this;
248248
}
249249

250+
$attachments = $this->hasData('rawAttachments') ? $this->getData('rawAttachments') : [];
251+
252+
$attachments[] = compact('data', 'name', 'options');
253+
250254
return $this->setData('rawAttachments', $attachments);
251255
}
252256

tests/SenderTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,28 @@ public function attachments_are_added_to_the_email()
167167
$this->assertEquals('application/pdf', $attachment->getContentType());
168168
}
169169

170+
/** @test */
171+
public function attachments_are_not_added_if_the_data_is_not_valid()
172+
{
173+
$this->sent = [];
174+
$this->composeEmail()->attach(null)->send();
175+
$this->artisan('email:send');
176+
$attachments = reset($this->sent)->getMessage()->getChildren();
177+
$this->assertCount(0, $attachments);
178+
179+
$this->sent = [];
180+
$this->composeEmail()->attach(false)->send();
181+
$this->artisan('email:send');
182+
$attachments = reset($this->sent)->getMessage()->getChildren();
183+
$this->assertCount(0, $attachments);
184+
185+
$this->sent = [];
186+
$this->composeEmail()->attach('')->send();
187+
$this->artisan('email:send');
188+
$attachments = reset($this->sent)->getMessage()->getChildren();
189+
$this->assertCount(0, $attachments);
190+
}
191+
170192
/** @test */
171193
public function raw_attachments_are_added_to_the_email()
172194
{
@@ -189,4 +211,26 @@ public function raw_attachments_are_added_to_the_email()
189211
$this->assertEquals('application/pdf', $attachment->getContentType());
190212
$this->assertContains('Hello CI!', $attachment->getBody());
191213
}
214+
215+
/** @test */
216+
public function raw_attachments_are_not_added_if_the_data_is_not_valid()
217+
{
218+
$this->sent = [];
219+
$this->composeEmail()->attachData(null, 'test.png')->send();
220+
$this->artisan('email:send');
221+
$attachments = reset($this->sent)->getMessage()->getChildren();
222+
$this->assertCount(0, $attachments);
223+
224+
$this->sent = [];
225+
$this->composeEmail()->attachData(false, 'test.png')->send();
226+
$this->artisan('email:send');
227+
$attachments = reset($this->sent)->getMessage()->getChildren();
228+
$this->assertCount(0, $attachments);
229+
230+
$this->sent = [];
231+
$this->composeEmail()->attachData('', 'test.png')->send();
232+
$this->artisan('email:send');
233+
$attachments = reset($this->sent)->getMessage()->getChildren();
234+
$this->assertCount(0, $attachments);
235+
}
192236
}

0 commit comments

Comments
 (0)