Skip to content

Commit a0ce238

Browse files
[10.x] Added assertClosurePushed and assertClosureNotPushed (#41713)
* Added `assertClosurePushed` and `assertClosureNotPushed`. * Styles updates. * Added docblocks to facade. * Update QueueFake.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 5babbe7 commit a0ce238

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/Illuminate/Support/Facades/Queue.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
* @method static mixed pushRaw(string $payload, string $queue = null, array $options = [])
1818
* @method static string getConnectionName()
1919
* @method static void assertNotPushed(string|\Closure $job, callable $callback = null)
20+
* @method static void assertClosureNotPushed(callable $callback = null)
2021
* @method static void assertNothingPushed()
2122
* @method static void assertPushed(string|\Closure $job, callable|int $callback = null)
23+
* @method static void assertClosurePushed(callable|int $callback = null)
2224
* @method static void assertPushedOn(string $queue, string|\Closure $job, callable $callback = null)
2325
* @method static void assertPushedWithChain(string $job, array $expectedChain = [], callable $callback = null)
2426
*

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BadMethodCallException;
66
use Closure;
77
use Illuminate\Contracts\Queue\Queue;
8+
use Illuminate\Queue\CallQueuedClosure;
89
use Illuminate\Queue\QueueManager;
910
use Illuminate\Support\Collection;
1011
use Illuminate\Support\Traits\ReflectsClosures;
@@ -201,6 +202,28 @@ protected function assertPushedWithChainOfClasses($job, $expectedChain, $callbac
201202
);
202203
}
203204

205+
/**
206+
* Assert if a closure was pushed based on a truth-test callback.
207+
*
208+
* @param callable|int|null $callback
209+
* @return void
210+
*/
211+
public function assertClosurePushed($callback = null)
212+
{
213+
$this->assertPushed(CallQueuedClosure::class, $callback);
214+
}
215+
216+
/**
217+
* Assert that a closure was not pushed based on a truth-test callback.
218+
*
219+
* @param callable|null $callback
220+
* @return void
221+
*/
222+
public function assertClosureNotPushed($callback = null)
223+
{
224+
$this->assertNotPushed(CallQueuedClosure::class, $callback);
225+
}
226+
204227
/**
205228
* Determine if the given chain is entirely composed of objects.
206229
*
@@ -311,6 +334,10 @@ public function size($queue = null)
311334
public function push($job, $data = '', $queue = null)
312335
{
313336
if ($this->shouldFakeJob($job)) {
337+
if ($job instanceof Closure) {
338+
$job = CallQueuedClosure::create($job);
339+
}
340+
314341
$this->jobs[is_object($job) ? get_class($job) : $job][] = [
315342
'job' => $job,
316343
'queue' => $queue,

tests/Support/SupportTestingQueueFakeTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,35 @@ public function testCallUndefinedMethodErrorHandling()
314314
)));
315315
}
316316
}
317+
318+
public function testAssertClosurePushed()
319+
{
320+
$this->fake->push(function () {
321+
// Do nothing
322+
});
323+
324+
$this->fake->assertClosurePushed();
325+
}
326+
327+
public function testAssertClosurePushedWithTimes()
328+
{
329+
$this->fake->push(function () {
330+
// Do nothing
331+
});
332+
333+
$this->fake->push(function () {
334+
// Do nothing
335+
});
336+
337+
$this->fake->assertClosurePushed(2);
338+
}
339+
340+
public function testAssertClosureNotPushed()
341+
{
342+
$this->fake->push($this->job);
343+
344+
$this->fake->assertClosureNotPushed();
345+
}
317346
}
318347

319348
class JobStub

0 commit comments

Comments
 (0)