Skip to content

Commit 8e19549

Browse files
committed
perf: adjust assertPendingBatched
1 parent 8f018cb commit 8e19549

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

README.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Assert that the batch contains a job of the given type. You can also pass an int
1818

1919
```php
2020
use Circle33\LaravelBusFluentable\Bus as BusFacade;
21+
use Circle33\LaravelBusFluentable\FluentPendingBatch;
2122

2223
Bus::fake();
2324

@@ -26,7 +27,7 @@ Bus::batch([
2627
new BJob,
2728
])->dispatch();
2829

29-
BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) =>
30+
BusFacade::assertPendingBatched(fn (FluentPendingBatch $batch) =>
3031
$batch->has(2)
3132
->has(AJob::class, [1, 2])
3233
);
@@ -40,14 +41,15 @@ Assert that the batch does not contain a job of the given type.
4041

4142
```php
4243
use Circle33\LaravelBusFluentable\Bus as BusFacade;
44+
use Circle33\LaravelBusFluentable\FluentPendingBatch;
4345

4446
Bus::fake();
4547

4648
Bus::batch([
4749
new BJob,
4850
])->dispatch();
4951

50-
BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) =>
52+
BusFacade::assertPendingBatched(fn (FluentPendingBatch $batch) =>
5153
$batch->missing(AJob::class)
5254
);
5355
```
@@ -60,6 +62,7 @@ Assert that the batch contains all of the given jobs.
6062

6163
```php
6264
use Circle33\LaravelBusFluentable\Bus as BusFacade;
65+
use Circle33\LaravelBusFluentable\FluentPendingBatch;
6366

6467
Bus::fake();
6568

@@ -68,7 +71,7 @@ Bus::batch([
6871
new BJob,
6972
])->dispatch();
7073

71-
BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) =>
74+
BusFacade::assertPendingBatched(fn (FluentPendingBatch $batch) =>
7275
$batch->hasAll([AJob::class, BJob::class])
7376
);
7477
```
@@ -81,6 +84,7 @@ Assert that the batch does not contain any of the given jobs.
8184

8285
```php
8386
use Circle33\LaravelBusFluentable\Bus as BusFacade;
87+
use Circle33\LaravelBusFluentable\FluentPendingBatch;
8488

8589
Bus::fake();
8690

@@ -89,7 +93,7 @@ Bus::batch([
8993
new BJob,
9094
])->dispatch();
9195

92-
BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) =>
96+
BusFacade::assertPendingBatched(fn (FluentPendingBatch $batch) =>
9397
$batch->missingAll([CJob::class, DJob::class])
9498
);
9599
```
@@ -102,6 +106,7 @@ Assert that the batch contains any of the given jobs.
102106

103107
```php
104108
use Circle33\LaravelBusFluentable\Bus as BusFacade;
109+
use Circle33\LaravelBusFluentable\FluentPendingBatch;
105110

106111
Bus::fake();
107112

@@ -110,7 +115,7 @@ Bus::batch([
110115
new BJob,
111116
])->dispatch();
112117

113-
BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) =>
118+
BusFacade::assertPendingBatched(fn (FluentPendingBatch $batch) =>
114119
$batch->hasAny(AJob::class, CJob::class)
115120
);
116121
```
@@ -123,6 +128,7 @@ Assert that the first job in the batch matches the given callback.
123128

124129
```php
125130
use Circle33\LaravelBusFluentable\Bus as BusFacade;
131+
use Circle33\LaravelBusFluentable\FluentPendingBatch;
126132

127133
Bus::fake();
128134

@@ -134,7 +140,7 @@ Bus::batch([
134140
new CJob,
135141
])->dispatch();
136142

137-
BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) =>
143+
BusFacade::assertPendingBatched(fn (FluentPendingBatch $batch) =>
138144
$batch->first(fn (PendingBatchFake $firstBatch) =>
139145
$firstBatch->has(AJob::class, [1, 2])
140146
->has(BJob::class)
@@ -150,6 +156,7 @@ Assert that the nth job in the batch matches the given callback or type and para
150156

151157
```php
152158
use Circle33\LaravelBusFluentable\Bus as BusFacade;
159+
use Circle33\LaravelBusFluentable\FluentPendingBatch;
153160

154161
Bus::fake();
155162

@@ -161,8 +168,8 @@ Bus::batch([
161168
new CJob::class(1)
162169
])->dispatch();
163170

164-
BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) =>
165-
$batch->nth(0, fn (PendingBatchFake $batch) =>
171+
BusFacade::assertPendingBatched(fn (FluentPendingBatch $batch) =>
172+
$batch->nth(0, fn (FluentPendingBatch $batch) =>
166173
$batch->has(AJob::class, [1, 2])
167174
->has(BJob::class)
168175
)->nth(1, CJob::class, [1])
@@ -177,6 +184,7 @@ Assert that the batch contains exactly the given jobs with the specified paramet
177184

178185
```php
179186
use Circle33\LaravelBusFluentable\Bus as BusFacade;
187+
use Circle33\LaravelBusFluentable\FluentPendingBatch;
180188

181189
Bus::fake();
182190

@@ -188,7 +196,7 @@ Bus::batch([
188196
new CJob::class(1)
189197
])->dispatch();
190198

191-
BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) =>
199+
BusFacade::assertPendingBatched(fn (FluentPendingBatch $batch) =>
192200
$batch->equal([
193201
[
194202
AJob::class => [1, 2],
@@ -207,6 +215,7 @@ Assert that the batch has unexpected jobs beyond those checked.
207215

208216
```php
209217
use Circle33\LaravelBusFluentable\Bus as BusFacade;
218+
use Circle33\LaravelBusFluentable\FluentPendingBatch;
210219

211220
Bus::fake();
212221

@@ -216,7 +225,7 @@ Bus::batch([
216225
new CJob::class(1)
217226
])->dispatch();
218227

219-
BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) =>
228+
BusFacade::assertPendingBatched(fn (FluentPendingBatch $batch) =>
220229
$batch->has(AJob::class, [1, 2])
221230
->has(BJob::class)
222231
->etc()

src/Bus.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,28 @@
33
namespace Circle33\LaravelBusFluentable;
44

55
use Circle33\LaravelBusFluentable\FluentPendingBatch;
6+
use Illuminate\Support\Testing\Fakes\BusFake;
7+
use Illuminate\Contracts\Bus\Dispatcher;
8+
use PHPUnit\Framework\Assert as PHPUnit;
69

710
class Bus
811
{
12+
/**
13+
* Assert that a batch was dispatched based on a callback.
14+
*
15+
* @param callable $callback
16+
* @return void
17+
*/
918
public static function assertPendingBatched(callable $callback)
1019
{
11-
$dispatcher = app(\Illuminate\Contracts\Bus\Dispatcher::class);
20+
/** @var BusFake $busFake */
21+
$busFake = app(Dispatcher::class);
1222

13-
return collect($dispatcher->dispatchedBatches())
14-
->filter(fn ($batch) => $callback(new FluentPendingBatch($dispatcher, $batch->jobs)));
23+
PHPUnit::assertTrue(
24+
collect($busFake->dispatchedBatches())
25+
->filter(fn ($batch) => $callback(new FluentPendingBatch($busFake, $batch->jobs)))
26+
->isNotEmpty(),
27+
"The expected batch was not dispatched."
28+
);
1529
}
1630
}

0 commit comments

Comments
 (0)