Skip to content

Commit

Permalink
support bus chain on fake
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Oct 12, 2020
1 parent 92db0b7 commit a952ac2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/BusFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,19 @@ public function dispatchAfterResponse($command)
}
}

/**
* Create a new chain of queueable jobs.
*
* @param \Illuminate\Support\Collection|array $jobs
* @return \Illuminate\Foundation\Bus\PendingChain
*/
public function chain($jobs)
{
$jobs = Collection::wrap($jobs);

return new PendingChainFake($this, $jobs->shift(), $jobs->toArray());
}

/**
* Attempt to find the batch with the given ID.
*
Expand Down
56 changes: 56 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/PendingChainFake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Illuminate\Support\Testing\Fakes;

use Closure;
use Illuminate\Foundation\Bus\PendingChain;
use Illuminate\Queue\CallQueuedClosure;

class PendingChainFake extends PendingChain
{
/**
* The fake bus instance.
*
* @var \Illuminate\Support\Testing\Fakes\BusFake
*/
protected $bus;

/**
* Create a new pending chain instance.
*
* @param \Illuminate\Support\Testing\Fakes\BusFake $bus
* @param mixed $job
* @param array $chain
* @return void
*/
public function __construct(BusFake $bus, $job, $chain)
{
$this->bus = $bus;
$this->job = $job;
$this->chain = $chain;
}

/**
* Dispatch the job with the given arguments.
*
* @return \Illuminate\Foundation\Bus\PendingDispatch
*/
public function dispatch()
{
if (is_string($this->job)) {
$firstJob = new $this->job(...func_get_args());
} elseif ($this->job instanceof Closure) {
$firstJob = CallQueuedClosure::create($this->job);
} else {
$firstJob = $this->job;
}

$firstJob->allOnConnection($this->connection);
$firstJob->allOnQueue($this->queue);
$firstJob->chain($this->chain);
$firstJob->delay($this->delay);
$firstJob->chainCatchCallbacks = $this->catchCallbacks();

return $this->bus->dispatch($firstJob);
}
}

0 comments on commit a952ac2

Please sign in to comment.