diff --git a/src/Illuminate/Support/Facades/Bus.php b/src/Illuminate/Support/Facades/Bus.php index 9d6352a4692f..fcee13b61a28 100644 --- a/src/Illuminate/Support/Facades/Bus.php +++ b/src/Illuminate/Support/Facades/Bus.php @@ -2,6 +2,7 @@ namespace Illuminate\Support\Facades; +use Illuminate\Bus\BatchRepository; use Illuminate\Contracts\Bus\Dispatcher as BusDispatcherContract; use Illuminate\Foundation\Bus\PendingChain; use Illuminate\Support\Testing\Fakes\BusFake; @@ -42,11 +43,12 @@ class Bus extends Facade * Replace the bound instance with a fake. * * @param array|string $jobsToFake + * @param \Illuminate\Bus\BatchRepository|null $batchRepository * @return \Illuminate\Support\Testing\Fakes\BusFake */ - public static function fake($jobsToFake = []) + public static function fake($jobsToFake = [], BatchRepository $batchRepository = null) { - static::swap($fake = new BusFake(static::getFacadeRoot(), $jobsToFake)); + static::swap($fake = new BusFake(static::getFacadeRoot(), $jobsToFake, $batchRepository)); return $fake; } diff --git a/src/Illuminate/Support/Testing/Fakes/BusFake.php b/src/Illuminate/Support/Testing/Fakes/BusFake.php index 0c9f0618052d..900edf23f486 100644 --- a/src/Illuminate/Support/Testing/Fakes/BusFake.php +++ b/src/Illuminate/Support/Testing/Fakes/BusFake.php @@ -3,6 +3,7 @@ namespace Illuminate\Support\Testing\Fakes; use Closure; +use Illuminate\Bus\BatchRepository; use Illuminate\Bus\PendingBatch; use Illuminate\Contracts\Bus\QueueingDispatcher; use Illuminate\Support\Arr; @@ -75,13 +76,14 @@ class BusFake implements QueueingDispatcher * * @param \Illuminate\Contracts\Bus\QueueingDispatcher $dispatcher * @param array|string $jobsToFake + * @param BatchRepository|null $jobsToFake * @return void */ - public function __construct(QueueingDispatcher $dispatcher, $jobsToFake = []) + public function __construct(QueueingDispatcher $dispatcher, $jobsToFake = [], BatchRepository $batchRepository = null) { $this->dispatcher = $dispatcher; $this->jobsToFake = Arr::wrap($jobsToFake); - $this->batchRepository = new BatchRepositoryFake; + $this->batchRepository = $batchRepository ?: new BatchRepositoryFake; } /** diff --git a/tests/Support/SupportTestingBusFakeTest.php b/tests/Support/SupportTestingBusFakeTest.php index 600de62b6544..9b70ddd2bc02 100644 --- a/tests/Support/SupportTestingBusFakeTest.php +++ b/tests/Support/SupportTestingBusFakeTest.php @@ -5,6 +5,7 @@ use Illuminate\Bus\Batch; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Bus\QueueingDispatcher; +use Illuminate\Support\Testing\Fakes\BatchRepositoryFake; use Illuminate\Support\Testing\Fakes\BusFake; use Mockery as m; use PHPUnit\Framework\Constraint\ExceptionMessage; @@ -28,6 +29,20 @@ protected function tearDown(): void m::close(); } + public function testItUsesCustomBusRepository() + { + $busRepository = new BatchRepositoryFake; + + $fake = new BusFake(m::mock(QueueingDispatcher::class), [], $busRepository); + + $this->assertNull($fake->findBatch('non-existent-batch')); + + $batch = $fake->batch([])->dispatch(); + + $this->assertSame($batch, $fake->findBatch($batch->id)); + $this->assertSame($batch, $busRepository->find($batch->id)); + } + public function testAssertDispatched() { try {