Skip to content

Commit 2407c9b

Browse files
committed
Allow BusFake to use custom BusRepository
1 parent 011f2e1 commit 2407c9b

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/Illuminate/Support/Facades/Bus.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Support\Facades;
44

5+
use Illuminate\Bus\BatchRepository;
56
use Illuminate\Contracts\Bus\Dispatcher as BusDispatcherContract;
67
use Illuminate\Foundation\Bus\PendingChain;
78
use Illuminate\Support\Testing\Fakes\BusFake;
@@ -44,9 +45,9 @@ class Bus extends Facade
4445
* @param array|string $jobsToFake
4546
* @return \Illuminate\Support\Testing\Fakes\BusFake
4647
*/
47-
public static function fake($jobsToFake = [])
48+
public static function fake($jobsToFake = [], BatchRepository $batchRepository = null)
4849
{
49-
static::swap($fake = new BusFake(static::getFacadeRoot(), $jobsToFake));
50+
static::swap($fake = new BusFake(static::getFacadeRoot(), $jobsToFake, $batchRepository));
5051

5152
return $fake;
5253
}

src/Illuminate/Support/Testing/Fakes/BusFake.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Support\Testing\Fakes;
44

55
use Closure;
6+
use Illuminate\Bus\BatchRepository;
67
use Illuminate\Bus\PendingBatch;
78
use Illuminate\Contracts\Bus\QueueingDispatcher;
89
use Illuminate\Support\Arr;
@@ -75,13 +76,14 @@ class BusFake implements QueueingDispatcher
7576
*
7677
* @param \Illuminate\Contracts\Bus\QueueingDispatcher $dispatcher
7778
* @param array|string $jobsToFake
79+
* @param BatchRepository|null $jobsToFake
7880
* @return void
7981
*/
80-
public function __construct(QueueingDispatcher $dispatcher, $jobsToFake = [])
82+
public function __construct(QueueingDispatcher $dispatcher, $jobsToFake = [], BatchRepository $batchRepository = null)
8183
{
8284
$this->dispatcher = $dispatcher;
8385
$this->jobsToFake = Arr::wrap($jobsToFake);
84-
$this->batchRepository = new BatchRepositoryFake;
86+
$this->batchRepository = $batchRepository ?: new BatchRepositoryFake;
8587
}
8688

8789
/**

tests/Support/SupportTestingBusFakeTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Bus\Batch;
66
use Illuminate\Bus\Queueable;
77
use Illuminate\Contracts\Bus\QueueingDispatcher;
8+
use Illuminate\Support\Testing\Fakes\BatchRepositoryFake;
89
use Illuminate\Support\Testing\Fakes\BusFake;
910
use Mockery as m;
1011
use PHPUnit\Framework\Constraint\ExceptionMessage;
@@ -28,6 +29,20 @@ protected function tearDown(): void
2829
m::close();
2930
}
3031

32+
public function testItUsesCustomBusRepository()
33+
{
34+
$busRepository = new BatchRepositoryFake;
35+
36+
$fake = new BusFake(m::mock(QueueingDispatcher::class), [], $busRepository);
37+
38+
$this->assertNull($fake->findBatch('non-existent-batch'));
39+
40+
$batch = $fake->batch([])->dispatch();
41+
42+
$this->assertSame($batch, $fake->findBatch($batch->id));
43+
$this->assertSame($batch, $busRepository->find($batch->id));
44+
}
45+
3146
public function testAssertDispatched()
3247
{
3348
try {

0 commit comments

Comments
 (0)