Skip to content

Commit dfc24d1

Browse files
[11.x] Fake Queue Interactions (#49766)
* prototype fake queue interactions * check delay * Apply fixes from StyleCI * fix job id --------- Co-authored-by: StyleCI Bot <[email protected]>
1 parent 4c71f54 commit dfc24d1

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

src/Illuminate/Queue/InteractsWithQueue.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
use DateTimeInterface;
66
use Illuminate\Contracts\Queue\Job as JobContract;
7+
use Illuminate\Queue\Jobs\FakeJob;
78
use Illuminate\Support\InteractsWithTime;
89
use InvalidArgumentException;
10+
use PHPUnit\Framework\Assert as PHPUnit;
11+
use RuntimeException;
912
use Throwable;
1013

1114
trait InteractsWithQueue
@@ -79,6 +82,94 @@ public function release($delay = 0)
7982
}
8083
}
8184

85+
/**
86+
* Indicate that queue interactions like fail, delete, and release should be faked.
87+
*
88+
* @return $this
89+
*/
90+
public function withFakeQueueInteractions()
91+
{
92+
$this->job = new FakeJob;
93+
94+
return $this;
95+
}
96+
97+
/**
98+
* Assert that the job was deleted from the queue.
99+
*
100+
* @return $this
101+
*/
102+
public function assertDeleted()
103+
{
104+
$this->ensureQueueInteractionsHaveBeenFaked();
105+
106+
PHPUnit::assertTrue(
107+
$this->job->isDeleted(),
108+
'Job was expected to be deleted, but was not.'
109+
);
110+
111+
return $this;
112+
}
113+
114+
/**
115+
* Assert that the job was manually failed.
116+
*
117+
* @return $this
118+
*/
119+
public function assertFailed()
120+
{
121+
$this->ensureQueueInteractionsHaveBeenFaked();
122+
123+
PHPUnit::assertTrue(
124+
$this->job->hasFailed(),
125+
'Job was expected to be manually failed, but was not.'
126+
);
127+
128+
return $this;
129+
}
130+
131+
/**
132+
* Assert that the job was released back onto the queue.
133+
*
134+
* @param \DateTimeInterface|\DateInterval|int $delay
135+
* @return $this
136+
*/
137+
public function assertReleased($delay = null)
138+
{
139+
$this->ensureQueueInteractionsHaveBeenFaked();
140+
141+
$delay = $delay instanceof DateTimeInterface
142+
? $this->secondsUntil($delay)
143+
: $delay;
144+
145+
PHPUnit::assertTrue(
146+
$this->job->isReleased(),
147+
'Job was expected to be released, but was not.'
148+
);
149+
150+
if (! is_null($delay)) {
151+
PHPUnit::assertSame(
152+
$delay,
153+
$this->job->releaseDelay,
154+
"Expected job to be released with delay of [{$delay}] seconds, but was released with delay of [{$this->job->releaseDelay}] seconds."
155+
);
156+
}
157+
158+
return $this;
159+
}
160+
161+
/**
162+
* Ensure that queue interactions have been faked.
163+
*
164+
* @return void
165+
*/
166+
private function ensureQueueInteractionsHaveBeenFaked()
167+
{
168+
if (! $this->job instanceof FakeJob) {
169+
throw new RuntimeException('Queue interactions have not been faked.');
170+
}
171+
}
172+
82173
/**
83174
* Set the base queue job instance.
84175
*
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Illuminate\Queue\Jobs;
4+
5+
use Illuminate\Support\Str;
6+
7+
class FakeJob extends Job
8+
{
9+
/**
10+
* The number of seconds the released job was delayed.
11+
*
12+
* @var int
13+
*/
14+
public $releaseDelay;
15+
16+
/**
17+
* The exception the job failed with.
18+
*
19+
* @var \Throwable
20+
*/
21+
public $failedWith;
22+
23+
/**
24+
* Get the job identifier.
25+
*
26+
* @return string
27+
*/
28+
public function getJobId()
29+
{
30+
return once(fn () => (string) Str::uuid());
31+
}
32+
33+
/**
34+
* Get the raw body of the job.
35+
*
36+
* @return string
37+
*/
38+
public function getRawBody()
39+
{
40+
return '';
41+
}
42+
43+
/**
44+
* Release the job back into the queue after (n) seconds.
45+
*
46+
* @param \DateTimeInterface|\DateInterval|int $delay
47+
* @return void
48+
*/
49+
public function release($delay = 0)
50+
{
51+
$this->released = true;
52+
$this->releaseDelay = $delay;
53+
}
54+
55+
/**
56+
* Delete the job from the queue.
57+
*
58+
* @return void
59+
*/
60+
public function delete()
61+
{
62+
$this->deleted = true;
63+
}
64+
65+
/**
66+
* Delete the job, call the "failed" method, and raise the failed job event.
67+
*
68+
* @param \Throwable|null $e
69+
* @return void
70+
*/
71+
public function fail($exception = null)
72+
{
73+
$this->failed = true;
74+
$this->failedWith = $exception;
75+
}
76+
}

0 commit comments

Comments
 (0)