Skip to content

Commit 48ed0eb

Browse files
[10.x] Passthru missing methods on EventFake & MailFake for macro accessibility (#45988)
* Allow mailer macros to be called when faked * Allow event macros to be called when faked * Add test * Revert breaking mail fake changes * Rename test * Add support for method passthru on `MailFake` * CS fixes * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 89ce187 commit 48ed0eb

File tree

5 files changed

+71
-5
lines changed

5 files changed

+71
-5
lines changed

src/Illuminate/Support/Facades/Mail.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Mail extends Facade
6565
*/
6666
public static function fake()
6767
{
68-
static::swap($fake = new MailFake);
68+
static::swap($fake = new MailFake(static::getFacadeRoot()));
6969

7070
return $fake;
7171
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
use Illuminate\Contracts\Events\Dispatcher;
77
use Illuminate\Support\Arr;
88
use Illuminate\Support\Str;
9+
use Illuminate\Support\Traits\ForwardsCalls;
910
use Illuminate\Support\Traits\ReflectsClosures;
1011
use PHPUnit\Framework\Assert as PHPUnit;
1112
use ReflectionFunction;
1213

1314
class EventFake implements Dispatcher
1415
{
15-
use ReflectsClosures;
16+
use ForwardsCalls, ReflectsClosures;
1617

1718
/**
1819
* The original event dispatcher.
@@ -377,4 +378,16 @@ public function until($event, $payload = [])
377378
{
378379
return $this->dispatch($event, $payload, true);
379380
}
381+
382+
/**
383+
* Handle dynamic method calls to the dispatcher.
384+
*
385+
* @param string $method
386+
* @param array $parameters
387+
* @return mixed
388+
*/
389+
public function __call($method, $parameters)
390+
{
391+
return $this->forwardCallTo($this->dispatcher, $method, $parameters);
392+
}
380393
}

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@
88
use Illuminate\Contracts\Mail\Mailer;
99
use Illuminate\Contracts\Mail\MailQueue;
1010
use Illuminate\Contracts\Queue\ShouldQueue;
11+
use Illuminate\Support\Traits\ForwardsCalls;
1112
use Illuminate\Support\Traits\ReflectsClosures;
1213
use PHPUnit\Framework\Assert as PHPUnit;
1314

1415
class MailFake implements Factory, Mailer, MailQueue
1516
{
16-
use ReflectsClosures;
17+
use ForwardsCalls, ReflectsClosures;
18+
19+
/**
20+
* The mailer instance.
21+
*
22+
* @var Mailer
23+
*/
24+
protected $mailer;
1725

1826
/**
1927
* The mailer currently being used to send a message.
@@ -36,6 +44,17 @@ class MailFake implements Factory, Mailer, MailQueue
3644
*/
3745
protected $queuedMailables = [];
3846

47+
/**
48+
* Create a new mail fake.
49+
*
50+
* @param Mailer $mailer
51+
* @return void
52+
*/
53+
public function __construct(Mailer $mailer)
54+
{
55+
$this->mailer = $mailer;
56+
}
57+
3958
/**
4059
* Assert if a mailable was sent based on a truth-test callback.
4160
*
@@ -431,4 +450,16 @@ public function forgetMailers()
431450

432451
return $this;
433452
}
453+
454+
/**
455+
* Handle dynamic method calls to the mailer.
456+
*
457+
* @param string $method
458+
* @param array $parameters
459+
* @return mixed
460+
*/
461+
public function __call($method, $parameters)
462+
{
463+
return $this->forwardCallTo($this->mailer, $method, $parameters);
464+
}
434465
}

tests/Integration/Events/EventFakeTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function testAssertListening()
151151

152152
Post::observe(new PostObserver);
153153

154-
($post = new Post)->save();
154+
(new Post)->save();
155155

156156
Event::assertListening('event', 'listener');
157157
Event::assertListening('event', PostEventSubscriber::class);
@@ -163,6 +163,13 @@ public function testAssertListening()
163163
Event::assertListening('eloquent.saving: '.Post::class, PostObserver::class.'@saving');
164164
Event::assertListening('eloquent.saving: '.Post::class, [PostObserver::class, 'saving']);
165165
}
166+
167+
public function testMissingMethodsAreForwarded()
168+
{
169+
Event::macro('foo', fn () => 'bar');
170+
171+
$this->assertEquals('bar', Event::fake()->foo());
172+
}
166173
}
167174

168175
class Post extends Model

tests/Support/SupportTestingMailFakeTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@
55
use Illuminate\Contracts\Queue\ShouldQueue;
66
use Illuminate\Contracts\Translation\HasLocalePreference;
77
use Illuminate\Mail\Mailable;
8+
use Illuminate\Mail\Mailer;
89
use Illuminate\Support\Testing\Fakes\MailFake;
10+
use Mockery as m;
911
use PHPUnit\Framework\ExpectationFailedException;
1012
use PHPUnit\Framework\TestCase;
1113

1214
class SupportTestingMailFakeTest extends TestCase
1315
{
16+
/**
17+
* @var \Mockery
18+
*/
19+
private $mailer;
20+
1421
/**
1522
* @var \Illuminate\Support\Testing\Fakes\MailFake
1623
*/
@@ -24,7 +31,8 @@ class SupportTestingMailFakeTest extends TestCase
2431
protected function setUp(): void
2532
{
2633
parent::setUp();
27-
$this->fake = new MailFake;
34+
$this->mailer = m::mock(Mailer::class);
35+
$this->fake = new MailFake($this->mailer);
2836
$this->mailable = new MailableStub;
2937
}
3038

@@ -213,6 +221,13 @@ public function testAssertSentWithClosure()
213221
return $mail->hasTo($user);
214222
});
215223
}
224+
225+
public function testMissingMethodsAreForwarded()
226+
{
227+
$this->mailer->shouldReceive('foo')->andReturn('bar');
228+
229+
$this->assertEquals('bar', $this->fake->foo());
230+
}
216231
}
217232

218233
class MailableStub extends Mailable

0 commit comments

Comments
 (0)