Skip to content

Commit 4041053

Browse files
committed
bug fixes
1 parent f91cb8c commit 4041053

File tree

5 files changed

+78
-12
lines changed

5 files changed

+78
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
composer.phar
22
/vendor/
3+
.phpunit.result.cache

.phpunit.result.cache

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/LaravelMailAssertions/Mailer.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,23 @@ public function empty()
3131
$this->emails = new Collection();
3232
}
3333

34+
/**
35+
* Get the emails collection
36+
* @return Collection
37+
*/
38+
public function getEmails(): Collection
39+
{
40+
return $this->emails;
41+
}
42+
3443
/**
3544
* Check if there is an email sent for a given address
3645
* @param string $address
3746
* @return bool
3847
*/
3948
public function hasEmailFor($address): bool
4049
{
41-
return $this->emails->filter(function ($email) use ($address) {
50+
return $this->emails->filter(function (Email $email) use ($address) {
4251
return $email->hasRecipient($address);
4352
})->count() > 0;
4453
}

src/LaravelMailAssertions/MakesMailAssertions.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,17 @@ public function fakeMail()
132132
*/
133133
public function getEmails(): Collection
134134
{
135-
return $this->getEmailSender()->emails;
135+
return $this->getEmailSender()->getEmails();
136136
}
137137

138138
/**
139-
* @param array|string $address
139+
* @param string $address
140140
* @return \Illuminate\Support\Collection
141141
*/
142-
public function getEmailsFor($address): Collection
142+
public function getEmailsFor(string $address): Collection
143143
{
144-
$addresses = collect((array) $address);
145-
146-
return $this->getEmails()->filter(function (Email $email) use ($addresses) {
147-
return $addresses->contains(function ($address) use ($email) {
148-
return $email->hasRecipient($address);
149-
});
144+
return $this->getEmails()->filter(function (Email $email) use ($address) {
145+
return $email->hasRecipient($address);
150146
});
151147
}
152148

@@ -163,7 +159,7 @@ public function getLastEmailFor($email)
163159
* Get the last email sent
164160
* @return \Tests\MailTheif\Email|null
165161
*/
166-
public function lastEmail()
162+
public function getLastEmail()
167163
{
168164
return $this->getEmailSender()->lastEmail();
169165
}

tests/MailUtilitiesTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Bpedroza\LaravelMailAssertions\Email;
6+
use Bpedroza\LaravelMailAssertions\MakesMailAssertions;
7+
8+
use PHPUnit\Framework\TestCase;
9+
10+
use Illuminate\Support\Collection;
11+
use Illuminate\Support\Facades\Mail;
12+
13+
class MailUtilitiesTest extends TestCase
14+
{
15+
use MakesMailAssertions;
16+
17+
public function test_get_emails()
18+
{
19+
$this->assertInstanceOf(Collection::class, $this->getEmails());
20+
}
21+
22+
public function test_get_emails_for_address()
23+
{
24+
Mail::raw('', function($mail) {
25+
$mail->to('test@example.org');
26+
});
27+
28+
$all = $this->getEmailsFor('test@example.org');
29+
30+
$this->assertInstanceOf(Collection::class, $all);
31+
$this->assertEquals(1, $all->count());
32+
}
33+
34+
public function test_get_last_email_for_address()
35+
{
36+
Mail::raw('email 1', function($mail) {
37+
$mail->to('test@example.org');
38+
});
39+
40+
Mail::raw('email 2', function($mail) {
41+
$mail->to('test@example.org');
42+
});
43+
44+
$lastEmail = $this->getLastEmailFor('test@example.org');
45+
46+
$this->assertInstanceOf(Email::class, $lastEmail);
47+
$this->assertTrue($lastEmail->contains('email 2'));
48+
}
49+
50+
public function test_get_last_email()
51+
{
52+
Mail::raw('', function($mail) {
53+
$mail->to('test@example.org');
54+
});
55+
56+
$lastEmail = $this->getLastEmail();
57+
58+
$this->assertInstanceOf(Email::class, $lastEmail);
59+
}
60+
61+
}

0 commit comments

Comments
 (0)