|
| 1 | +When using Laravel, the current approach to testing batched jobs, as shown below, is somewhat unconventional: |
| 2 | + |
| 3 | +``` |
| 4 | +Bus::assertBatched(fn (PendingBatchFake $batchedCollection) => |
| 5 | + $batchedCollection->jobs->count() === 1 && $batchedCollection->jobs->first()->value === 'hello'; |
| 6 | +); |
| 7 | +``` |
| 8 | + |
| 9 | +This package introduces some helper functions to enhance the testability of batched jobs. Inspired by [fluent JSON testing](https://laravel.com/docs/11.x/http-tests#fluent-json-testing), these methods provide a more streamlined and readable approach to testing job batches within the Laravel application. The goal is to improve developer experience by offering clear, concise methods with illustrative examples. |
| 10 | + |
| 11 | +## Documentation |
| 12 | + |
| 13 | +### `has` |
| 14 | + |
| 15 | +Assert that the batch contains a job of the given type. You can also pass an integer to assert that the batch contains the exact number of jobs. |
| 16 | + |
| 17 | +**Example:** |
| 18 | + |
| 19 | +```php |
| 20 | +use Circle33\LaravelBusFluentable\Bus as BusFacade; |
| 21 | + |
| 22 | +Bus::fake(); |
| 23 | + |
| 24 | +Bus::batch([ |
| 25 | + new AJob(1, 2), |
| 26 | + new BJob, |
| 27 | +])->dispatch(); |
| 28 | + |
| 29 | +BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) => |
| 30 | + $batch->has(2) |
| 31 | + ->has(AJob::class, [1, 2]) |
| 32 | +); |
| 33 | +``` |
| 34 | + |
| 35 | +### `missing` |
| 36 | + |
| 37 | +Assert that the batch does not contain a job of the given type. |
| 38 | + |
| 39 | +**Example:** |
| 40 | + |
| 41 | +```php |
| 42 | +use Circle33\LaravelBusFluentable\Bus as BusFacade; |
| 43 | + |
| 44 | +Bus::fake(); |
| 45 | + |
| 46 | +Bus::batch([ |
| 47 | + new BJob, |
| 48 | +])->dispatch(); |
| 49 | + |
| 50 | +BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) => |
| 51 | + $batch->missing(AJob::class) |
| 52 | +); |
| 53 | +``` |
| 54 | + |
| 55 | +### `hasAll` |
| 56 | + |
| 57 | +Assert that the batch contains all of the given jobs. |
| 58 | + |
| 59 | +**Example:** |
| 60 | + |
| 61 | +```php |
| 62 | +use Circle33\LaravelBusFluentable\Bus as BusFacade; |
| 63 | + |
| 64 | +Bus::fake(); |
| 65 | + |
| 66 | +Bus::batch([ |
| 67 | + new AJob, |
| 68 | + new BJob, |
| 69 | +])->dispatch(); |
| 70 | + |
| 71 | +BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) => |
| 72 | + $batch->hasAll([AJob::class, BJob::class]) |
| 73 | +); |
| 74 | +``` |
| 75 | + |
| 76 | +### `missingAll` |
| 77 | + |
| 78 | +Assert that the batch does not contain any of the given jobs. |
| 79 | + |
| 80 | +**Example:** |
| 81 | + |
| 82 | +```php |
| 83 | +use Circle33\LaravelBusFluentable\Bus as BusFacade; |
| 84 | + |
| 85 | +Bus::fake(); |
| 86 | + |
| 87 | +Bus::batch([ |
| 88 | + new AJob, |
| 89 | + new BJob, |
| 90 | +])->dispatch(); |
| 91 | + |
| 92 | +BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) => |
| 93 | + $batch->missingAll([CJob::class, DJob::class]) |
| 94 | +); |
| 95 | +``` |
| 96 | + |
| 97 | +### `hasAny` |
| 98 | + |
| 99 | +Assert that the batch contains any of the given jobs. |
| 100 | + |
| 101 | +**Example:** |
| 102 | + |
| 103 | +```php |
| 104 | +use Circle33\LaravelBusFluentable\Bus as BusFacade; |
| 105 | + |
| 106 | +Bus::fake(); |
| 107 | + |
| 108 | +Bus::batch([ |
| 109 | + new AJob(1, 2), |
| 110 | + new BJob, |
| 111 | +])->dispatch(); |
| 112 | + |
| 113 | +BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) => |
| 114 | + $batch->hasAny(AJob::class, CJob::class) |
| 115 | +); |
| 116 | +``` |
| 117 | + |
| 118 | +### `first` |
| 119 | + |
| 120 | +Assert that the first job in the batch matches the given callback. |
| 121 | + |
| 122 | +**Example:** |
| 123 | + |
| 124 | +```php |
| 125 | +use Circle33\LaravelBusFluentable\Bus as BusFacade; |
| 126 | + |
| 127 | +Bus::fake(); |
| 128 | + |
| 129 | +Bus::batch([ |
| 130 | + [ |
| 131 | + new AJob(1, 2), |
| 132 | + new BJob, |
| 133 | + ], |
| 134 | + new CJob, |
| 135 | +])->dispatch(); |
| 136 | + |
| 137 | +BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) => |
| 138 | + $batch->first(fn (PendingBatchFake $firstBatch) => |
| 139 | + $firstBatch->has(AJob::class, [1, 2]) |
| 140 | + ->has(BJob::class) |
| 141 | + ) |
| 142 | +); |
| 143 | +``` |
| 144 | + |
| 145 | +### `nth` |
| 146 | + |
| 147 | +Assert that the nth job in the batch matches the given callback or type and parameters. |
| 148 | + |
| 149 | +**Example:** |
| 150 | + |
| 151 | +```php |
| 152 | +use Circle33\LaravelBusFluentable\Bus as BusFacade; |
| 153 | + |
| 154 | +Bus::fake(); |
| 155 | + |
| 156 | +Bus::batch([ |
| 157 | + [ |
| 158 | + new AJob(1, 2), |
| 159 | + new BJob |
| 160 | + ], |
| 161 | + new CJob::class(1) |
| 162 | +])->dispatch(); |
| 163 | + |
| 164 | +BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) => |
| 165 | + $batch->nth(0, fn (PendingBatchFake $batch) => |
| 166 | + $batch->has(AJob::class, [1, 2]) |
| 167 | + ->has(BJob::class) |
| 168 | + )->nth(1, CJob::class, [1]) |
| 169 | +); |
| 170 | +``` |
| 171 | + |
| 172 | +### `equal` |
| 173 | + |
| 174 | +Assert that the batch contains exactly the given jobs with the specified parameters. |
| 175 | + |
| 176 | +**Example:** |
| 177 | + |
| 178 | +```php |
| 179 | +use Circle33\LaravelBusFluentable\Bus as BusFacade; |
| 180 | + |
| 181 | +Bus::fake(); |
| 182 | + |
| 183 | +Bus::batch([ |
| 184 | + [ |
| 185 | + new AJob(1, 2), |
| 186 | + new BJob |
| 187 | + ], |
| 188 | + new CJob::class(1) |
| 189 | +])->dispatch(); |
| 190 | + |
| 191 | +BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) => |
| 192 | + $batch->equal([ |
| 193 | + [ |
| 194 | + AJob::class => [1, 2], |
| 195 | + BJob::class |
| 196 | + ], |
| 197 | + CJob::class => [1] |
| 198 | + ]) |
| 199 | +); |
| 200 | +``` |
| 201 | + |
| 202 | +### `etc` |
| 203 | + |
| 204 | +Assert that the batch has unexpected jobs beyond those checked. |
| 205 | + |
| 206 | +**Example:** |
| 207 | + |
| 208 | +```php |
| 209 | +use Circle33\LaravelBusFluentable\Bus as BusFacade; |
| 210 | + |
| 211 | +Bus::fake(); |
| 212 | + |
| 213 | +Bus::batch([ |
| 214 | + new AJob(1, 2), |
| 215 | + new BJob, |
| 216 | + new CJob::class(1) |
| 217 | +])->dispatch(); |
| 218 | + |
| 219 | +BusFacade::assertPendingBatched(fn (PendingBatchFake $batch) => |
| 220 | + $batch->has(AJob::class, [1, 2]) |
| 221 | + ->has(BJob::class) |
| 222 | + ->etc() |
| 223 | +); |
| 224 | +``` |
| 225 | + |
| 226 | +# Contributing |
| 227 | + |
| 228 | +Thank you for considering contributing to this project! We welcome and appreciate your help. |
| 229 | + |
| 230 | +## How to Contribute |
| 231 | + |
| 232 | +### Fork the Repository |
| 233 | + |
| 234 | +1. Fork the repository to your GitHub account. |
| 235 | +2. Clone your forked repository to your local machine: |
| 236 | + ```sh |
| 237 | + git clone https://github.com/your-username/laravel-bus-fluentable.git |
| 238 | + |
| 239 | +## License |
| 240 | + |
| 241 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments