| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Support\Testing\Fakes;
|
|
|
4 |
|
|
|
5 |
use Closure;
|
|
|
6 |
use Illuminate\Bus\BatchRepository;
|
|
|
7 |
use Illuminate\Bus\PendingBatch;
|
|
|
8 |
use Illuminate\Contracts\Bus\QueueingDispatcher;
|
|
|
9 |
use Illuminate\Support\Arr;
|
|
|
10 |
use Illuminate\Support\Collection;
|
|
|
11 |
use Illuminate\Support\Traits\ReflectsClosures;
|
|
|
12 |
use PHPUnit\Framework\Assert as PHPUnit;
|
|
|
13 |
|
|
|
14 |
class BusFake implements QueueingDispatcher
|
|
|
15 |
{
|
|
|
16 |
use ReflectsClosures;
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* The original Bus dispatcher implementation.
|
|
|
20 |
*
|
|
|
21 |
* @var \Illuminate\Contracts\Bus\QueueingDispatcher
|
|
|
22 |
*/
|
|
|
23 |
protected $dispatcher;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* The job types that should be intercepted instead of dispatched.
|
|
|
27 |
*
|
|
|
28 |
* @var array
|
|
|
29 |
*/
|
|
|
30 |
protected $jobsToFake = [];
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* The job types that should be dispatched instead of faked.
|
|
|
34 |
*
|
|
|
35 |
* @var array
|
|
|
36 |
*/
|
|
|
37 |
protected $jobsToDispatch = [];
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* The fake repository to track batched jobs.
|
|
|
41 |
*
|
|
|
42 |
* @var \Illuminate\Bus\BatchRepository
|
|
|
43 |
*/
|
|
|
44 |
protected $batchRepository;
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* The commands that have been dispatched.
|
|
|
48 |
*
|
|
|
49 |
* @var array
|
|
|
50 |
*/
|
|
|
51 |
protected $commands = [];
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* The commands that have been dispatched synchronously.
|
|
|
55 |
*
|
|
|
56 |
* @var array
|
|
|
57 |
*/
|
|
|
58 |
protected $commandsSync = [];
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* The commands that have been dispatched after the response has been sent.
|
|
|
62 |
*
|
|
|
63 |
* @var array
|
|
|
64 |
*/
|
|
|
65 |
protected $commandsAfterResponse = [];
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* The batches that have been dispatched.
|
|
|
69 |
*
|
|
|
70 |
* @var array
|
|
|
71 |
*/
|
|
|
72 |
protected $batches = [];
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Create a new bus fake instance.
|
|
|
76 |
*
|
|
|
77 |
* @param \Illuminate\Contracts\Bus\QueueingDispatcher $dispatcher
|
|
|
78 |
* @param array|string $jobsToFake
|
|
|
79 |
* @param \Illuminate\Bus\BatchRepository|null $batchRepository
|
|
|
80 |
* @return void
|
|
|
81 |
*/
|
|
|
82 |
public function __construct(QueueingDispatcher $dispatcher, $jobsToFake = [], BatchRepository $batchRepository = null)
|
|
|
83 |
{
|
|
|
84 |
$this->dispatcher = $dispatcher;
|
|
|
85 |
$this->jobsToFake = Arr::wrap($jobsToFake);
|
|
|
86 |
$this->batchRepository = $batchRepository ?: new BatchRepositoryFake;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Specify the jobs that should be dispatched instead of faked.
|
|
|
91 |
*
|
|
|
92 |
* @param array|string $jobsToDispatch
|
|
|
93 |
* @return void
|
|
|
94 |
*/
|
|
|
95 |
public function except($jobsToDispatch)
|
|
|
96 |
{
|
|
|
97 |
$this->jobsToDispatch = array_merge($this->jobsToDispatch, Arr::wrap($jobsToDispatch));
|
|
|
98 |
|
|
|
99 |
return $this;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Assert if a job was dispatched based on a truth-test callback.
|
|
|
104 |
*
|
|
|
105 |
* @param string|\Closure $command
|
|
|
106 |
* @param callable|int|null $callback
|
|
|
107 |
* @return void
|
|
|
108 |
*/
|
|
|
109 |
public function assertDispatched($command, $callback = null)
|
|
|
110 |
{
|
|
|
111 |
if ($command instanceof Closure) {
|
|
|
112 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command];
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
if (is_numeric($callback)) {
|
|
|
116 |
return $this->assertDispatchedTimes($command, $callback);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
PHPUnit::assertTrue(
|
|
|
120 |
$this->dispatched($command, $callback)->count() > 0 ||
|
|
|
121 |
$this->dispatchedAfterResponse($command, $callback)->count() > 0 ||
|
|
|
122 |
$this->dispatchedSync($command, $callback)->count() > 0,
|
|
|
123 |
"The expected [{$command}] job was not dispatched."
|
|
|
124 |
);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Assert if a job was pushed a number of times.
|
|
|
129 |
*
|
| 621 |
lars |
130 |
* @param string|\Closure $command
|
| 148 |
lars |
131 |
* @param int $times
|
|
|
132 |
* @return void
|
|
|
133 |
*/
|
|
|
134 |
public function assertDispatchedTimes($command, $times = 1)
|
|
|
135 |
{
|
| 621 |
lars |
136 |
$callback = null;
|
| 148 |
lars |
137 |
|
| 621 |
lars |
138 |
if ($command instanceof Closure) {
|
|
|
139 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command];
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
$count = $this->dispatched($command, $callback)->count() +
|
|
|
143 |
$this->dispatchedAfterResponse($command, $callback)->count() +
|
|
|
144 |
$this->dispatchedSync($command, $callback)->count();
|
|
|
145 |
|
| 148 |
lars |
146 |
PHPUnit::assertSame(
|
|
|
147 |
$times, $count,
|
|
|
148 |
"The expected [{$command}] job was pushed {$count} times instead of {$times} times."
|
|
|
149 |
);
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Determine if a job was dispatched based on a truth-test callback.
|
|
|
154 |
*
|
|
|
155 |
* @param string|\Closure $command
|
|
|
156 |
* @param callable|null $callback
|
|
|
157 |
* @return void
|
|
|
158 |
*/
|
|
|
159 |
public function assertNotDispatched($command, $callback = null)
|
|
|
160 |
{
|
|
|
161 |
if ($command instanceof Closure) {
|
|
|
162 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command];
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
PHPUnit::assertTrue(
|
|
|
166 |
$this->dispatched($command, $callback)->count() === 0 &&
|
|
|
167 |
$this->dispatchedAfterResponse($command, $callback)->count() === 0 &&
|
|
|
168 |
$this->dispatchedSync($command, $callback)->count() === 0,
|
|
|
169 |
"The unexpected [{$command}] job was dispatched."
|
|
|
170 |
);
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
* Assert that no jobs were dispatched.
|
|
|
175 |
*
|
|
|
176 |
* @return void
|
|
|
177 |
*/
|
|
|
178 |
public function assertNothingDispatched()
|
|
|
179 |
{
|
|
|
180 |
PHPUnit::assertEmpty($this->commands, 'Jobs were dispatched unexpectedly.');
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* Assert if a job was explicitly dispatched synchronously based on a truth-test callback.
|
|
|
185 |
*
|
|
|
186 |
* @param string|\Closure $command
|
|
|
187 |
* @param callable|int|null $callback
|
|
|
188 |
* @return void
|
|
|
189 |
*/
|
|
|
190 |
public function assertDispatchedSync($command, $callback = null)
|
|
|
191 |
{
|
|
|
192 |
if ($command instanceof Closure) {
|
|
|
193 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command];
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
if (is_numeric($callback)) {
|
|
|
197 |
return $this->assertDispatchedSyncTimes($command, $callback);
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
PHPUnit::assertTrue(
|
|
|
201 |
$this->dispatchedSync($command, $callback)->count() > 0,
|
|
|
202 |
"The expected [{$command}] job was not dispatched synchronously."
|
|
|
203 |
);
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
/**
|
|
|
207 |
* Assert if a job was pushed synchronously a number of times.
|
|
|
208 |
*
|
| 621 |
lars |
209 |
* @param string|\Closure $command
|
| 148 |
lars |
210 |
* @param int $times
|
|
|
211 |
* @return void
|
|
|
212 |
*/
|
|
|
213 |
public function assertDispatchedSyncTimes($command, $times = 1)
|
|
|
214 |
{
|
| 621 |
lars |
215 |
$callback = null;
|
| 148 |
lars |
216 |
|
| 621 |
lars |
217 |
if ($command instanceof Closure) {
|
|
|
218 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command];
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
$count = $this->dispatchedSync($command, $callback)->count();
|
|
|
222 |
|
| 148 |
lars |
223 |
PHPUnit::assertSame(
|
|
|
224 |
$times, $count,
|
|
|
225 |
"The expected [{$command}] job was synchronously pushed {$count} times instead of {$times} times."
|
|
|
226 |
);
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
/**
|
|
|
230 |
* Determine if a job was dispatched based on a truth-test callback.
|
|
|
231 |
*
|
|
|
232 |
* @param string|\Closure $command
|
|
|
233 |
* @param callable|null $callback
|
|
|
234 |
* @return void
|
|
|
235 |
*/
|
|
|
236 |
public function assertNotDispatchedSync($command, $callback = null)
|
|
|
237 |
{
|
|
|
238 |
if ($command instanceof Closure) {
|
|
|
239 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command];
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
PHPUnit::assertCount(
|
|
|
243 |
0, $this->dispatchedSync($command, $callback),
|
|
|
244 |
"The unexpected [{$command}] job was dispatched synchronously."
|
|
|
245 |
);
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
/**
|
|
|
249 |
* Assert if a job was dispatched after the response was sent based on a truth-test callback.
|
|
|
250 |
*
|
|
|
251 |
* @param string|\Closure $command
|
|
|
252 |
* @param callable|int|null $callback
|
|
|
253 |
* @return void
|
|
|
254 |
*/
|
|
|
255 |
public function assertDispatchedAfterResponse($command, $callback = null)
|
|
|
256 |
{
|
|
|
257 |
if ($command instanceof Closure) {
|
|
|
258 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command];
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
if (is_numeric($callback)) {
|
|
|
262 |
return $this->assertDispatchedAfterResponseTimes($command, $callback);
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
PHPUnit::assertTrue(
|
|
|
266 |
$this->dispatchedAfterResponse($command, $callback)->count() > 0,
|
|
|
267 |
"The expected [{$command}] job was not dispatched after sending the response."
|
|
|
268 |
);
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
/**
|
|
|
272 |
* Assert if a job was pushed after the response was sent a number of times.
|
|
|
273 |
*
|
| 621 |
lars |
274 |
* @param string|\Closure $command
|
| 148 |
lars |
275 |
* @param int $times
|
|
|
276 |
* @return void
|
|
|
277 |
*/
|
|
|
278 |
public function assertDispatchedAfterResponseTimes($command, $times = 1)
|
|
|
279 |
{
|
| 621 |
lars |
280 |
$callback = null;
|
| 148 |
lars |
281 |
|
| 621 |
lars |
282 |
if ($command instanceof Closure) {
|
|
|
283 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command];
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
$count = $this->dispatchedAfterResponse($command, $callback)->count();
|
|
|
287 |
|
| 148 |
lars |
288 |
PHPUnit::assertSame(
|
|
|
289 |
$times, $count,
|
|
|
290 |
"The expected [{$command}] job was pushed {$count} times instead of {$times} times."
|
|
|
291 |
);
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
/**
|
|
|
295 |
* Determine if a job was dispatched based on a truth-test callback.
|
|
|
296 |
*
|
|
|
297 |
* @param string|\Closure $command
|
|
|
298 |
* @param callable|null $callback
|
|
|
299 |
* @return void
|
|
|
300 |
*/
|
|
|
301 |
public function assertNotDispatchedAfterResponse($command, $callback = null)
|
|
|
302 |
{
|
|
|
303 |
if ($command instanceof Closure) {
|
|
|
304 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command];
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
PHPUnit::assertCount(
|
|
|
308 |
0, $this->dispatchedAfterResponse($command, $callback),
|
|
|
309 |
"The unexpected [{$command}] job was dispatched after sending the response."
|
|
|
310 |
);
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
/**
|
|
|
314 |
* Assert if a chain of jobs was dispatched.
|
|
|
315 |
*
|
|
|
316 |
* @param array $expectedChain
|
|
|
317 |
* @return void
|
|
|
318 |
*/
|
|
|
319 |
public function assertChained(array $expectedChain)
|
|
|
320 |
{
|
|
|
321 |
$command = $expectedChain[0];
|
|
|
322 |
|
|
|
323 |
$expectedChain = array_slice($expectedChain, 1);
|
|
|
324 |
|
|
|
325 |
$callback = null;
|
|
|
326 |
|
|
|
327 |
if ($command instanceof Closure) {
|
|
|
328 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command];
|
|
|
329 |
} elseif (! is_string($command)) {
|
|
|
330 |
$instance = $command;
|
|
|
331 |
|
|
|
332 |
$command = get_class($instance);
|
|
|
333 |
|
|
|
334 |
$callback = function ($job) use ($instance) {
|
|
|
335 |
return serialize($this->resetChainPropertiesToDefaults($job)) === serialize($instance);
|
|
|
336 |
};
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
PHPUnit::assertTrue(
|
|
|
340 |
$this->dispatched($command, $callback)->isNotEmpty(),
|
|
|
341 |
"The expected [{$command}] job was not dispatched."
|
|
|
342 |
);
|
|
|
343 |
|
|
|
344 |
PHPUnit::assertTrue(
|
|
|
345 |
collect($expectedChain)->isNotEmpty(),
|
|
|
346 |
'The expected chain can not be empty.'
|
|
|
347 |
);
|
|
|
348 |
|
|
|
349 |
$this->isChainOfObjects($expectedChain)
|
|
|
350 |
? $this->assertDispatchedWithChainOfObjects($command, $expectedChain, $callback)
|
|
|
351 |
: $this->assertDispatchedWithChainOfClasses($command, $expectedChain, $callback);
|
|
|
352 |
}
|
|
|
353 |
|
|
|
354 |
/**
|
|
|
355 |
* Reset the chain properties to their default values on the job.
|
|
|
356 |
*
|
|
|
357 |
* @param mixed $job
|
|
|
358 |
* @return mixed
|
|
|
359 |
*/
|
|
|
360 |
protected function resetChainPropertiesToDefaults($job)
|
|
|
361 |
{
|
|
|
362 |
return tap(clone $job, function ($job) {
|
|
|
363 |
$job->chainConnection = null;
|
|
|
364 |
$job->chainQueue = null;
|
|
|
365 |
$job->chainCatchCallbacks = null;
|
|
|
366 |
$job->chained = [];
|
|
|
367 |
});
|
|
|
368 |
}
|
|
|
369 |
|
|
|
370 |
/**
|
|
|
371 |
* Assert if a job was dispatched with an empty chain based on a truth-test callback.
|
|
|
372 |
*
|
|
|
373 |
* @param string|\Closure $command
|
|
|
374 |
* @param callable|null $callback
|
|
|
375 |
* @return void
|
|
|
376 |
*/
|
|
|
377 |
public function assertDispatchedWithoutChain($command, $callback = null)
|
|
|
378 |
{
|
|
|
379 |
if ($command instanceof Closure) {
|
|
|
380 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command];
|
|
|
381 |
}
|
|
|
382 |
|
|
|
383 |
PHPUnit::assertTrue(
|
|
|
384 |
$this->dispatched($command, $callback)->isNotEmpty(),
|
|
|
385 |
"The expected [{$command}] job was not dispatched."
|
|
|
386 |
);
|
|
|
387 |
|
|
|
388 |
$this->assertDispatchedWithChainOfClasses($command, [], $callback);
|
|
|
389 |
}
|
|
|
390 |
|
|
|
391 |
/**
|
|
|
392 |
* Assert if a job was dispatched with chained jobs based on a truth-test callback.
|
|
|
393 |
*
|
|
|
394 |
* @param string $command
|
|
|
395 |
* @param array $expectedChain
|
|
|
396 |
* @param callable|null $callback
|
|
|
397 |
* @return void
|
|
|
398 |
*/
|
|
|
399 |
protected function assertDispatchedWithChainOfObjects($command, $expectedChain, $callback)
|
|
|
400 |
{
|
|
|
401 |
$chain = collect($expectedChain)->map(fn ($job) => serialize($job))->all();
|
|
|
402 |
|
|
|
403 |
PHPUnit::assertTrue(
|
|
|
404 |
$this->dispatched($command, $callback)->filter(
|
|
|
405 |
fn ($job) => $job->chained == $chain
|
|
|
406 |
)->isNotEmpty(),
|
|
|
407 |
'The expected chain was not dispatched.'
|
|
|
408 |
);
|
|
|
409 |
}
|
|
|
410 |
|
|
|
411 |
/**
|
|
|
412 |
* Assert if a job was dispatched with chained jobs based on a truth-test callback.
|
|
|
413 |
*
|
|
|
414 |
* @param string $command
|
|
|
415 |
* @param array $expectedChain
|
|
|
416 |
* @param callable|null $callback
|
|
|
417 |
* @return void
|
|
|
418 |
*/
|
|
|
419 |
protected function assertDispatchedWithChainOfClasses($command, $expectedChain, $callback)
|
|
|
420 |
{
|
|
|
421 |
$matching = $this->dispatched($command, $callback)->map->chained->map(function ($chain) {
|
|
|
422 |
return collect($chain)->map(
|
|
|
423 |
fn ($job) => get_class(unserialize($job))
|
|
|
424 |
);
|
|
|
425 |
})->filter(
|
|
|
426 |
fn ($chain) => $chain->all() === $expectedChain
|
|
|
427 |
);
|
|
|
428 |
|
|
|
429 |
PHPUnit::assertTrue(
|
|
|
430 |
$matching->isNotEmpty(), 'The expected chain was not dispatched.'
|
|
|
431 |
);
|
|
|
432 |
}
|
|
|
433 |
|
|
|
434 |
/**
|
|
|
435 |
* Determine if the given chain is entirely composed of objects.
|
|
|
436 |
*
|
|
|
437 |
* @param array $chain
|
|
|
438 |
* @return bool
|
|
|
439 |
*/
|
|
|
440 |
protected function isChainOfObjects($chain)
|
|
|
441 |
{
|
|
|
442 |
return ! collect($chain)->contains(fn ($job) => ! is_object($job));
|
|
|
443 |
}
|
|
|
444 |
|
|
|
445 |
/**
|
|
|
446 |
* Assert if a batch was dispatched based on a truth-test callback.
|
|
|
447 |
*
|
|
|
448 |
* @param callable $callback
|
|
|
449 |
* @return void
|
|
|
450 |
*/
|
|
|
451 |
public function assertBatched(callable $callback)
|
|
|
452 |
{
|
|
|
453 |
PHPUnit::assertTrue(
|
|
|
454 |
$this->batched($callback)->count() > 0,
|
|
|
455 |
'The expected batch was not dispatched.'
|
|
|
456 |
);
|
|
|
457 |
}
|
|
|
458 |
|
|
|
459 |
/**
|
|
|
460 |
* Assert the number of batches that have been dispatched.
|
|
|
461 |
*
|
|
|
462 |
* @param int $count
|
|
|
463 |
* @return void
|
|
|
464 |
*/
|
|
|
465 |
public function assertBatchCount($count)
|
|
|
466 |
{
|
|
|
467 |
PHPUnit::assertCount(
|
|
|
468 |
$count, $this->batches,
|
|
|
469 |
);
|
|
|
470 |
}
|
|
|
471 |
|
|
|
472 |
/**
|
|
|
473 |
* Assert that no batched jobs were dispatched.
|
|
|
474 |
*
|
|
|
475 |
* @return void
|
|
|
476 |
*/
|
|
|
477 |
public function assertNothingBatched()
|
|
|
478 |
{
|
|
|
479 |
PHPUnit::assertEmpty($this->batches, 'Batched jobs were dispatched unexpectedly.');
|
|
|
480 |
}
|
|
|
481 |
|
|
|
482 |
/**
|
|
|
483 |
* Get all of the jobs matching a truth-test callback.
|
|
|
484 |
*
|
|
|
485 |
* @param string $command
|
|
|
486 |
* @param callable|null $callback
|
|
|
487 |
* @return \Illuminate\Support\Collection
|
|
|
488 |
*/
|
|
|
489 |
public function dispatched($command, $callback = null)
|
|
|
490 |
{
|
|
|
491 |
if (! $this->hasDispatched($command)) {
|
|
|
492 |
return collect();
|
|
|
493 |
}
|
|
|
494 |
|
|
|
495 |
$callback = $callback ?: fn () => true;
|
|
|
496 |
|
|
|
497 |
return collect($this->commands[$command])->filter(fn ($command) => $callback($command));
|
|
|
498 |
}
|
|
|
499 |
|
|
|
500 |
/**
|
|
|
501 |
* Get all of the jobs dispatched synchronously matching a truth-test callback.
|
|
|
502 |
*
|
|
|
503 |
* @param string $command
|
|
|
504 |
* @param callable|null $callback
|
|
|
505 |
* @return \Illuminate\Support\Collection
|
|
|
506 |
*/
|
|
|
507 |
public function dispatchedSync(string $command, $callback = null)
|
|
|
508 |
{
|
|
|
509 |
if (! $this->hasDispatchedSync($command)) {
|
|
|
510 |
return collect();
|
|
|
511 |
}
|
|
|
512 |
|
|
|
513 |
$callback = $callback ?: fn () => true;
|
|
|
514 |
|
|
|
515 |
return collect($this->commandsSync[$command])->filter(fn ($command) => $callback($command));
|
|
|
516 |
}
|
|
|
517 |
|
|
|
518 |
/**
|
|
|
519 |
* Get all of the jobs dispatched after the response was sent matching a truth-test callback.
|
|
|
520 |
*
|
|
|
521 |
* @param string $command
|
|
|
522 |
* @param callable|null $callback
|
|
|
523 |
* @return \Illuminate\Support\Collection
|
|
|
524 |
*/
|
|
|
525 |
public function dispatchedAfterResponse(string $command, $callback = null)
|
|
|
526 |
{
|
|
|
527 |
if (! $this->hasDispatchedAfterResponse($command)) {
|
|
|
528 |
return collect();
|
|
|
529 |
}
|
|
|
530 |
|
|
|
531 |
$callback = $callback ?: fn () => true;
|
|
|
532 |
|
|
|
533 |
return collect($this->commandsAfterResponse[$command])->filter(fn ($command) => $callback($command));
|
|
|
534 |
}
|
|
|
535 |
|
|
|
536 |
/**
|
|
|
537 |
* Get all of the pending batches matching a truth-test callback.
|
|
|
538 |
*
|
|
|
539 |
* @param callable $callback
|
|
|
540 |
* @return \Illuminate\Support\Collection
|
|
|
541 |
*/
|
|
|
542 |
public function batched(callable $callback)
|
|
|
543 |
{
|
|
|
544 |
if (empty($this->batches)) {
|
|
|
545 |
return collect();
|
|
|
546 |
}
|
|
|
547 |
|
|
|
548 |
return collect($this->batches)->filter(fn ($batch) => $callback($batch));
|
|
|
549 |
}
|
|
|
550 |
|
|
|
551 |
/**
|
|
|
552 |
* Determine if there are any stored commands for a given class.
|
|
|
553 |
*
|
|
|
554 |
* @param string $command
|
|
|
555 |
* @return bool
|
|
|
556 |
*/
|
|
|
557 |
public function hasDispatched($command)
|
|
|
558 |
{
|
|
|
559 |
return isset($this->commands[$command]) && ! empty($this->commands[$command]);
|
|
|
560 |
}
|
|
|
561 |
|
|
|
562 |
/**
|
|
|
563 |
* Determine if there are any stored commands for a given class.
|
|
|
564 |
*
|
|
|
565 |
* @param string $command
|
|
|
566 |
* @return bool
|
|
|
567 |
*/
|
|
|
568 |
public function hasDispatchedSync($command)
|
|
|
569 |
{
|
|
|
570 |
return isset($this->commandsSync[$command]) && ! empty($this->commandsSync[$command]);
|
|
|
571 |
}
|
|
|
572 |
|
|
|
573 |
/**
|
|
|
574 |
* Determine if there are any stored commands for a given class.
|
|
|
575 |
*
|
|
|
576 |
* @param string $command
|
|
|
577 |
* @return bool
|
|
|
578 |
*/
|
|
|
579 |
public function hasDispatchedAfterResponse($command)
|
|
|
580 |
{
|
|
|
581 |
return isset($this->commandsAfterResponse[$command]) && ! empty($this->commandsAfterResponse[$command]);
|
|
|
582 |
}
|
|
|
583 |
|
|
|
584 |
/**
|
|
|
585 |
* Dispatch a command to its appropriate handler.
|
|
|
586 |
*
|
|
|
587 |
* @param mixed $command
|
|
|
588 |
* @return mixed
|
|
|
589 |
*/
|
|
|
590 |
public function dispatch($command)
|
|
|
591 |
{
|
|
|
592 |
if ($this->shouldFakeJob($command)) {
|
|
|
593 |
$this->commands[get_class($command)][] = $command;
|
|
|
594 |
} else {
|
|
|
595 |
return $this->dispatcher->dispatch($command);
|
|
|
596 |
}
|
|
|
597 |
}
|
|
|
598 |
|
|
|
599 |
/**
|
|
|
600 |
* Dispatch a command to its appropriate handler in the current process.
|
|
|
601 |
*
|
|
|
602 |
* Queueable jobs will be dispatched to the "sync" queue.
|
|
|
603 |
*
|
|
|
604 |
* @param mixed $command
|
|
|
605 |
* @param mixed $handler
|
|
|
606 |
* @return mixed
|
|
|
607 |
*/
|
|
|
608 |
public function dispatchSync($command, $handler = null)
|
|
|
609 |
{
|
|
|
610 |
if ($this->shouldFakeJob($command)) {
|
|
|
611 |
$this->commandsSync[get_class($command)][] = $command;
|
|
|
612 |
} else {
|
|
|
613 |
return $this->dispatcher->dispatchSync($command, $handler);
|
|
|
614 |
}
|
|
|
615 |
}
|
|
|
616 |
|
|
|
617 |
/**
|
|
|
618 |
* Dispatch a command to its appropriate handler in the current process.
|
|
|
619 |
*
|
|
|
620 |
* @param mixed $command
|
|
|
621 |
* @param mixed $handler
|
|
|
622 |
* @return mixed
|
|
|
623 |
*/
|
|
|
624 |
public function dispatchNow($command, $handler = null)
|
|
|
625 |
{
|
|
|
626 |
if ($this->shouldFakeJob($command)) {
|
|
|
627 |
$this->commands[get_class($command)][] = $command;
|
|
|
628 |
} else {
|
|
|
629 |
return $this->dispatcher->dispatchNow($command, $handler);
|
|
|
630 |
}
|
|
|
631 |
}
|
|
|
632 |
|
|
|
633 |
/**
|
|
|
634 |
* Dispatch a command to its appropriate handler behind a queue.
|
|
|
635 |
*
|
|
|
636 |
* @param mixed $command
|
|
|
637 |
* @return mixed
|
|
|
638 |
*/
|
|
|
639 |
public function dispatchToQueue($command)
|
|
|
640 |
{
|
|
|
641 |
if ($this->shouldFakeJob($command)) {
|
|
|
642 |
$this->commands[get_class($command)][] = $command;
|
|
|
643 |
} else {
|
|
|
644 |
return $this->dispatcher->dispatchToQueue($command);
|
|
|
645 |
}
|
|
|
646 |
}
|
|
|
647 |
|
|
|
648 |
/**
|
|
|
649 |
* Dispatch a command to its appropriate handler.
|
|
|
650 |
*
|
|
|
651 |
* @param mixed $command
|
|
|
652 |
* @return mixed
|
|
|
653 |
*/
|
|
|
654 |
public function dispatchAfterResponse($command)
|
|
|
655 |
{
|
|
|
656 |
if ($this->shouldFakeJob($command)) {
|
|
|
657 |
$this->commandsAfterResponse[get_class($command)][] = $command;
|
|
|
658 |
} else {
|
|
|
659 |
return $this->dispatcher->dispatch($command);
|
|
|
660 |
}
|
|
|
661 |
}
|
|
|
662 |
|
|
|
663 |
/**
|
|
|
664 |
* Create a new chain of queueable jobs.
|
|
|
665 |
*
|
|
|
666 |
* @param \Illuminate\Support\Collection|array $jobs
|
|
|
667 |
* @return \Illuminate\Foundation\Bus\PendingChain
|
|
|
668 |
*/
|
|
|
669 |
public function chain($jobs)
|
|
|
670 |
{
|
|
|
671 |
$jobs = Collection::wrap($jobs);
|
|
|
672 |
|
|
|
673 |
return new PendingChainFake($this, $jobs->shift(), $jobs->toArray());
|
|
|
674 |
}
|
|
|
675 |
|
|
|
676 |
/**
|
|
|
677 |
* Attempt to find the batch with the given ID.
|
|
|
678 |
*
|
|
|
679 |
* @param string $batchId
|
|
|
680 |
* @return \Illuminate\Bus\Batch|null
|
|
|
681 |
*/
|
|
|
682 |
public function findBatch(string $batchId)
|
|
|
683 |
{
|
|
|
684 |
return $this->batchRepository->find($batchId);
|
|
|
685 |
}
|
|
|
686 |
|
|
|
687 |
/**
|
|
|
688 |
* Create a new batch of queueable jobs.
|
|
|
689 |
*
|
|
|
690 |
* @param \Illuminate\Support\Collection|array $jobs
|
|
|
691 |
* @return \Illuminate\Bus\PendingBatch
|
|
|
692 |
*/
|
|
|
693 |
public function batch($jobs)
|
|
|
694 |
{
|
|
|
695 |
return new PendingBatchFake($this, Collection::wrap($jobs));
|
|
|
696 |
}
|
|
|
697 |
|
|
|
698 |
/**
|
|
|
699 |
* Dispatch an empty job batch for testing.
|
|
|
700 |
*
|
|
|
701 |
* @param string $name
|
|
|
702 |
* @return \Illuminate\Bus\Batch
|
|
|
703 |
*/
|
|
|
704 |
public function dispatchFakeBatch($name = '')
|
|
|
705 |
{
|
|
|
706 |
return $this->batch([])->name($name)->dispatch();
|
|
|
707 |
}
|
|
|
708 |
|
|
|
709 |
/**
|
|
|
710 |
* Record the fake pending batch dispatch.
|
|
|
711 |
*
|
|
|
712 |
* @param \Illuminate\Bus\PendingBatch $pendingBatch
|
|
|
713 |
* @return \Illuminate\Bus\Batch
|
|
|
714 |
*/
|
|
|
715 |
public function recordPendingBatch(PendingBatch $pendingBatch)
|
|
|
716 |
{
|
|
|
717 |
$this->batches[] = $pendingBatch;
|
|
|
718 |
|
|
|
719 |
return $this->batchRepository->store($pendingBatch);
|
|
|
720 |
}
|
|
|
721 |
|
|
|
722 |
/**
|
|
|
723 |
* Determine if a command should be faked or actually dispatched.
|
|
|
724 |
*
|
|
|
725 |
* @param mixed $command
|
|
|
726 |
* @return bool
|
|
|
727 |
*/
|
|
|
728 |
protected function shouldFakeJob($command)
|
|
|
729 |
{
|
|
|
730 |
if ($this->shouldDispatchCommand($command)) {
|
|
|
731 |
return false;
|
|
|
732 |
}
|
|
|
733 |
|
|
|
734 |
if (empty($this->jobsToFake)) {
|
|
|
735 |
return true;
|
|
|
736 |
}
|
|
|
737 |
|
|
|
738 |
return collect($this->jobsToFake)
|
|
|
739 |
->filter(function ($job) use ($command) {
|
|
|
740 |
return $job instanceof Closure
|
|
|
741 |
? $job($command)
|
|
|
742 |
: $job === get_class($command);
|
|
|
743 |
})->isNotEmpty();
|
|
|
744 |
}
|
|
|
745 |
|
|
|
746 |
/**
|
|
|
747 |
* Determine if a command should be dispatched or not.
|
|
|
748 |
*
|
|
|
749 |
* @param mixed $command
|
|
|
750 |
* @return bool
|
|
|
751 |
*/
|
|
|
752 |
protected function shouldDispatchCommand($command)
|
|
|
753 |
{
|
|
|
754 |
return collect($this->jobsToDispatch)
|
|
|
755 |
->filter(function ($job) use ($command) {
|
|
|
756 |
return $job instanceof Closure
|
|
|
757 |
? $job($command)
|
|
|
758 |
: $job === get_class($command);
|
|
|
759 |
})->isNotEmpty();
|
|
|
760 |
}
|
|
|
761 |
|
|
|
762 |
/**
|
|
|
763 |
* Set the pipes commands should be piped through before dispatching.
|
|
|
764 |
*
|
|
|
765 |
* @param array $pipes
|
|
|
766 |
* @return $this
|
|
|
767 |
*/
|
|
|
768 |
public function pipeThrough(array $pipes)
|
|
|
769 |
{
|
|
|
770 |
$this->dispatcher->pipeThrough($pipes);
|
|
|
771 |
|
|
|
772 |
return $this;
|
|
|
773 |
}
|
|
|
774 |
|
|
|
775 |
/**
|
|
|
776 |
* Determine if the given command has a handler.
|
|
|
777 |
*
|
|
|
778 |
* @param mixed $command
|
|
|
779 |
* @return bool
|
|
|
780 |
*/
|
|
|
781 |
public function hasCommandHandler($command)
|
|
|
782 |
{
|
|
|
783 |
return $this->dispatcher->hasCommandHandler($command);
|
|
|
784 |
}
|
|
|
785 |
|
|
|
786 |
/**
|
|
|
787 |
* Retrieve the handler for a command.
|
|
|
788 |
*
|
|
|
789 |
* @param mixed $command
|
|
|
790 |
* @return mixed
|
|
|
791 |
*/
|
|
|
792 |
public function getCommandHandler($command)
|
|
|
793 |
{
|
|
|
794 |
return $this->dispatcher->getCommandHandler($command);
|
|
|
795 |
}
|
|
|
796 |
|
|
|
797 |
/**
|
|
|
798 |
* Map a command to a handler.
|
|
|
799 |
*
|
|
|
800 |
* @param array $map
|
|
|
801 |
* @return $this
|
|
|
802 |
*/
|
|
|
803 |
public function map(array $map)
|
|
|
804 |
{
|
|
|
805 |
$this->dispatcher->map($map);
|
|
|
806 |
|
|
|
807 |
return $this;
|
|
|
808 |
}
|
|
|
809 |
}
|