-
Notifications
You must be signed in to change notification settings - Fork 5
/
FakeProcessSequence.php
121 lines (105 loc) · 3.4 KB
/
FakeProcessSequence.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Illuminate\Process;
use Illuminate\Contracts\Process\ProcessResult as ProcessResultContract;
use OutOfBoundsException;
class FakeProcessSequence
{
/**
* The fake process results and descriptions.
*
* @var array
*/
protected $processes = [];
/**
* Indicates that invoking this sequence when it is empty should throw an exception.
*
* @var bool
*/
protected $failWhenEmpty = true;
/**
* The response that should be returned when the sequence is empty.
*
* @var \Illuminate\Contracts\Process\ProcessResult|\Illuminate\Process\FakeProcessDescription
*/
protected $emptyProcess;
/**
* Create a new fake process sequence instance.
*
* @param array $processes
* @return void
*/
public function __construct(array $processes = [])
{
$this->processes = $processes;
}
/**
* Push a new process result or description onto the sequence.
*
* @param \Illuminate\Contracts\Process\ProcessResult|\Illuminate\Process\FakeProcessDescription|array|string $process
* @return $this
*/
public function push(ProcessResultContract|FakeProcessDescription|array|string $process)
{
$this->processes[] = $this->toProcessResult($process);
return $this;
}
/**
* Make the sequence return a default result when it is empty.
*
* @param \Illuminate\Contracts\Process\ProcessResult|\Illuminate\Process\FakeProcessDescription|array|string $process
* @return $this
*/
public function whenEmpty(ProcessResultContract|FakeProcessDescription|array|string $process)
{
$this->failWhenEmpty = false;
$this->emptyProcess = $this->toProcessResult($process);
return $this;
}
/**
* Convert the given result into an actual process result or description.
*
* @param \Illuminate\Contracts\Process\ProcessResult|\Illuminate\Process\FakeProcessDescription|array|string $process
* @return \Illuminate\Contracts\Process\ProcessResult|\Illuminate\Process\FakeProcessDescription
*/
protected function toProcessResult(ProcessResultContract|FakeProcessDescription|array|string $process)
{
return is_array($process) || is_string($process)
? new FakeProcessResult(output: $process)
: $process;
}
/**
* Make the sequence return a default result when it is empty.
*
* @return $this
*/
public function dontFailWhenEmpty()
{
return $this->whenEmpty(new FakeProcessResult);
}
/**
* Indicate that this sequence has depleted all of its process results.
*
* @return bool
*/
public function isEmpty()
{
return count($this->processes) === 0;
}
/**
* Get the next process in the sequence.
*
* @return \Illuminate\Contracts\Process\ProcessResult|\Illuminate\Process\FakeProcessDescription
*
* @throws \OutOfBoundsException
*/
public function __invoke()
{
if ($this->failWhenEmpty && count($this->processes) === 0) {
throw new OutOfBoundsException('A process was invoked, but the process result sequence is empty.');
}
if (! $this->failWhenEmpty && count($this->processes) === 0) {
return value($this->emptyProcess ?? new FakeProcessResult);
}
return array_shift($this->processes);
}
}