Skip to content

Commit 0b4d441

Browse files
Accept closure in bus assertion helpers (#46075)
* Accept a closure as the first argument for `assertDispatchedTimes()` helper * Accept a closure as the first argument for `assertDispatchedSyncTimes()` helper * Accept a closure as the first argument for `assertDispatchedAfterResponseTimes()` helper * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 3954928 commit 0b4d441

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

Facades/Bus.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@
2121
* @method static \Illuminate\Bus\Dispatcher pipeThrough(array $pipes)
2222
* @method static \Illuminate\Bus\Dispatcher map(array $map)
2323
* @method static void except(array|string $jobsToDispatch)
24-
* @method static void assertDispatched(string|\Closure $command, callable|int|null $callback = null)
25-
* @method static void assertDispatchedTimes(string $command, int $times = 1)
26-
* @method static void assertNotDispatched(string|\Closure $command, callable|null $callback = null)
24+
* @method static void assertDispatched(\Closure|string $command, callable|int|null $callback = null)
25+
* @method static void assertDispatchedTimes(\Closure|string $command, int $times = 1)
26+
* @method static void assertNotDispatched(\Closure|string $command, callable|null $callback = null)
2727
* @method static void assertNothingDispatched()
28-
* @method static void assertDispatchedSync(string|\Closure $command, callable|int|null $callback = null)
29-
* @method static void assertDispatchedSyncTimes(string $command, int $times = 1)
30-
* @method static void assertNotDispatchedSync(string|\Closure $command, callable|null $callback = null)
31-
* @method static void assertDispatchedAfterResponse(string|\Closure $command, callable|int|null $callback = null)
32-
* @method static void assertDispatchedAfterResponseTimes(string $command, int $times = 1)
33-
* @method static void assertNotDispatchedAfterResponse(string|\Closure $command, callable|null $callback = null)
28+
* @method static void assertDispatchedSync(\Closure|string $command, callable|int|null $callback = null)
29+
* @method static void assertDispatchedSyncTimes(\Closure|string $command, int $times = 1)
30+
* @method static void assertNotDispatchedSync(\Closure|string $command, callable|null $callback = null)
31+
* @method static void assertDispatchedAfterResponse(\Closure|string $command, callable|int|null $callback = null)
32+
* @method static void assertDispatchedAfterResponseTimes(\Closure|string $command, int $times = 1)
33+
* @method static void assertNotDispatchedAfterResponse(\Closure|string $command, callable|null $callback = null)
3434
* @method static void assertChained(array $expectedChain)
35-
* @method static void assertDispatchedWithoutChain(string|\Closure $command, callable|null $callback = null)
35+
* @method static void assertDispatchedWithoutChain(\Closure|string $command, callable|null $callback = null)
3636
* @method static void assertBatched(callable $callback)
3737
* @method static void assertBatchCount(int $count)
3838
* @method static void assertNothingBatched()

Testing/Fakes/BusFake.php

+26-8
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,21 @@ public function assertDispatched($command, $callback = null)
127127
/**
128128
* Assert if a job was pushed a number of times.
129129
*
130-
* @param string $command
130+
* @param string|\Closure $command
131131
* @param int $times
132132
* @return void
133133
*/
134134
public function assertDispatchedTimes($command, $times = 1)
135135
{
136-
$count = $this->dispatched($command)->count() +
137-
$this->dispatchedAfterResponse($command)->count() +
138-
$this->dispatchedSync($command)->count();
136+
$callback = null;
137+
138+
if ($command instanceof Closure) {
139+
[$command, $callback] = [$this->firstClosureParameterType($command), $command];
140+
}
141+
142+
$count = $this->dispatched($command, $callback)->count() +
143+
$this->dispatchedAfterResponse($command, $callback)->count() +
144+
$this->dispatchedSync($command, $callback)->count();
139145

140146
PHPUnit::assertSame(
141147
$times, $count,
@@ -200,13 +206,19 @@ public function assertDispatchedSync($command, $callback = null)
200206
/**
201207
* Assert if a job was pushed synchronously a number of times.
202208
*
203-
* @param string $command
209+
* @param string|\Closure $command
204210
* @param int $times
205211
* @return void
206212
*/
207213
public function assertDispatchedSyncTimes($command, $times = 1)
208214
{
209-
$count = $this->dispatchedSync($command)->count();
215+
$callback = null;
216+
217+
if ($command instanceof Closure) {
218+
[$command, $callback] = [$this->firstClosureParameterType($command), $command];
219+
}
220+
221+
$count = $this->dispatchedSync($command, $callback)->count();
210222

211223
PHPUnit::assertSame(
212224
$times, $count,
@@ -259,13 +271,19 @@ public function assertDispatchedAfterResponse($command, $callback = null)
259271
/**
260272
* Assert if a job was pushed after the response was sent a number of times.
261273
*
262-
* @param string $command
274+
* @param string|\Closure $command
263275
* @param int $times
264276
* @return void
265277
*/
266278
public function assertDispatchedAfterResponseTimes($command, $times = 1)
267279
{
268-
$count = $this->dispatchedAfterResponse($command)->count();
280+
$callback = null;
281+
282+
if ($command instanceof Closure) {
283+
[$command, $callback] = [$this->firstClosureParameterType($command), $command];
284+
}
285+
286+
$count = $this->dispatchedAfterResponse($command, $callback)->count();
269287

270288
PHPUnit::assertSame(
271289
$times, $count,

0 commit comments

Comments
 (0)