| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Support\Facades;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Database\Eloquent\Model;
|
|
|
6 |
use Illuminate\Support\Testing\Fakes\EventFake;
|
|
|
7 |
|
|
|
8 |
/**
|
|
|
9 |
* @method static void listen(\Closure|string|array $events, \Closure|string|array|null $listener = null)
|
|
|
10 |
* @method static bool hasListeners(string $eventName)
|
|
|
11 |
* @method static bool hasWildcardListeners(string $eventName)
|
|
|
12 |
* @method static void push(string $event, object|array $payload = [])
|
|
|
13 |
* @method static void flush(string $event)
|
|
|
14 |
* @method static void subscribe(object|string $subscriber)
|
|
|
15 |
* @method static array|null until(string|object $event, mixed $payload = [])
|
|
|
16 |
* @method static array|null dispatch(string|object $event, mixed $payload = [], bool $halt = false)
|
|
|
17 |
* @method static array getListeners(string $eventName)
|
|
|
18 |
* @method static \Closure makeListener(\Closure|string|array $listener, bool $wildcard = false)
|
|
|
19 |
* @method static \Closure createClassListener(string $listener, bool $wildcard = false)
|
|
|
20 |
* @method static void forget(string $event)
|
|
|
21 |
* @method static void forgetPushed()
|
|
|
22 |
* @method static \Illuminate\Events\Dispatcher setQueueResolver(callable $resolver)
|
|
|
23 |
* @method static array getRawListeners()
|
|
|
24 |
* @method static void macro(string $name, object|callable $macro)
|
|
|
25 |
* @method static void mixin(object $mixin, bool $replace = true)
|
|
|
26 |
* @method static bool hasMacro(string $name)
|
|
|
27 |
* @method static void flushMacros()
|
|
|
28 |
* @method static \Illuminate\Support\Testing\Fakes\EventFake except(array|string $eventsToDispatch)
|
|
|
29 |
* @method static void assertListening(string $expectedEvent, string|array $expectedListener)
|
|
|
30 |
* @method static void assertDispatched(string|\Closure $event, callable|int|null $callback = null)
|
|
|
31 |
* @method static void assertDispatchedTimes(string $event, int $times = 1)
|
|
|
32 |
* @method static void assertNotDispatched(string|\Closure $event, callable|null $callback = null)
|
|
|
33 |
* @method static void assertNothingDispatched()
|
|
|
34 |
* @method static \Illuminate\Support\Collection dispatched(string $event, callable|null $callback = null)
|
|
|
35 |
* @method static bool hasDispatched(string $event)
|
|
|
36 |
*
|
|
|
37 |
* @see \Illuminate\Events\Dispatcher
|
|
|
38 |
* @see \Illuminate\Support\Testing\Fakes\EventFake
|
|
|
39 |
*/
|
|
|
40 |
class Event extends Facade
|
|
|
41 |
{
|
|
|
42 |
/**
|
|
|
43 |
* Replace the bound instance with a fake.
|
|
|
44 |
*
|
|
|
45 |
* @param array|string $eventsToFake
|
|
|
46 |
* @return \Illuminate\Support\Testing\Fakes\EventFake
|
|
|
47 |
*/
|
|
|
48 |
public static function fake($eventsToFake = [])
|
|
|
49 |
{
|
|
|
50 |
static::swap($fake = new EventFake(static::getFacadeRoot(), $eventsToFake));
|
|
|
51 |
|
|
|
52 |
Model::setEventDispatcher($fake);
|
|
|
53 |
Cache::refreshEventDispatcher();
|
|
|
54 |
|
|
|
55 |
return $fake;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Replace the bound instance with a fake that fakes all events except the given events.
|
|
|
60 |
*
|
|
|
61 |
* @param string[]|string $eventsToAllow
|
|
|
62 |
* @return \Illuminate\Support\Testing\Fakes\EventFake
|
|
|
63 |
*/
|
|
|
64 |
public static function fakeExcept($eventsToAllow)
|
|
|
65 |
{
|
|
|
66 |
return static::fake([
|
|
|
67 |
function ($eventName) use ($eventsToAllow) {
|
|
|
68 |
return ! in_array($eventName, (array) $eventsToAllow);
|
|
|
69 |
},
|
|
|
70 |
]);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Replace the bound instance with a fake during the given callable's execution.
|
|
|
75 |
*
|
|
|
76 |
* @param callable $callable
|
|
|
77 |
* @param array $eventsToFake
|
|
|
78 |
* @return mixed
|
|
|
79 |
*/
|
|
|
80 |
public static function fakeFor(callable $callable, array $eventsToFake = [])
|
|
|
81 |
{
|
|
|
82 |
$originalDispatcher = static::getFacadeRoot();
|
|
|
83 |
|
|
|
84 |
static::fake($eventsToFake);
|
|
|
85 |
|
|
|
86 |
return tap($callable(), function () use ($originalDispatcher) {
|
|
|
87 |
static::swap($originalDispatcher);
|
|
|
88 |
|
|
|
89 |
Model::setEventDispatcher($originalDispatcher);
|
|
|
90 |
Cache::refreshEventDispatcher();
|
|
|
91 |
});
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Replace the bound instance with a fake during the given callable's execution.
|
|
|
96 |
*
|
|
|
97 |
* @param callable $callable
|
|
|
98 |
* @param array $eventsToAllow
|
|
|
99 |
* @return mixed
|
|
|
100 |
*/
|
|
|
101 |
public static function fakeExceptFor(callable $callable, array $eventsToAllow = [])
|
|
|
102 |
{
|
|
|
103 |
$originalDispatcher = static::getFacadeRoot();
|
|
|
104 |
|
|
|
105 |
static::fakeExcept($eventsToAllow);
|
|
|
106 |
|
|
|
107 |
return tap($callable(), function () use ($originalDispatcher) {
|
|
|
108 |
static::swap($originalDispatcher);
|
|
|
109 |
|
|
|
110 |
Model::setEventDispatcher($originalDispatcher);
|
|
|
111 |
Cache::refreshEventDispatcher();
|
|
|
112 |
});
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Get the registered name of the component.
|
|
|
117 |
*
|
|
|
118 |
* @return string
|
|
|
119 |
*/
|
|
|
120 |
protected static function getFacadeAccessor()
|
|
|
121 |
{
|
|
|
122 |
return 'events';
|
|
|
123 |
}
|
|
|
124 |
}
|