Skip to content

Commit 39f28e8

Browse files
joelbutcherdriesvintstaylorotwell
authored
[10.x] Facade Fake Awareness (#46188)
* Don't fake facades if they've already been faked * Fix auto-imported FQNs * Remove unused param * fix test * Update src/Illuminate/Support/Facades/Facade.php * Update src/Illuminate/Support/Facades/Queue.php * Update src/Illuminate/Support/Facades/Facade.php * Update Facade.php --------- Co-authored-by: Dries Vints <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 1225650 commit 39f28e8

File tree

13 files changed

+56
-24
lines changed

13 files changed

+56
-24
lines changed

src/Illuminate/Support/Facades/Bus.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ class Bus extends Facade
6060
*/
6161
public static function fake($jobsToFake = [], BatchRepository $batchRepository = null)
6262
{
63-
static::swap($fake = new BusFake(static::getFacadeRoot(), $jobsToFake, $batchRepository));
64-
65-
return $fake;
63+
return tap(new BusFake(static::getFacadeRoot(), $jobsToFake, $batchRepository), function ($fake) {
64+
if (! static::isFake()) {
65+
static::swap($fake);
66+
}
67+
});
6668
}
6769

6870
/**

src/Illuminate/Support/Facades/Event.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ class Event extends Facade
4747
*/
4848
public static function fake($eventsToFake = [])
4949
{
50-
static::swap($fake = new EventFake(static::getFacadeRoot(), $eventsToFake));
50+
return tap(new EventFake(static::getFacadeRoot(), $eventsToFake), function ($fake) {
51+
if (! static::isFake()) {
52+
static::swap($fake);
5153

52-
Model::setEventDispatcher($fake);
53-
Cache::refreshEventDispatcher();
54-
55-
return $fake;
54+
Model::setEventDispatcher($fake);
55+
Cache::refreshEventDispatcher();
56+
}
57+
});
5658
}
5759

5860
/**

src/Illuminate/Support/Facades/Facade.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Arr;
88
use Illuminate\Support\Js;
99
use Illuminate\Support\Str;
10+
use Illuminate\Support\Testing\Fakes\Fake;
1011
use Mockery;
1112
use Mockery\LegacyMockInterface;
1213
use RuntimeException;
@@ -183,6 +184,19 @@ public static function swap($instance)
183184
}
184185
}
185186

187+
/**
188+
* Determines whether a "fake" has been set as the facade instance.
189+
*
190+
* @return bool
191+
*/
192+
protected static function isFake()
193+
{
194+
$name = static::getFacadeAccessor();
195+
196+
return isset(static::$resolvedInstance[$name]) &&
197+
static::$resolvedInstance[$name] instanceof Fake;
198+
}
199+
186200
/**
187201
* Get the root object behind the facade.
188202
*

src/Illuminate/Support/Facades/Mail.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ class Mail extends Facade
6565
*/
6666
public static function fake()
6767
{
68-
static::swap($fake = new MailFake(static::getFacadeRoot()));
69-
70-
return $fake;
68+
return tap(new MailFake(static::getFacadeRoot()), function ($fake) {
69+
if (! static::isFake()) {
70+
static::swap($fake);
71+
}
72+
});
7173
}
7274

7375
/**

src/Illuminate/Support/Facades/Notification.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ class Notification extends Facade
4949
*/
5050
public static function fake()
5151
{
52-
static::swap($fake = new NotificationFake);
53-
54-
return $fake;
52+
return tap(new NotificationFake(), function ($fake) {
53+
if (! static::isFake()) {
54+
static::swap($fake);
55+
}
56+
});
5557
}
5658

5759
/**

src/Illuminate/Support/Facades/Queue.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ public static function popUsing($workerName, $callback)
7676
*/
7777
public static function fake($jobsToFake = [])
7878
{
79-
static::swap($fake = new QueueFake(static::getFacadeApplication(), $jobsToFake, static::getFacadeRoot()));
80-
81-
return $fake;
79+
tap(new QueueFake(static::getFacadeApplication(), $jobsToFake, static::getFacadeRoot()), function ($fake) {
80+
if (! static::isFake()) {
81+
static::swap($fake);
82+
}
83+
});
8284
}
8385

8486
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Illuminate\Support\Traits\ReflectsClosures;
1212
use PHPUnit\Framework\Assert as PHPUnit;
1313

14-
class BusFake implements QueueingDispatcher
14+
class BusFake implements Fake, QueueingDispatcher
1515
{
1616
use ReflectsClosures;
1717

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use PHPUnit\Framework\Assert as PHPUnit;
1212
use ReflectionFunction;
1313

14-
class EventFake implements Dispatcher
14+
class EventFake implements Dispatcher, Fake
1515
{
1616
use ForwardsCalls, ReflectsClosures;
1717

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Illuminate\Support\Testing\Fakes;
4+
5+
interface Fake
6+
{
7+
//
8+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Illuminate\Support\Traits\ReflectsClosures;
1414
use PHPUnit\Framework\Assert as PHPUnit;
1515

16-
class MailFake implements Factory, Mailer, MailQueue
16+
class MailFake implements Factory, Fake, Mailer, MailQueue
1717
{
1818
use ForwardsCalls, ReflectsClosures;
1919

0 commit comments

Comments
 (0)