Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add helper to dispatch fake job batches #44176

Merged
merged 2 commits into from
Sep 21, 2022
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
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/Bus.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/**
* @method static \Illuminate\Bus\Batch|null findBatch(string $batchId)
* @method static \Illuminate\Bus\PendingBatch batch(array|mixed $jobs)
* @method static \Illuminate\Bus\Batch dispatchFakeBatch($name = '')
* @method static \Illuminate\Contracts\Bus\Dispatcher map(array $map)
* @method static \Illuminate\Contracts\Bus\Dispatcher pipeThrough(array $pipes)
* @method static \Illuminate\Foundation\Bus\PendingChain chain(array $jobs)
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/BusFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,17 @@ public function batch($jobs)
return new PendingBatchFake($this, Collection::wrap($jobs));
}

/**
* Dispatch an empty job batch for testing.
*
* @param string $name
* @return \Illuminate\Bus\Batch
*/
public function dispatchFakeBatch($name = '')
{
return $this->batch([])->name($name)->dispatch();
}

/**
* Record the fake pending batch dispatch.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/Support/SupportTestingBusFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Support;

use Illuminate\Bus\Batch;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Bus\QueueingDispatcher;
use Illuminate\Support\Testing\Fakes\BusFake;
Expand Down Expand Up @@ -520,6 +521,25 @@ public function testBatchesCanBeCancelled()

$this->assertTrue($batch->cancelled());
}

public function testDispatchFakeBatch()
{
$this->fake->assertNothingBatched();

$batch = $this->fake->dispatchFakeBatch('my fake job batch');

$this->fake->assertBatchCount(1);
$this->assertInstanceOf(Batch::class, $batch);
$this->assertSame('my fake job batch', $batch->name);
$this->assertSame(0, $batch->totalJobs);

$batch = $this->fake->dispatchFakeBatch();

$this->fake->assertBatchCount(2);
$this->assertInstanceOf(Batch::class, $batch);
$this->assertSame('', $batch->name);
$this->assertSame(0, $batch->totalJobs);
}
}

class BusJobStub
Expand Down