Skip to content

Commit b12d863

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 34471b4 commit b12d863

File tree

3 files changed

+108
-18
lines changed

3 files changed

+108
-18
lines changed

src/Illuminate/Support/Facades/Bus.php

Lines changed: 10 additions & 10 deletions
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()

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

Lines changed: 26 additions & 8 deletions
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,

tests/Support/SupportTestingBusFakeTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,30 @@ public function testAssertDispatchedAfterResponseWithCallbackFunction()
225225
});
226226
}
227227

228+
public function testAssertDispatchedAfterResponseTimesWithCallbackFunction()
229+
{
230+
$this->fake->dispatchAfterResponse(new OtherBusJobStub(0));
231+
$this->fake->dispatchAfterResponse(new OtherBusJobStub(1));
232+
$this->fake->dispatchAfterResponse(new OtherBusJobStub(1));
233+
234+
try {
235+
$this->fake->assertDispatchedAfterResponseTimes(function (OtherBusJobStub $job) {
236+
return $job->id === 0;
237+
}, 2);
238+
$this->fail();
239+
} catch (ExpectationFailedException $e) {
240+
$this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\OtherBusJobStub] job was pushed 1 times instead of 2 times.'));
241+
}
242+
243+
$this->fake->assertDispatchedAfterResponseTimes(function (OtherBusJobStub $job) {
244+
return $job->id === 0;
245+
});
246+
247+
$this->fake->assertDispatchedAfterResponseTimes(function (OtherBusJobStub $job) {
248+
return $job->id === 1;
249+
}, 2);
250+
}
251+
228252
public function testAssertDispatchedSyncWithCallbackFunction()
229253
{
230254
$this->fake->dispatchSync(new OtherBusJobStub);
@@ -263,6 +287,30 @@ public function testAssertDispatchedTimes()
263287
$this->fake->assertDispatchedTimes(BusJobStub::class, 2);
264288
}
265289

290+
public function testAssertDispatchedTimesWithCallbackFunction()
291+
{
292+
$this->fake->dispatch(new OtherBusJobStub(0));
293+
$this->fake->dispatchNow(new OtherBusJobStub(1));
294+
$this->fake->dispatchAfterResponse(new OtherBusJobStub(1));
295+
296+
try {
297+
$this->fake->assertDispatchedTimes(function (OtherBusJobStub $job) {
298+
return $job->id === 0;
299+
}, 2);
300+
$this->fail();
301+
} catch (ExpectationFailedException $e) {
302+
$this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\OtherBusJobStub] job was pushed 1 times instead of 2 times.'));
303+
}
304+
305+
$this->fake->assertDispatchedTimes(function (OtherBusJobStub $job) {
306+
return $job->id === 0;
307+
});
308+
309+
$this->fake->assertDispatchedTimes(function (OtherBusJobStub $job) {
310+
return $job->id === 1;
311+
}, 2);
312+
}
313+
266314
public function testAssertDispatchedAfterResponseTimes()
267315
{
268316
$this->fake->dispatchAfterResponse(new BusJobStub);
@@ -293,6 +341,30 @@ public function testAssertDispatchedSyncTimes()
293341
$this->fake->assertDispatchedSyncTimes(BusJobStub::class, 2);
294342
}
295343

344+
public function testAssertDispatchedSyncTimesWithCallbackFunction()
345+
{
346+
$this->fake->dispatchSync(new OtherBusJobStub(0));
347+
$this->fake->dispatchSync(new OtherBusJobStub(1));
348+
$this->fake->dispatchSync(new OtherBusJobStub(1));
349+
350+
try {
351+
$this->fake->assertDispatchedSyncTimes(function (OtherBusJobStub $job) {
352+
return $job->id === 0;
353+
}, 2);
354+
$this->fail();
355+
} catch (ExpectationFailedException $e) {
356+
$this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\OtherBusJobStub] job was synchronously pushed 1 times instead of 2 times.'));
357+
}
358+
359+
$this->fake->assertDispatchedSyncTimes(function (OtherBusJobStub $job) {
360+
return $job->id === 0;
361+
});
362+
363+
$this->fake->assertDispatchedSyncTimes(function (OtherBusJobStub $job) {
364+
return $job->id === 1;
365+
}, 2);
366+
}
367+
296368
public function testAssertNotDispatched()
297369
{
298370
$this->fake->assertNotDispatched(BusJobStub::class);

0 commit comments

Comments
 (0)