| 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(\Illuminate\Events\QueuedClosure|callable|array|string $events, \Illuminate\Events\QueuedClosure|callable|array|string|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 \Illuminate\Events\Dispatcher setTransactionManagerResolver(callable $resolver) |
| 24 | * @method static mixed defer(callable $callback, string[]|null $events = null) |
| 25 | * @method static array getRawListeners() |
| 26 | * @method static void macro(string $name, object|callable $macro) |
| 27 | * @method static void mixin(object $mixin, bool $replace = true) |
| 28 | * @method static bool hasMacro(string $name) |
| 29 | * @method static void flushMacros() |
| 30 | * @method static \Illuminate\Support\Testing\Fakes\EventFake except(array|string $eventsToDispatch) |
| 31 | * @method static void assertListening(string $expectedEvent, string|array $expectedListener) |
| 32 | * @method static void assertDispatched(string|\Closure $event, callable|int|null $callback = null) |
| 33 | * @method static void assertDispatchedOnce(string $event) |
| 34 | * @method static void assertDispatchedTimes(string $event, int $times = 1) |
| 35 | * @method static void assertNotDispatched(string|\Closure $event, callable|null $callback = null) |
| 36 | * @method static void assertNothingDispatched() |
| 37 | * @method static \Illuminate\Support\Collection dispatched(string $event, callable|null $callback = null) |
| 38 | * @method static bool hasDispatched(string $event) |
| 39 | * @method static array dispatchedEvents() |
| 40 | * |
| 41 | * @see \Illuminate\Events\Dispatcher |
| 42 | * @see \Illuminate\Support\Testing\Fakes\EventFake |
| 43 | */ |
| 44 | class Event extends Facade |
| 45 | { |
| 46 | /** |
| 47 | * Replace the bound instance with a fake. |
| 48 | * |
| 49 | * @param array|string $eventsToFake |
| 50 | * @return \Illuminate\Support\Testing\Fakes\EventFake |
| 51 | */ |
| 52 | public static function fake($eventsToFake = []) |
| 53 | { |
| 54 | $actualDispatcher = static::isFake() |
| 55 | ? static::getFacadeRoot()->dispatcher |
| 56 | : static::getFacadeRoot(); |
| 57 | |
| 58 | return tap(new EventFake($actualDispatcher, $eventsToFake), function ($fake) { |
| 59 | static::swap($fake); |
| 60 | |
| 61 | Model::setEventDispatcher($fake); |
| 62 | Cache::refreshEventDispatcher(); |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Replace the bound instance with a fake that fakes all events except the given events. |
| 68 | * |
| 69 | * @param string[]|string $eventsToAllow |
| 70 | * @return \Illuminate\Support\Testing\Fakes\EventFake |
| 71 | */ |
| 72 | public static function fakeExcept($eventsToAllow) |
| 73 | { |
| 74 | return static::fake([ |
| 75 | function ($eventName) use ($eventsToAllow) { |
| 76 | return ! in_array($eventName, (array) $eventsToAllow); |
| 77 | }, |
| 78 | ]); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Replace the bound instance with a fake during the given callable's execution. |
| 83 | * |
| 84 | * @param callable $callable |
| 85 | * @param array $eventsToFake |
| 86 | * @return mixed |
| 87 | */ |
| 88 | public static function fakeFor(callable $callable, array $eventsToFake = []) |
| 89 | { |
| 90 | $originalDispatcher = static::getFacadeRoot(); |
| 91 | |
| 92 | static::fake($eventsToFake); |
| 93 | |
| 94 | try { |
| 95 | return $callable(); |
| 96 | } finally { |
| 97 | static::swap($originalDispatcher); |
| 98 | |
| 99 | Model::setEventDispatcher($originalDispatcher); |
| 100 | Cache::refreshEventDispatcher(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Replace the bound instance with a fake during the given callable's execution. |
| 106 | * |
| 107 | * @param callable $callable |
| 108 | * @param array $eventsToAllow |
| 109 | * @return mixed |
| 110 | */ |
| 111 | public static function fakeExceptFor(callable $callable, array $eventsToAllow = []) |
| 112 | { |
| 113 | $originalDispatcher = static::getFacadeRoot(); |
| 114 | |
| 115 | static::fakeExcept($eventsToAllow); |
| 116 | |
| 117 | try { |
| 118 | return $callable(); |
| 119 | } finally { |
| 120 | static::swap($originalDispatcher); |
| 121 | |
| 122 | Model::setEventDispatcher($originalDispatcher); |
| 123 | Cache::refreshEventDispatcher(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get the registered name of the component. |
| 129 | * |
| 130 | * @return string |
| 131 | */ |
| 132 | protected static function getFacadeAccessor() |
| 133 | { |
| 134 | return 'events'; |
| 135 | } |
| 136 | } |