Skip to content

Commit

Permalink
Improve testability of batched jobs (#44075)
Browse files Browse the repository at this point in the history
* Track batched jobs

* Add helper to fake batch jobs

* formatting

* formatting

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
jasonmccreary and taylorotwell authored Sep 12, 2022
1 parent eff2275 commit a155ccd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
29 changes: 22 additions & 7 deletions src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@

class BatchRepositoryFake implements BatchRepository
{
/**
* The batches stored in the repository.
*
* @var \Illuminate\Bus\Batch[]
*/
protected $batches = [];

/**
* Retrieve a list of batches.
*
Expand All @@ -22,7 +29,7 @@ class BatchRepositoryFake implements BatchRepository
*/
public function get($limit, $before)
{
return [];
return $this->batches;
}

/**
Expand All @@ -33,7 +40,7 @@ public function get($limit, $before)
*/
public function find(string $batchId)
{
//
return $this->batches[$batchId] ?? null;
}

/**
Expand All @@ -44,10 +51,12 @@ public function find(string $batchId)
*/
public function store(PendingBatch $batch)
{
return new Batch(
$id = (string) Str::orderedUuid();

$this->batches[$id] = new Batch(
new QueueFake(Facade::getFacadeApplication()),
$this,
(string) Str::orderedUuid(),
$id,
$batch->name,
count($batch->jobs),
count($batch->jobs),
Expand All @@ -58,6 +67,8 @@ public function store(PendingBatch $batch)
null,
null
);

return $this->batches[$id];
}

/**
Expand Down Expand Up @@ -104,7 +115,9 @@ public function incrementFailedJobs(string $batchId, string $jobId)
*/
public function markAsFinished(string $batchId)
{
//
if (isset($this->batches[$batchId])) {
$this->batches[$batchId]->finishedAt = now();
}
}

/**
Expand All @@ -115,7 +128,9 @@ public function markAsFinished(string $batchId)
*/
public function cancel(string $batchId)
{
//
if (isset($this->batches[$batchId])) {
$this->batches[$batchId]->cancelledAt = now();
}
}

/**
Expand All @@ -126,7 +141,7 @@ public function cancel(string $batchId)
*/
public function delete(string $batchId)
{
//
unset($this->batches[$batchId]);
}

/**
Expand Down
13 changes: 10 additions & 3 deletions src/Illuminate/Support/Testing/Fakes/BusFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class BusFake implements QueueingDispatcher
*/
protected $jobsToFake;

/**
* The fake repository to track batched jobs.
*
* @var \Illuminate\Bus\BatchRepository
*/
protected $batchRepository;

/**
* The commands that have been dispatched.
*
Expand Down Expand Up @@ -66,8 +73,8 @@ class BusFake implements QueueingDispatcher
public function __construct(QueueingDispatcher $dispatcher, $jobsToFake = [])
{
$this->dispatcher = $dispatcher;

$this->jobsToFake = Arr::wrap($jobsToFake);
$this->batchRepository = new BatchRepositoryFake;
}

/**
Expand Down Expand Up @@ -634,7 +641,7 @@ public function chain($jobs)
*/
public function findBatch(string $batchId)
{
//
return $this->batchRepository->find($batchId);
}

/**
Expand All @@ -658,7 +665,7 @@ public function recordPendingBatch(PendingBatch $pendingBatch)
{
$this->batches[] = $pendingBatch;

return (new BatchRepositoryFake)->store($pendingBatch);
return $this->batchRepository->store($pendingBatch);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/Support/SupportTestingBusFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,26 @@ public function testAssertNothingBatched()
$this->assertThat($e, new ExceptionMessage('Batched jobs were dispatched unexpectedly.'));
}
}

public function testFindBatch()
{
$this->assertNull($this->fake->findBatch('non-existent-batch'));

$batch = $this->fake->batch([])->dispatch();

$this->assertSame($batch, $this->fake->findBatch($batch->id));
}

public function testBatchesCanBeCancelled()
{
$batch = $this->fake->batch([])->dispatch();

$this->assertFalse($batch->cancelled());

$batch->cancel();

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

class BusJobStub
Expand Down

0 comments on commit a155ccd

Please sign in to comment.