From 5802bdfc41df541eb08aa95cf69196ee535fb855 Mon Sep 17 00:00:00 2001 From: Frankie Jarrett Date: Wed, 10 Jan 2024 11:36:37 -0600 Subject: [PATCH] dispatchIf() and dispatchUnless() for batches --- src/Illuminate/Bus/PendingBatch.php | 22 +++++++ tests/Bus/BusPendingBatchTest.php | 98 +++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/src/Illuminate/Bus/PendingBatch.php b/src/Illuminate/Bus/PendingBatch.php index 60ff3884c8b8..b9622f8cc064 100644 --- a/src/Illuminate/Bus/PendingBatch.php +++ b/src/Illuminate/Bus/PendingBatch.php @@ -344,4 +344,26 @@ protected function dispatchExistingBatch($batch) new BatchDispatched($batch) ); } + + /** + * Dispatch the batch if the given truth test passes. + * + * @param bool|\Closure $boolean + * @return \Illuminate\Bus\Batch|null + */ + public function dispatchIf($boolean) + { + return value($boolean) ? $this->dispatch() : null; + } + + /** + * Dispatch the batch unless the given truth test passes. + * + * @param bool|\Closure $boolean + * @return \Illuminate\Bus\Batch|null + */ + public function dispatchUnless($boolean) + { + return ! value($boolean) ? $this->dispatch() : null; + } } diff --git a/tests/Bus/BusPendingBatchTest.php b/tests/Bus/BusPendingBatchTest.php index 7cd5f7e3a80e..6a6e9186e686 100644 --- a/tests/Bus/BusPendingBatchTest.php +++ b/tests/Bus/BusPendingBatchTest.php @@ -88,4 +88,102 @@ public function test_batch_is_deleted_from_storage_if_exception_thrown_during_ba $pendingBatch->dispatch(); } + + public function test_batch_is_dispatched_when_dispatchif_is_true() + { + $container = new Container; + + $eventDispatcher = m::mock(Dispatcher::class); + $eventDispatcher->shouldReceive('dispatch')->once(); + $container->instance(Dispatcher::class, $eventDispatcher); + + $job = new class + { + use Batchable; + }; + + $pendingBatch = new PendingBatch($container, new Collection([$job])); + + $repository = m::mock(BatchRepository::class); + $repository->shouldReceive('store')->once()->andReturn($batch = m::mock(stdClass::class)); + $batch->shouldReceive('add')->once()->andReturn($batch = m::mock(Batch::class)); + + $container->instance(BatchRepository::class, $repository); + + $result = $pendingBatch->dispatchIf(true); + + $this->assertInstanceOf(Batch::class, $result); + } + + public function test_batch_is_not_dispatched_when_dispatchif_is_false() + { + $container = new Container; + + $eventDispatcher = m::mock(Dispatcher::class); + $eventDispatcher->shouldNotReceive('dispatch'); + $container->instance(Dispatcher::class, $eventDispatcher); + + $job = new class + { + use Batchable; + }; + + $pendingBatch = new PendingBatch($container, new Collection([$job])); + + $repository = m::mock(BatchRepository::class); + $container->instance(BatchRepository::class, $repository); + + $result = $pendingBatch->dispatchIf(false); + + $this->assertNull($result); + } + + public function test_batch_is_dispatched_when_dispatchunless_is_false() + { + $container = new Container; + + $eventDispatcher = m::mock(Dispatcher::class); + $eventDispatcher->shouldReceive('dispatch')->once(); + $container->instance(Dispatcher::class, $eventDispatcher); + + $job = new class + { + use Batchable; + }; + + $pendingBatch = new PendingBatch($container, new Collection([$job])); + + $repository = m::mock(BatchRepository::class); + $repository->shouldReceive('store')->once()->andReturn($batch = m::mock(stdClass::class)); + $batch->shouldReceive('add')->once()->andReturn($batch = m::mock(Batch::class)); + + $container->instance(BatchRepository::class, $repository); + + $result = $pendingBatch->dispatchUnless(false); + + $this->assertInstanceOf(Batch::class, $result); + } + + public function test_batch_is_not_dispatched_when_dispatchunless_is_true() + { + $container = new Container; + + $eventDispatcher = m::mock(Dispatcher::class); + $eventDispatcher->shouldNotReceive('dispatch'); + $container->instance(Dispatcher::class, $eventDispatcher); + + $job = new class + { + use Batchable; + }; + + $pendingBatch = new PendingBatch($container, new Collection([$job])); + + $repository = m::mock(BatchRepository::class); + $container->instance(BatchRepository::class, $repository); + + $result = $pendingBatch->dispatchUnless(true); + + $this->assertNull($result); + } }