Skip to content

Commit 9684a04

Browse files
Add partial queue faking (#41425)
* add partial queue faking * Apply fixes from StyleCI Co-authored-by: StyleCI Bot <[email protected]>
1 parent 001e4ec commit 9684a04

File tree

3 files changed

+96
-6
lines changed

3 files changed

+96
-6
lines changed

src/Illuminate/Support/Facades/Queue.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ public static function popUsing($workerName, $callback)
4242
/**
4343
* Replace the bound instance with a fake.
4444
*
45+
* @param array|string $jobsToFake
4546
* @return \Illuminate\Support\Testing\Fakes\QueueFake
4647
*/
47-
public static function fake()
48+
public static function fake($jobsToFake = [])
4849
{
49-
static::swap($fake = new QueueFake(static::getFacadeApplication()));
50+
static::swap($fake = new QueueFake(static::getFacadeApplication(), $jobsToFake, static::getFacadeRoot()));
5051

5152
return $fake;
5253
}

src/Illuminate/Support/Testing/Fakes/QueueFake.php

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,51 @@
66
use Closure;
77
use Illuminate\Contracts\Queue\Queue;
88
use Illuminate\Queue\QueueManager;
9+
use Illuminate\Support\Collection;
910
use Illuminate\Support\Traits\ReflectsClosures;
1011
use PHPUnit\Framework\Assert as PHPUnit;
1112

1213
class QueueFake extends QueueManager implements Queue
1314
{
1415
use ReflectsClosures;
1516

17+
/**
18+
* The original queue manager.
19+
*
20+
* @var \Illuminate\Contracts\Queue\Queue
21+
*/
22+
protected $queue;
23+
24+
/**
25+
* The job types that should be intercepted instead of pushed to the queue.
26+
*
27+
* @var array
28+
*/
29+
protected $jobsToFake;
30+
1631
/**
1732
* All of the jobs that have been pushed.
1833
*
1934
* @var array
2035
*/
2136
protected $jobs = [];
2237

38+
/**
39+
* Create a new fake queue instance.
40+
*
41+
* @param \Illuminate\Contracts\Foundation\Application $app
42+
* @param array $jobsToFake
43+
* @param \Illuminate\Queue\QueueManager|null $queue
44+
* @return void
45+
*/
46+
public function __construct($app, $jobsToFake = [], $queue = null)
47+
{
48+
parent::__construct($app);
49+
50+
$this->jobsToFake = Collection::wrap($jobsToFake);
51+
$this->queue = $queue;
52+
}
53+
2354
/**
2455
* Assert if a job was pushed based on a truth-test callback.
2556
*
@@ -279,10 +310,33 @@ public function size($queue = null)
279310
*/
280311
public function push($job, $data = '', $queue = null)
281312
{
282-
$this->jobs[is_object($job) ? get_class($job) : $job][] = [
283-
'job' => $job,
284-
'queue' => $queue,
285-
];
313+
if ($this->shouldFakeJob($job)) {
314+
$this->jobs[is_object($job) ? get_class($job) : $job][] = [
315+
'job' => $job,
316+
'queue' => $queue,
317+
];
318+
} else {
319+
is_object($job) && isset($job->connection)
320+
? $this->queue->connection($job->connection)->push($job, $data, $queue)
321+
: $this->queue->push($job, $data, $queue);
322+
}
323+
}
324+
325+
/**
326+
* Determine if a job should be faked or actually dispatched.
327+
*
328+
* @param object $job
329+
* @return bool
330+
*/
331+
public function shouldFakeJob($job)
332+
{
333+
if ($this->jobsToFake->isEmpty()) {
334+
return true;
335+
}
336+
337+
return $this->jobsToFake->contains(function ($jobToFake) use ($job) {
338+
return $job instanceof ((string) $jobToFake);
339+
});
286340
}
287341

288342
/**

tests/Support/SupportTestingQueueFakeTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use BadMethodCallException;
66
use Illuminate\Bus\Queueable;
77
use Illuminate\Foundation\Application;
8+
use Illuminate\Queue\QueueManager;
89
use Illuminate\Support\Testing\Fakes\QueueFake;
10+
use Mockery as m;
911
use PHPUnit\Framework\Constraint\ExceptionMessage;
1012
use PHPUnit\Framework\ExpectationFailedException;
1113
use PHPUnit\Framework\TestCase;
@@ -29,6 +31,13 @@ protected function setUp(): void
2931
$this->job = new JobStub;
3032
}
3133

34+
protected function tearDown(): void
35+
{
36+
parent::tearDown();
37+
38+
m::close();
39+
}
40+
3241
public function testAssertPushed()
3342
{
3443
try {
@@ -43,6 +52,24 @@ public function testAssertPushed()
4352
$this->fake->assertPushed(JobStub::class);
4453
}
4554

55+
public function testAssertPushedWithIgnore()
56+
{
57+
$job = new JobStub;
58+
59+
$manager = m::mock(QueueManager::class);
60+
$manager->shouldReceive('push')->once()->withArgs(function ($passedJob) use ($job) {
61+
return $passedJob === $job;
62+
});
63+
64+
$fake = new QueueFake(new Application, JobToFakeStub::class, $manager);
65+
66+
$fake->push($job);
67+
$fake->push(new JobToFakeStub());
68+
69+
$fake->assertNotPushed(JobStub::class);
70+
$fake->assertPushed(JobToFakeStub::class);
71+
}
72+
4673
public function testAssertPushedWithClosure()
4774
{
4875
$this->fake->push($this->job);
@@ -297,6 +324,14 @@ public function handle()
297324
}
298325
}
299326

327+
class JobToFakeStub
328+
{
329+
public function handle()
330+
{
331+
//
332+
}
333+
}
334+
300335
class JobWithChainStub
301336
{
302337
use Queueable;

0 commit comments

Comments
 (0)