| 1 | <?php |
| 2 | |
| 3 | namespace Illuminate\Support\Facades; |
| 4 | |
| 5 | use Illuminate\Queue\Worker; |
| 6 | use Illuminate\Support\Testing\Fakes\QueueFake; |
| 7 | |
| 8 | /** |
| 9 | * @method static void before(mixed $callback) |
| 10 | * @method static void after(mixed $callback) |
| 11 | * @method static void exceptionOccurred(mixed $callback) |
| 12 | * @method static void looping(mixed $callback) |
| 13 | * @method static void failing(mixed $callback) |
| 14 | * @method static void starting(mixed $callback) |
| 15 | * @method static void stopping(mixed $callback) |
| 16 | * @method static bool connected(string|null $name = null) |
| 17 | * @method static \Illuminate\Contracts\Queue\Queue connection(string|null $name = null) |
| 18 | * @method static void pause(string $connection, string $queue) |
| 19 | * @method static void pauseFor(string $connection, string $queue, \DateTimeInterface|\DateInterval|int $ttl) |
| 20 | * @method static void resume(string $connection, string $queue) |
| 21 | * @method static bool isPaused(string $connection, string $queue) |
| 22 | * @method static void withoutInterruptionPolling() |
| 23 | * @method static void extend(string $driver, \Closure $resolver) |
| 24 | * @method static void addConnector(string $driver, \Closure $resolver) |
| 25 | * @method static string getDefaultDriver() |
| 26 | * @method static void setDefaultDriver(string $name) |
| 27 | * @method static string getName(string|null $connection = null) |
| 28 | * @method static \Illuminate\Contracts\Foundation\Application getApplication() |
| 29 | * @method static \Illuminate\Queue\QueueManager setApplication(\Illuminate\Contracts\Foundation\Application $app) |
| 30 | * @method static int size(string|null $queue = null) |
| 31 | * @method static mixed push(string|object $job, mixed $data = '', string|null $queue = null) |
| 32 | * @method static mixed pushOn(string $queue, string|object $job, mixed $data = '') |
| 33 | * @method static mixed pushRaw(string $payload, string|null $queue = null, array $options = []) |
| 34 | * @method static mixed later(\DateTimeInterface|\DateInterval|int $delay, string|object $job, mixed $data = '', string|null $queue = null) |
| 35 | * @method static mixed laterOn(string $queue, \DateTimeInterface|\DateInterval|int $delay, string|object $job, mixed $data = '') |
| 36 | * @method static mixed bulk(array $jobs, mixed $data = '', string|null $queue = null) |
| 37 | * @method static \Illuminate\Contracts\Queue\Job|null pop(string|null $queue = null) |
| 38 | * @method static string getConnectionName() |
| 39 | * @method static \Illuminate\Contracts\Queue\Queue setConnectionName(string $name) |
| 40 | * @method static int pendingSize(string|null $queue = null) |
| 41 | * @method static int delayedSize(string|null $queue = null) |
| 42 | * @method static int reservedSize(string|null $queue = null) |
| 43 | * @method static int|null creationTimeOfOldestPendingJob(string|null $queue = null) |
| 44 | * @method static mixed getJobTries(mixed $job) |
| 45 | * @method static mixed getJobBackoff(mixed $job) |
| 46 | * @method static mixed getJobExpiration(mixed $job) |
| 47 | * @method static void createPayloadUsing(callable|null $callback) |
| 48 | * @method static array getConfig() |
| 49 | * @method static \Illuminate\Queue\Queue setConfig(array $config) |
| 50 | * @method static \Illuminate\Container\Container getContainer() |
| 51 | * @method static void setContainer(\Illuminate\Container\Container $container) |
| 52 | * @method static \Illuminate\Support\Testing\Fakes\QueueFake except(array|string $jobsToBeQueued) |
| 53 | * @method static void assertPushed(string|\Closure $job, callable|int|null $callback = null) |
| 54 | * @method static void assertPushedTimes(string $job, int $times = 1) |
| 55 | * @method static void assertPushedOn(string $queue, string|\Closure $job, callable|null $callback = null) |
| 56 | * @method static void assertPushedWithChain(string $job, array $expectedChain = [], callable|null $callback = null) |
| 57 | * @method static void assertPushedWithoutChain(string $job, callable|null $callback = null) |
| 58 | * @method static void assertClosurePushed(callable|int|null $callback = null) |
| 59 | * @method static void assertClosureNotPushed(callable|null $callback = null) |
| 60 | * @method static void assertNotPushed(string|\Closure $job, callable|null $callback = null) |
| 61 | * @method static void assertCount(int $expectedCount) |
| 62 | * @method static void assertNothingPushed() |
| 63 | * @method static \Illuminate\Support\Collection pushed(string $job, callable|null $callback = null) |
| 64 | * @method static \Illuminate\Support\Collection pushedRaw(null|\Closure $callback = null) |
| 65 | * @method static \Illuminate\Support\Collection listenersPushed(string $listenerClass, \Closure|null $callback = null) |
| 66 | * @method static bool hasPushed(string $job) |
| 67 | * @method static bool shouldFakeJob(object $job) |
| 68 | * @method static array pushedJobs() |
| 69 | * @method static array rawPushes() |
| 70 | * @method static \Illuminate\Support\Testing\Fakes\QueueFake serializeAndRestore(bool $serializeAndRestore = true) |
| 71 | * @method static void releaseUniqueJobLocks() |
| 72 | * |
| 73 | * @see \Illuminate\Queue\QueueManager |
| 74 | * @see \Illuminate\Queue\Queue |
| 75 | * @see \Illuminate\Support\Testing\Fakes\QueueFake |
| 76 | */ |
| 77 | class Queue extends Facade |
| 78 | { |
| 79 | /** |
| 80 | * Register a callback to be executed to pick jobs. |
| 81 | * |
| 82 | * @param string $workerName |
| 83 | * @param callable $callback |
| 84 | * @return void |
| 85 | */ |
| 86 | public static function popUsing($workerName, $callback) |
| 87 | { |
| 88 | Worker::popUsing($workerName, $callback); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Replace the bound instance with a fake. |
| 93 | * |
| 94 | * @param array|string $jobsToFake |
| 95 | * @return \Illuminate\Support\Testing\Fakes\QueueFake |
| 96 | */ |
| 97 | public static function fake($jobsToFake = []) |
| 98 | { |
| 99 | $actualQueueManager = static::isFake() |
| 100 | ? tap(static::getFacadeRoot(), fn ($fake) => $fake->releaseUniqueJobLocks())->queue |
| 101 | : static::getFacadeRoot(); |
| 102 | |
| 103 | return tap(new QueueFake(static::getFacadeApplication(), $jobsToFake, $actualQueueManager), function ($fake) { |
| 104 | static::swap($fake); |
| 105 | }); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Replace the bound instance with a fake that fakes all jobs except the given jobs. |
| 110 | * |
| 111 | * @param string[]|string $jobsToAllow |
| 112 | * @return \Illuminate\Support\Testing\Fakes\QueueFake |
| 113 | */ |
| 114 | public static function fakeExcept($jobsToAllow) |
| 115 | { |
| 116 | return static::fake()->except($jobsToAllow); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Replace the bound instance with a fake during the given callable's execution. |
| 121 | * |
| 122 | * @param callable $callable |
| 123 | * @param array $jobsToFake |
| 124 | * @return mixed |
| 125 | */ |
| 126 | public static function fakeFor(callable $callable, array $jobsToFake = []) |
| 127 | { |
| 128 | $originalQueueManager = static::getFacadeRoot(); |
| 129 | |
| 130 | static::fake($jobsToFake); |
| 131 | |
| 132 | try { |
| 133 | return $callable(); |
| 134 | } finally { |
| 135 | static::swap($originalQueueManager); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Replace the bound instance with a fake during the given callable's execution. |
| 141 | * |
| 142 | * @param callable $callable |
| 143 | * @param array $jobsToAllow |
| 144 | * @return mixed |
| 145 | */ |
| 146 | public static function fakeExceptFor(callable $callable, array $jobsToAllow = []) |
| 147 | { |
| 148 | $originalQueueManager = static::getFacadeRoot(); |
| 149 | |
| 150 | static::fakeExcept($jobsToAllow); |
| 151 | |
| 152 | try { |
| 153 | return $callable(); |
| 154 | } finally { |
| 155 | static::swap($originalQueueManager); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Get the registered name of the component. |
| 161 | * |
| 162 | * @return string |
| 163 | */ |
| 164 | protected static function getFacadeAccessor() |
| 165 | { |
| 166 | return 'queue'; |
| 167 | } |
| 168 | } |