Skip to content

Commit d493d6f

Browse files
authored
[12.x] Add fakeFor and fakeExceptFor methods to Queue facade (#56149)
* feat: add fakeExcept and fakeFor methods to Queue facade * feat: add QueueFakeTest with tests for fakeFor and fakeExcept methods * feat: add SupportFacadesQueueTest with tests for Queue::fakeFor and Queue::fakeExcept methods * refactor: simplify mock usage in QueueFakeTest and SupportFacadesQueueTest * refactor: clean up spacing in SupportFacadesQueueTest * refactor: move defineEnvironment method to QueueFakeTest class
1 parent 4065ab7 commit d493d6f

File tree

3 files changed

+249
-0
lines changed

3 files changed

+249
-0
lines changed

src/Illuminate/Support/Facades/Queue.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,53 @@ public static function fake($jobsToFake = [])
9696
});
9797
}
9898

99+
/**
100+
* Replace the bound instance with a fake that fakes all jobs except the given jobs.
101+
*
102+
* @param string[]|string $jobsToAllow
103+
* @return \Illuminate\Support\Testing\Fakes\QueueFake
104+
*/
105+
public static function fakeExcept($jobsToAllow)
106+
{
107+
return static::fake()->except($jobsToAllow);
108+
}
109+
110+
/**
111+
* Replace the bound instance with a fake during the given callable's execution.
112+
*
113+
* @param callable $callable
114+
* @param array $jobsToFake
115+
* @return mixed
116+
*/
117+
public static function fakeFor(callable $callable, array $jobsToFake = [])
118+
{
119+
$originalQueueManager = static::getFacadeRoot();
120+
121+
static::fake($jobsToFake);
122+
123+
return tap($callable(), function () use ($originalQueueManager) {
124+
static::swap($originalQueueManager);
125+
});
126+
}
127+
128+
/**
129+
* Replace the bound instance with a fake during the given callable's execution.
130+
*
131+
* @param callable $callable
132+
* @param array $jobsToAllow
133+
* @return mixed
134+
*/
135+
public static function fakeExceptFor(callable $callable, array $jobsToAllow = [])
136+
{
137+
$originalQueueManager = static::getFacadeRoot();
138+
139+
static::fakeExcept($jobsToAllow);
140+
141+
return tap($callable(), function () use ($originalQueueManager) {
142+
static::swap($originalQueueManager);
143+
});
144+
}
145+
99146
/**
100147
* Get the registered name of the component.
101148
*
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Queue;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Support\Facades\Queue;
7+
use Illuminate\Support\Testing\Fakes\QueueFake;
8+
use Orchestra\Testbench\TestCase;
9+
10+
class QueueFakeTest extends TestCase
11+
{
12+
protected function defineEnvironment($app)
13+
{
14+
$app['config']->set('queue.default', 'sync');
15+
}
16+
17+
public function testFakeFor()
18+
{
19+
Queue::fakeFor(function () {
20+
Queue::push(new TestJob);
21+
Queue::assertPushed(TestJob::class);
22+
});
23+
}
24+
25+
public function testFakeExceptFor()
26+
{
27+
Queue::fakeExceptFor(function () {
28+
Queue::push(new TestJob);
29+
Queue::push(new OtherTestJob);
30+
31+
Queue::assertNotPushed(TestJob::class);
32+
Queue::assertPushed(OtherTestJob::class);
33+
}, [TestJob::class]);
34+
}
35+
36+
public function testFakeExcept()
37+
{
38+
$fake = Queue::fakeExcept([TestJob::class]);
39+
40+
$this->assertInstanceOf(QueueFake::class, $fake);
41+
}
42+
43+
public function testFakeForReturnValue()
44+
{
45+
$result = Queue::fakeFor(function () {
46+
return 'test-value';
47+
});
48+
49+
$this->assertEquals('test-value', $result);
50+
}
51+
52+
public function testFakeExceptForReturnValue()
53+
{
54+
$result = Queue::fakeExceptFor(function () {
55+
return 'test-value';
56+
}, []);
57+
58+
$this->assertEquals('test-value', $result);
59+
}
60+
}
61+
62+
class TestJob
63+
{
64+
use Queueable;
65+
66+
public function handle()
67+
{
68+
//
69+
}
70+
}
71+
72+
class OtherTestJob
73+
{
74+
use Queueable;
75+
76+
public function handle()
77+
{
78+
//
79+
}
80+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Support;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Container\Container;
7+
use Illuminate\Contracts\Queue\Factory as QueueContract;
8+
use Illuminate\Support\Facades\Facade;
9+
use Illuminate\Support\Facades\Queue;
10+
use Illuminate\Support\Testing\Fakes\QueueFake;
11+
use Mockery as m;
12+
use PHPUnit\Framework\TestCase;
13+
14+
class SupportFacadesQueueTest extends TestCase
15+
{
16+
private $queueManager;
17+
18+
protected function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
$this->queueManager = m::mock(Factory::class);
23+
24+
$container = new Container;
25+
$container->instance('queue', $this->queueManager);
26+
$container->alias('queue', QueueContract::class);
27+
28+
Facade::setFacadeApplication($container);
29+
}
30+
31+
protected function tearDown(): void
32+
{
33+
Queue::clearResolvedInstances();
34+
Queue::setFacadeApplication(null);
35+
36+
m::close();
37+
}
38+
39+
public function testFakeFor()
40+
{
41+
Queue::fakeFor(function () {
42+
(new QueueForStub)->pushJob();
43+
44+
Queue::assertPushed(QueueJobStub::class);
45+
});
46+
47+
$this->queueManager->shouldReceive('push')->once();
48+
49+
(new QueueForStub)->pushJob();
50+
}
51+
52+
public function testFakeForSwapsQueueManager()
53+
{
54+
Queue::fakeFor(function () {
55+
$this->assertInstanceOf(QueueFake::class, Queue::getFacadeRoot());
56+
});
57+
58+
$this->assertSame($this->queueManager, Queue::getFacadeRoot());
59+
}
60+
61+
public function testFakeExcept()
62+
{
63+
$fake = Queue::fakeExcept(QueueJobStub::class);
64+
65+
$this->assertInstanceOf(QueueFake::class, $fake);
66+
$this->assertSame($fake, Queue::getFacadeRoot());
67+
}
68+
69+
public function testFakeExceptFor()
70+
{
71+
Queue::fakeExceptFor(function () {
72+
$this->assertInstanceOf(QueueFake::class, Queue::getFacadeRoot());
73+
}, [QueueJobStub::class]);
74+
75+
$this->assertSame($this->queueManager, Queue::getFacadeRoot());
76+
}
77+
78+
public function testFakeExceptForSwapsQueueManager()
79+
{
80+
Queue::fakeExceptFor(function () {
81+
$this->assertInstanceOf(QueueFake::class, Queue::getFacadeRoot());
82+
}, []);
83+
84+
$this->assertSame($this->queueManager, Queue::getFacadeRoot());
85+
}
86+
87+
public function testFakeExceptForReturnValue()
88+
{
89+
$result = Queue::fakeExceptFor(function () {
90+
return 'test-result';
91+
});
92+
93+
$this->assertSame('test-result', $result);
94+
}
95+
96+
public function testFakeForReturnValue()
97+
{
98+
$result = Queue::fakeFor(function () {
99+
return 'test-result';
100+
});
101+
102+
$this->assertSame('test-result', $result);
103+
}
104+
}
105+
106+
class QueueJobStub
107+
{
108+
use Queueable;
109+
}
110+
111+
class OtherQueueJobStub
112+
{
113+
use Queueable;
114+
}
115+
116+
class QueueForStub
117+
{
118+
public function pushJob()
119+
{
120+
Queue::push(new QueueJobStub);
121+
}
122+
}

0 commit comments

Comments
 (0)