Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Illuminate/Bus/PendingBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
98 changes: 98 additions & 0 deletions tests/Bus/BusPendingBatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}