Skip to content

Commit 88e807f

Browse files
committed
Fix bug with instance being recreated across tests
1 parent 4041053 commit 88e807f

File tree

3 files changed

+58
-47
lines changed

3 files changed

+58
-47
lines changed

src/LaravelMailAssertions/Mailer.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class Mailer
1414
*/
1515
private $emails;
1616

17+
/**
18+
* @var Mailer
19+
*/
20+
private static $instance;
21+
1722
/**
1823
* Initialize the messages collection
1924
*/
@@ -23,21 +28,21 @@ public function __construct()
2328
}
2429

2530
/**
26-
* Clear out the emails
27-
* @return void
31+
* Get the emails collection
32+
* @return Collection
2833
*/
29-
public function empty()
34+
public function all(): Collection
3035
{
31-
$this->emails = new Collection();
36+
return $this->emails;
3237
}
3338

3439
/**
35-
* Get the emails collection
36-
* @return Collection
40+
* Clear out the emails
41+
* @return void
3742
*/
38-
public function getEmails(): Collection
43+
public function empty()
3944
{
40-
return $this->emails;
45+
$this->emails = new Collection();
4146
}
4247

4348
/**
@@ -76,12 +81,25 @@ public function hasEmailWithSubject(string $subject): bool
7681
})->count() > 0;
7782
}
7883

84+
/**
85+
* Get the current instance of the mailer
86+
* @return Mailer
87+
*/
88+
public static function instance(): Mailer
89+
{
90+
if(static::$instance === null) {
91+
static::$instance = new static();
92+
}
93+
94+
return static::$instance;
95+
}
96+
7997
/**
8098
* Get the last email sent
8199
*
82100
* @return Email|null
83101
*/
84-
public function lastEmail()
102+
public function last()
85103
{
86104
return $this->emails->last();
87105
}

src/LaravelMailAssertions/MakesMailAssertions.php

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ trait MakesMailAssertions
1616
/**
1717
* @var Mailer
1818
*/
19-
private static $emailSender;
19+
private $emailSender;
2020

2121
/**
2222
* See that there is no email sent
@@ -26,7 +26,7 @@ trait MakesMailAssertions
2626
public function assertEmailNotSent($message = 'An unexpected email was sent.')
2727
{
2828
$this->assertNull(
29-
$this->lastEmail(),
29+
$this->getLastEmail(),
3030
$message
3131
);
3232

@@ -41,7 +41,7 @@ public function assertEmailNotSent($message = 'An unexpected email was sent.')
4141
public function assertEmailSent($message = 'Unable to find a generated email.')
4242
{
4343
$this->assertNotNull(
44-
$this->lastEmail(),
44+
$this->getlastEmail(),
4545
$message
4646
);
4747

@@ -107,23 +107,34 @@ public function assertLastEmailContains($string)
107107
{
108108
$this->assertEmailSent();
109109
$this->assertTrue(
110-
$this->lastEmail()->contains($string),
111-
sprintf("%s \n Could not find [%s] in the above message.", $this->lastEmail()->getBody(), $string)
110+
$this->getLastEmail()->contains($string),
111+
sprintf("%s \n Could not find [%s] in the above message.", $this->getLastEmail()->getBody(), $string)
112112
);
113113
}
114114

115+
/**
116+
* @after
117+
*/
118+
public function emptyMail()
119+
{
120+
$this->getEmailSender()->empty();
121+
\Mockery::close();
122+
}
123+
115124
/**
116125
* @before
117126
*/
118127
public function fakeMail()
119128
{
120-
if(method_exists($this, 'afterApplicationCreated')) {
121-
$this->afterApplicationCreated(function () {
122-
$this->mockMailFacade();
123-
});
124-
} else {
125-
$this->mockMailFacade();
126-
}
129+
Mail::shouldReceive('send')->andReturnUsing(function (...$args) {
130+
$this->getEmailSender()->send(...$args);
131+
});
132+
133+
Mail::shouldReceive('raw')->andReturnUsing(function (...$args) {
134+
$this->getEmailSender()->sendRaw(...$args);
135+
});
136+
137+
Mail::shouldReceive('failures');
127138
}
128139

129140
/**
@@ -132,7 +143,7 @@ public function fakeMail()
132143
*/
133144
public function getEmails(): Collection
134145
{
135-
return $this->getEmailSender()->getEmails();
146+
return $this->getEmailSender()->all();
136147
}
137148

138149
/**
@@ -161,38 +172,15 @@ public function getLastEmailFor($email)
161172
*/
162173
public function getLastEmail()
163174
{
164-
return $this->getEmailSender()->lastEmail();
175+
return $this->getEmailSender()->last();
165176
}
166177

167-
168178
/**
169179
* Gets the sender class
170180
* @return Mailer
171181
*/
172182
private function getEmailSender()
173183
{
174-
if(static::$emailSender === null) {
175-
static::$emailSender = new Mailer;
176-
}
177-
178-
return static::$emailSender;
179-
}
180-
181-
/**
182-
* Mock the mail facade
183-
* @return void
184-
*/
185-
private function mockMailFacade()
186-
{
187-
$this->getEmailSender()->empty();
188-
Mail::shouldReceive('send')->andReturnUsing(function (...$args) {
189-
$this->getEmailSender()->send(...$args);
190-
});
191-
192-
Mail::shouldReceive('raw')->andReturnUsing(function (...$args) {
193-
$this->getEmailSender()->sendRaw(...$args);
194-
});
195-
196-
Mail::shouldReceive('failures');
184+
return Mailer::instance();
197185
}
198186
}

tests/MailUtilitiesTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ class MailUtilitiesTest extends TestCase
1616

1717
public function test_get_emails()
1818
{
19+
Mail::raw('', function($mail) {
20+
$mail->to('test@example.org');
21+
});
22+
1923
$this->assertInstanceOf(Collection::class, $this->getEmails());
24+
$this->assertEquals(1, $this->getEmails()->count());
2025
}
2126

2227
public function test_get_emails_for_address()

0 commit comments

Comments
 (0)