Skip to content

Commit

Permalink
[9.x] Allow to define which jobs should be actually dispatched when u…
Browse files Browse the repository at this point in the history
…sing `Bus::fake` (#44106)

* Allow to define which jobs should be dispatched instead of faked

* add tests

* Implement fluent API
  • Loading branch information
mateusjunges authored Sep 13, 2022
1 parent 05bdefd commit 8d1bfc1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/Illuminate/Support/Testing/Fakes/BusFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ class BusFake implements QueueingDispatcher
*
* @var array
*/
protected $jobsToFake;
protected $jobsToFake = [];

/**
* The job types that should be dispatched instead of faked.
*
* @var array
*/
protected $jobsToDispatch = [];

/**
* The fake repository to track batched jobs.
Expand Down Expand Up @@ -77,6 +84,19 @@ public function __construct(QueueingDispatcher $dispatcher, $jobsToFake = [])
$this->batchRepository = new BatchRepositoryFake;
}

/**
* Specify the jobs that should be dispatched instead of faked.
*
* @param array|string $jobsToDispatch
* @return void
*/
public function except($jobsToDispatch)
{
$this->jobsToDispatch = array_merge($this->jobsToDispatch, Arr::wrap($jobsToDispatch));

return $this;
}

/**
* Assert if a job was dispatched based on a truth-test callback.
*
Expand Down Expand Up @@ -676,6 +696,10 @@ public function recordPendingBatch(PendingBatch $pendingBatch)
*/
protected function shouldFakeJob($command)
{
if ($this->shouldDispatchCommand($command)) {
return false;
}

if (empty($this->jobsToFake)) {
return true;
}
Expand All @@ -688,6 +712,22 @@ protected function shouldFakeJob($command)
})->isNotEmpty();
}

/**
* Determine if a command should be dispatched or not.
*
* @param mixed $command
* @return bool
*/
protected function shouldDispatchCommand($command)
{
return collect($this->jobsToDispatch)
->filter(function ($job) use ($command) {
return $job instanceof Closure
? $job($command)
: $job === get_class($command);
})->isNotEmpty();
}

/**
* Set the pipes commands should be piped through before dispatching.
*
Expand Down
37 changes: 37 additions & 0 deletions tests/Support/SupportTestingBusFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,38 @@ public function testAssertDispatchedWithIgnoreClass()
$fake->assertDispatchedTimes(OtherBusJobStub::class, 2);
}

public function testDispatchedFakingOnlyGivenJobs()
{
$dispatcher = m::mock(QueueingDispatcher::class);

$job = new BusJobStub;
$dispatcher->shouldReceive('dispatch')->never()->with($job);
$dispatcher->shouldReceive('dispatchNow')->never()->with($job, null);

$otherJob = new OtherBusJobStub;
$dispatcher->shouldReceive('dispatch')->once()->with($otherJob);
$dispatcher->shouldReceive('dispatchNow')->once()->with($otherJob, null);

$thirdJob = new ThirdJob;
$dispatcher->shouldReceive('dispatch')->never()->with($thirdJob);
$dispatcher->shouldReceive('dispatchNow')->never()->with($thirdJob, null);

$fake = (new BusFake($dispatcher))->except(OtherBusJobStub::class);

$fake->dispatch($job);
$fake->dispatchNow($job);

$fake->dispatch($otherJob);
$fake->dispatchNow($otherJob);

$fake->dispatch($thirdJob);
$fake->dispatchNow($thirdJob);

$fake->assertNotDispatched(OtherBusJobStub::class);
$fake->assertDispatchedTimes(BusJobStub::class, 2);
$fake->assertDispatchedTimes(ThirdJob::class, 2);
}

public function testAssertDispatchedWithIgnoreCallback()
{
$dispatcher = m::mock(QueueingDispatcher::class);
Expand Down Expand Up @@ -509,3 +541,8 @@ public function __construct($id = null)
$this->id = $id;
}
}

class ThirdJob
{
//
}

0 comments on commit 8d1bfc1

Please sign in to comment.