Skip to content

Commit 63eafa3

Browse files
committed
first commit
0 parents  commit 63eafa3

File tree

8 files changed

+1337
-0
lines changed

8 files changed

+1337
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.{yml,yaml}]
15+
indent_size = 2

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/.phpunit.cache
2+
/vendor
3+
composer.phar
4+
composer.lock
5+
.DS_Store
6+
Thumbs.db
7+
/phpunit.xml
8+
/.idea
9+
/.fleet
10+
/.vscode
11+
.phpunit.result.cache

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) circle33 <newnash13@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
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.

composer.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "circle33/laravel-bus-fluentable",
3+
"description": "Make Laravel testing batched jobs more fluent.",
4+
"keywords" : [
5+
"laravel",
6+
"laravel-bus-fluentable"
7+
],
8+
"type": "library",
9+
"license": "MIT",
10+
"autoload": {
11+
"psr-4": {
12+
"Circle33\\LaravelBusFluentable\\": "src/"
13+
}
14+
},
15+
"authors": [
16+
{
17+
"name": "circle33",
18+
"email": "newnash13@gmail.com"
19+
}
20+
],
21+
"minimum-stability": "stable",
22+
"require-dev": {
23+
"laravel/framework": "^11.35",
24+
"phpunit/phpunit": "^11.5",
25+
"orchestra/testbench-core": "^9.8"
26+
}
27+
}

src/Bus.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Circle33\LaravelBusFluentable;
4+
5+
use Circle33\LaravelBusFluentable\FluentPendingBatch;
6+
7+
class Bus
8+
{
9+
public static function assertPendingBatched(callable $callback)
10+
{
11+
$dispatcher = app(\Illuminate\Contracts\Bus\Dispatcher::class);
12+
13+
return collect($dispatcher->dispatchedBatches())
14+
->filter(fn ($batch) => $callback(new FluentPendingBatch($dispatcher, $batch->jobs)));
15+
}
16+
}

0 commit comments

Comments
 (0)