Skip to content

Commit 2c51b48

Browse files
authored
[10.x] Use MailManager as underlying passthru object for MailFake (#46055)
* Update mail facade call forwarding * Test macro's work when faked
1 parent 24e0fb8 commit 2c51b48

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

src/Illuminate/Support/Testing/Fakes/MailFake.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Contracts\Mail\Mailer;
99
use Illuminate\Contracts\Mail\MailQueue;
1010
use Illuminate\Contracts\Queue\ShouldQueue;
11+
use Illuminate\Mail\MailManager;
1112
use Illuminate\Support\Traits\ForwardsCalls;
1213
use Illuminate\Support\Traits\ReflectsClosures;
1314
use PHPUnit\Framework\Assert as PHPUnit;
@@ -19,9 +20,9 @@ class MailFake implements Factory, Mailer, MailQueue
1920
/**
2021
* The mailer instance.
2122
*
22-
* @var Mailer
23+
* @var MailManager
2324
*/
24-
protected $mailer;
25+
protected $manager;
2526

2627
/**
2728
* The mailer currently being used to send a message.
@@ -47,12 +48,12 @@ class MailFake implements Factory, Mailer, MailQueue
4748
/**
4849
* Create a new mail fake.
4950
*
50-
* @param Mailer $mailer
51+
* @param MailManager $manager
5152
* @return void
5253
*/
53-
public function __construct(Mailer $mailer)
54+
public function __construct(MailManager $manager)
5455
{
55-
$this->mailer = $mailer;
56+
$this->manager = $manager;
5657
}
5758

5859
/**
@@ -460,6 +461,6 @@ public function forgetMailers()
460461
*/
461462
public function __call($method, $parameters)
462463
{
463-
return $this->forwardCallTo($this->mailer, $method, $parameters);
464+
return $this->forwardCallTo($this->manager, $method, $parameters);
464465
}
465466
}

tests/Support/SupportMailTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Support;
4+
5+
use Illuminate\Support\Facades\Mail;
6+
use Orchestra\Testbench\TestCase;
7+
8+
class SupportMailTest extends TestCase
9+
{
10+
public function testItRegisterAndCallMacros()
11+
{
12+
Mail::macro('test', fn (string $str) => $str === 'foo'
13+
? 'it works!'
14+
: 'it failed.',
15+
);
16+
17+
$this->assertEquals('it works!', Mail::test('foo'));
18+
}
19+
20+
public function testItRegisterAndCallMacrosWhenFaked()
21+
{
22+
Mail::macro('test', fn (string $str) => $str === 'foo'
23+
? 'it works!'
24+
: 'it failed.',
25+
);
26+
27+
Mail::fake();
28+
29+
$this->assertEquals('it works!', Mail::test('foo'));
30+
}
31+
}

tests/Support/SupportTestingMailFakeTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Illuminate\Contracts\Queue\ShouldQueue;
66
use Illuminate\Contracts\Translation\HasLocalePreference;
77
use Illuminate\Mail\Mailable;
8-
use Illuminate\Mail\Mailer;
8+
use Illuminate\Mail\MailManager;
99
use Illuminate\Support\Testing\Fakes\MailFake;
1010
use Mockery as m;
1111
use PHPUnit\Framework\ExpectationFailedException;
@@ -16,7 +16,7 @@ class SupportTestingMailFakeTest extends TestCase
1616
/**
1717
* @var \Mockery
1818
*/
19-
private $mailer;
19+
private $mailManager;
2020

2121
/**
2222
* @var \Illuminate\Support\Testing\Fakes\MailFake
@@ -31,8 +31,8 @@ class SupportTestingMailFakeTest extends TestCase
3131
protected function setUp(): void
3232
{
3333
parent::setUp();
34-
$this->mailer = m::mock(Mailer::class);
35-
$this->fake = new MailFake($this->mailer);
34+
$this->mailManager = m::mock(MailManager::class);
35+
$this->fake = new MailFake($this->mailManager);
3636
$this->mailable = new MailableStub;
3737
}
3838

@@ -224,7 +224,7 @@ public function testAssertSentWithClosure()
224224

225225
public function testMissingMethodsAreForwarded()
226226
{
227-
$this->mailer->shouldReceive('foo')->andReturn('bar');
227+
$this->mailManager->shouldReceive('foo')->andReturn('bar');
228228

229229
$this->assertEquals('bar', $this->fake->foo());
230230
}

0 commit comments

Comments
 (0)