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] Improve testability of batched jobs #44075

Merged
merged 4 commits into from
Sep 12, 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
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