Skip to content

Commit ce12bc0

Browse files
taylorotwellStyleCIBotcrynobone
authored
Fake Batches (#44104)
* work on batch fake * Apply fixes from StyleCI * add deleted method * add other dates * return array * Update src/Illuminate/Bus/Batchable.php Co-authored-by: Mior Muhammad Zaki <[email protected]> Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: Mior Muhammad Zaki <[email protected]>
1 parent b4d75cf commit ce12bc0

File tree

2 files changed

+213
-0
lines changed

2 files changed

+213
-0
lines changed

src/Illuminate/Bus/Batchable.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Illuminate\Bus;
44

5+
use Carbon\CarbonImmutable;
56
use Illuminate\Container\Container;
7+
use Illuminate\Support\Str;
8+
use Illuminate\Support\Testing\Fakes\BatchFake;
69

710
trait Batchable
811
{
@@ -13,13 +16,24 @@ trait Batchable
1316
*/
1417
public $batchId;
1518

19+
/**
20+
* The fake batch, if applicable.
21+
*
22+
* @var \Illuminate\Support\Testing\BatchFake
23+
*/
24+
private $fakeBatch;
25+
1626
/**
1727
* Get the batch instance for the job, if applicable.
1828
*
1929
* @return \Illuminate\Bus\Batch|null
2030
*/
2131
public function batch()
2232
{
33+
if ($this->fakeBatch) {
34+
return $this->fakeBatch;
35+
}
36+
2337
if ($this->batchId) {
2438
return Container::getInstance()->make(BatchRepository::class)->find($this->batchId);
2539
}
@@ -49,4 +63,43 @@ public function withBatchId(string $batchId)
4963

5064
return $this;
5165
}
66+
67+
/**
68+
* Indicate that the job should use a fake batch.
69+
*
70+
* @param string $id
71+
* @param string $name
72+
* @param int $totalJobs
73+
* @param int $pendingJobs
74+
* @param int $failedJobs
75+
* @param array $failedJobIds
76+
* @param array $options
77+
* @return array{0: $this, 1: \Illuminate\Support\Testing\BatchFake}
78+
*/
79+
public function withFakeBatch(string $id = '',
80+
string $name = '',
81+
int $totalJobs = 0,
82+
int $pendingJobs = 0,
83+
int $failedJobs = 0,
84+
array $failedJobIds = [],
85+
array $options = [],
86+
CarbonImmutable $createdAt = null,
87+
?CarbonImmutable $cancelledAt = null,
88+
?CarbonImmutable $finishedAt = null)
89+
{
90+
$this->fakeBatch = new BatchFake(
91+
empty($id) ? (string) Str::uuid() : $id,
92+
$name,
93+
$totalJobs,
94+
$pendingJobs,
95+
$failedJobs,
96+
$failedJobIds,
97+
$options,
98+
$createdAt ?? CarbonImmutable::now(),
99+
$cancelledAt,
100+
$finishedAt,
101+
);
102+
103+
return [$this, $this->fakeBatch];
104+
}
52105
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
namespace Illuminate\Support\Testing\Fakes;
4+
5+
use Carbon\CarbonImmutable;
6+
use Illuminate\Bus\Batch;
7+
use Illuminate\Support\Carbon;
8+
9+
class BatchFake extends Batch
10+
{
11+
/**
12+
* The jobs that have been added to the batch.
13+
*
14+
* @var array
15+
*/
16+
public $added = [];
17+
18+
/**
19+
* Indicates if the batch has been deleted.
20+
*
21+
* @var bool
22+
*/
23+
public $deleted = false;
24+
25+
/**
26+
* Create a new batch instance.
27+
*
28+
* @param string $id
29+
* @param string $name
30+
* @param int $totalJobs
31+
* @param int $pendingJobs
32+
* @param int $failedJobs
33+
* @param array $failedJobIds
34+
* @param array $options
35+
* @param \Carbon\CarbonImmutable $createdAt
36+
* @param \Carbon\CarbonImmutable|null $cancelledAt
37+
* @param \Carbon\CarbonImmutable|null $finishedAt
38+
* @return void
39+
*/
40+
public function __construct(string $id,
41+
string $name,
42+
int $totalJobs,
43+
int $pendingJobs,
44+
int $failedJobs,
45+
array $failedJobIds,
46+
array $options,
47+
CarbonImmutable $createdAt,
48+
?CarbonImmutable $cancelledAt = null,
49+
?CarbonImmutable $finishedAt = null)
50+
{
51+
$this->id = $id;
52+
$this->name = $name;
53+
$this->totalJobs = $totalJobs;
54+
$this->pendingJobs = $pendingJobs;
55+
$this->failedJobs = $failedJobs;
56+
$this->failedJobIds = $failedJobIds;
57+
$this->options = $options;
58+
$this->createdAt = $createdAt;
59+
$this->cancelledAt = $cancelledAt;
60+
$this->finishedAt = $finishedAt;
61+
}
62+
63+
/**
64+
* Get a fresh instance of the batch represented by this ID.
65+
*
66+
* @return self
67+
*/
68+
public function fresh()
69+
{
70+
return $this;
71+
}
72+
73+
/**
74+
* Add additional jobs to the batch.
75+
*
76+
* @param \Illuminate\Support\Enumerable|object|array $jobs
77+
* @return self
78+
*/
79+
public function add($jobs)
80+
{
81+
$this->added[] = array_merge($this->added, $jobs);
82+
83+
return $this;
84+
}
85+
86+
/**
87+
* Record that a job within the batch finished successfully, executing any callbacks if necessary.
88+
*
89+
* @param string $jobId
90+
* @return void
91+
*/
92+
public function recordSuccessfulJob(string $jobId)
93+
{
94+
//
95+
}
96+
97+
/**
98+
* Decrement the pending jobs for the batch.
99+
*
100+
* @param string $jobId
101+
* @return \Illuminate\Bus\UpdatedBatchJobCounts
102+
*/
103+
public function decrementPendingJobs(string $jobId)
104+
{
105+
//
106+
}
107+
108+
/**
109+
* Record that a job within the batch failed to finish successfully, executing any callbacks if necessary.
110+
*
111+
* @param string $jobId
112+
* @param \Throwable $e
113+
* @return void
114+
*/
115+
public function recordFailedJob(string $jobId, $e)
116+
{
117+
//
118+
}
119+
120+
/**
121+
* Increment the failed jobs for the batch.
122+
*
123+
* @param string $jobId
124+
* @return \Illuminate\Bus\UpdatedBatchJobCounts
125+
*/
126+
public function incrementFailedJobs(string $jobId)
127+
{
128+
return new UpdatedBatchJobCounts;
129+
}
130+
131+
/**
132+
* Cancel the batch.
133+
*
134+
* @return void
135+
*/
136+
public function cancel()
137+
{
138+
$this->cancelledAt = Carbon::now();
139+
}
140+
141+
/**
142+
* Delete the batch from storage.
143+
*
144+
* @return void
145+
*/
146+
public function delete()
147+
{
148+
$this->deleted = true;
149+
}
150+
151+
/**
152+
* Determine if the batch has been deleted.
153+
*
154+
* @return bool
155+
*/
156+
public function deleted()
157+
{
158+
return $this->deleted;
159+
}
160+
}

0 commit comments

Comments
 (0)