Skip to content

Commit 8bb98e3

Browse files
dispatchIf() and dispatchUnless() for batches (#49639)
1 parent e753742 commit 8bb98e3

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

src/Illuminate/Bus/PendingBatch.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,26 @@ protected function dispatchExistingBatch($batch)
344344
new BatchDispatched($batch)
345345
);
346346
}
347+
348+
/**
349+
* Dispatch the batch if the given truth test passes.
350+
*
351+
* @param bool|\Closure $boolean
352+
* @return \Illuminate\Bus\Batch|null
353+
*/
354+
public function dispatchIf($boolean)
355+
{
356+
return value($boolean) ? $this->dispatch() : null;
357+
}
358+
359+
/**
360+
* Dispatch the batch unless the given truth test passes.
361+
*
362+
* @param bool|\Closure $boolean
363+
* @return \Illuminate\Bus\Batch|null
364+
*/
365+
public function dispatchUnless($boolean)
366+
{
367+
return ! value($boolean) ? $this->dispatch() : null;
368+
}
347369
}

tests/Bus/BusPendingBatchTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,102 @@ public function test_batch_is_deleted_from_storage_if_exception_thrown_during_ba
8888

8989
$pendingBatch->dispatch();
9090
}
91+
92+
public function test_batch_is_dispatched_when_dispatchif_is_true()
93+
{
94+
$container = new Container;
95+
96+
$eventDispatcher = m::mock(Dispatcher::class);
97+
$eventDispatcher->shouldReceive('dispatch')->once();
98+
$container->instance(Dispatcher::class, $eventDispatcher);
99+
100+
$job = new class
101+
{
102+
use Batchable;
103+
};
104+
105+
$pendingBatch = new PendingBatch($container, new Collection([$job]));
106+
107+
$repository = m::mock(BatchRepository::class);
108+
$repository->shouldReceive('store')->once()->andReturn($batch = m::mock(stdClass::class));
109+
$batch->shouldReceive('add')->once()->andReturn($batch = m::mock(Batch::class));
110+
111+
$container->instance(BatchRepository::class, $repository);
112+
113+
$result = $pendingBatch->dispatchIf(true);
114+
115+
$this->assertInstanceOf(Batch::class, $result);
116+
}
117+
118+
public function test_batch_is_not_dispatched_when_dispatchif_is_false()
119+
{
120+
$container = new Container;
121+
122+
$eventDispatcher = m::mock(Dispatcher::class);
123+
$eventDispatcher->shouldNotReceive('dispatch');
124+
$container->instance(Dispatcher::class, $eventDispatcher);
125+
126+
$job = new class
127+
{
128+
use Batchable;
129+
};
130+
131+
$pendingBatch = new PendingBatch($container, new Collection([$job]));
132+
133+
$repository = m::mock(BatchRepository::class);
134+
$container->instance(BatchRepository::class, $repository);
135+
136+
$result = $pendingBatch->dispatchIf(false);
137+
138+
$this->assertNull($result);
139+
}
140+
141+
public function test_batch_is_dispatched_when_dispatchunless_is_false()
142+
{
143+
$container = new Container;
144+
145+
$eventDispatcher = m::mock(Dispatcher::class);
146+
$eventDispatcher->shouldReceive('dispatch')->once();
147+
$container->instance(Dispatcher::class, $eventDispatcher);
148+
149+
$job = new class
150+
{
151+
use Batchable;
152+
};
153+
154+
$pendingBatch = new PendingBatch($container, new Collection([$job]));
155+
156+
$repository = m::mock(BatchRepository::class);
157+
$repository->shouldReceive('store')->once()->andReturn($batch = m::mock(stdClass::class));
158+
$batch->shouldReceive('add')->once()->andReturn($batch = m::mock(Batch::class));
159+
160+
$container->instance(BatchRepository::class, $repository);
161+
162+
$result = $pendingBatch->dispatchUnless(false);
163+
164+
$this->assertInstanceOf(Batch::class, $result);
165+
}
166+
167+
public function test_batch_is_not_dispatched_when_dispatchunless_is_true()
168+
{
169+
$container = new Container;
170+
171+
$eventDispatcher = m::mock(Dispatcher::class);
172+
$eventDispatcher->shouldNotReceive('dispatch');
173+
$container->instance(Dispatcher::class, $eventDispatcher);
174+
175+
$job = new class
176+
{
177+
use Batchable;
178+
};
179+
180+
$pendingBatch = new PendingBatch($container, new Collection([$job]));
181+
182+
$repository = m::mock(BatchRepository::class);
183+
$container->instance(BatchRepository::class, $repository);
184+
185+
$result = $pendingBatch->dispatchUnless(true);
186+
187+
$this->assertNull($result);
188+
}
91189
}

0 commit comments

Comments
 (0)