Skip to content

Commit a155ccd

Browse files
Improve testability of batched jobs (#44075)
* Track batched jobs * Add helper to fake batch jobs * formatting * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent eff2275 commit a155ccd

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

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

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

1414
class BatchRepositoryFake implements BatchRepository
1515
{
16+
/**
17+
* The batches stored in the repository.
18+
*
19+
* @var \Illuminate\Bus\Batch[]
20+
*/
21+
protected $batches = [];
22+
1623
/**
1724
* Retrieve a list of batches.
1825
*
@@ -22,7 +29,7 @@ class BatchRepositoryFake implements BatchRepository
2229
*/
2330
public function get($limit, $before)
2431
{
25-
return [];
32+
return $this->batches;
2633
}
2734

2835
/**
@@ -33,7 +40,7 @@ public function get($limit, $before)
3340
*/
3441
public function find(string $batchId)
3542
{
36-
//
43+
return $this->batches[$batchId] ?? null;
3744
}
3845

3946
/**
@@ -44,10 +51,12 @@ public function find(string $batchId)
4451
*/
4552
public function store(PendingBatch $batch)
4653
{
47-
return new Batch(
54+
$id = (string) Str::orderedUuid();
55+
56+
$this->batches[$id] = new Batch(
4857
new QueueFake(Facade::getFacadeApplication()),
4958
$this,
50-
(string) Str::orderedUuid(),
59+
$id,
5160
$batch->name,
5261
count($batch->jobs),
5362
count($batch->jobs),
@@ -58,6 +67,8 @@ public function store(PendingBatch $batch)
5867
null,
5968
null
6069
);
70+
71+
return $this->batches[$id];
6172
}
6273

6374
/**
@@ -104,7 +115,9 @@ public function incrementFailedJobs(string $batchId, string $jobId)
104115
*/
105116
public function markAsFinished(string $batchId)
106117
{
107-
//
118+
if (isset($this->batches[$batchId])) {
119+
$this->batches[$batchId]->finishedAt = now();
120+
}
108121
}
109122

110123
/**
@@ -115,7 +128,9 @@ public function markAsFinished(string $batchId)
115128
*/
116129
public function cancel(string $batchId)
117130
{
118-
//
131+
if (isset($this->batches[$batchId])) {
132+
$this->batches[$batchId]->cancelledAt = now();
133+
}
119134
}
120135

121136
/**
@@ -126,7 +141,7 @@ public function cancel(string $batchId)
126141
*/
127142
public function delete(string $batchId)
128143
{
129-
//
144+
unset($this->batches[$batchId]);
130145
}
131146

132147
/**

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ class BusFake implements QueueingDispatcher
2828
*/
2929
protected $jobsToFake;
3030

31+
/**
32+
* The fake repository to track batched jobs.
33+
*
34+
* @var \Illuminate\Bus\BatchRepository
35+
*/
36+
protected $batchRepository;
37+
3138
/**
3239
* The commands that have been dispatched.
3340
*
@@ -66,8 +73,8 @@ class BusFake implements QueueingDispatcher
6673
public function __construct(QueueingDispatcher $dispatcher, $jobsToFake = [])
6774
{
6875
$this->dispatcher = $dispatcher;
69-
7076
$this->jobsToFake = Arr::wrap($jobsToFake);
77+
$this->batchRepository = new BatchRepositoryFake;
7178
}
7279

7380
/**
@@ -634,7 +641,7 @@ public function chain($jobs)
634641
*/
635642
public function findBatch(string $batchId)
636643
{
637-
//
644+
return $this->batchRepository->find($batchId);
638645
}
639646

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

661-
return (new BatchRepositoryFake)->store($pendingBatch);
668+
return $this->batchRepository->store($pendingBatch);
662669
}
663670

664671
/**

tests/Support/SupportTestingBusFakeTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,26 @@ public function testAssertNothingBatched()
468468
$this->assertThat($e, new ExceptionMessage('Batched jobs were dispatched unexpectedly.'));
469469
}
470470
}
471+
472+
public function testFindBatch()
473+
{
474+
$this->assertNull($this->fake->findBatch('non-existent-batch'));
475+
476+
$batch = $this->fake->batch([])->dispatch();
477+
478+
$this->assertSame($batch, $this->fake->findBatch($batch->id));
479+
}
480+
481+
public function testBatchesCanBeCancelled()
482+
{
483+
$batch = $this->fake->batch([])->dispatch();
484+
485+
$this->assertFalse($batch->cancelled());
486+
487+
$batch->cancel();
488+
489+
$this->assertTrue($batch->cancelled());
490+
}
471491
}
472492

473493
class BusJobStub

0 commit comments

Comments
 (0)