Skip to content

Commit a952ac2

Browse files
committed
support bus chain on fake
1 parent 92db0b7 commit a952ac2

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

Diff for: src/Illuminate/Support/Testing/Fakes/BusFake.php

+13
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,19 @@ public function dispatchAfterResponse($command)
360360
}
361361
}
362362

363+
/**
364+
* Create a new chain of queueable jobs.
365+
*
366+
* @param \Illuminate\Support\Collection|array $jobs
367+
* @return \Illuminate\Foundation\Bus\PendingChain
368+
*/
369+
public function chain($jobs)
370+
{
371+
$jobs = Collection::wrap($jobs);
372+
373+
return new PendingChainFake($this, $jobs->shift(), $jobs->toArray());
374+
}
375+
363376
/**
364377
* Attempt to find the batch with the given ID.
365378
*
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Illuminate\Support\Testing\Fakes;
4+
5+
use Closure;
6+
use Illuminate\Foundation\Bus\PendingChain;
7+
use Illuminate\Queue\CallQueuedClosure;
8+
9+
class PendingChainFake extends PendingChain
10+
{
11+
/**
12+
* The fake bus instance.
13+
*
14+
* @var \Illuminate\Support\Testing\Fakes\BusFake
15+
*/
16+
protected $bus;
17+
18+
/**
19+
* Create a new pending chain instance.
20+
*
21+
* @param \Illuminate\Support\Testing\Fakes\BusFake $bus
22+
* @param mixed $job
23+
* @param array $chain
24+
* @return void
25+
*/
26+
public function __construct(BusFake $bus, $job, $chain)
27+
{
28+
$this->bus = $bus;
29+
$this->job = $job;
30+
$this->chain = $chain;
31+
}
32+
33+
/**
34+
* Dispatch the job with the given arguments.
35+
*
36+
* @return \Illuminate\Foundation\Bus\PendingDispatch
37+
*/
38+
public function dispatch()
39+
{
40+
if (is_string($this->job)) {
41+
$firstJob = new $this->job(...func_get_args());
42+
} elseif ($this->job instanceof Closure) {
43+
$firstJob = CallQueuedClosure::create($this->job);
44+
} else {
45+
$firstJob = $this->job;
46+
}
47+
48+
$firstJob->allOnConnection($this->connection);
49+
$firstJob->allOnQueue($this->queue);
50+
$firstJob->chain($this->chain);
51+
$firstJob->delay($this->delay);
52+
$firstJob->chainCatchCallbacks = $this->catchCallbacks();
53+
54+
return $this->bus->dispatch($firstJob);
55+
}
56+
}

0 commit comments

Comments
 (0)