Skip to content

Commit

Permalink
Allow BusFake to use custom BusRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaliak committed Dec 6, 2022
1 parent 011f2e1 commit 2407c9b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/Illuminate/Support/Facades/Bus.php
Expand Up @@ -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;
Expand Down Expand Up @@ -44,9 +45,9 @@ class Bus extends Facade
* @param array|string $jobsToFake
* @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;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Illuminate/Support/Testing/Fakes/BusFake.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/Support/SupportTestingBusFakeTest.php
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down

0 comments on commit 2407c9b

Please sign in to comment.