1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of php-task library. |
5
|
|
|
* |
6
|
|
|
* (c) php-task |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Task\TaskBundle\Tests\Unit\Executor; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Process\Process; |
15
|
|
|
use Task\Execution\TaskExecutionInterface; |
16
|
|
|
use Task\Executor\FailedException; |
17
|
|
|
use Task\Executor\RetryTaskHandlerInterface; |
18
|
|
|
use Task\Handler\TaskHandlerFactoryInterface; |
19
|
|
|
use Task\Handler\TaskHandlerInterface; |
20
|
|
|
use Task\Storage\TaskExecutionRepositoryInterface; |
21
|
|
|
use Task\TaskBundle\Executor\ExecutionProcessFactory; |
22
|
|
|
use Task\TaskBundle\Executor\SeparateProcessException; |
23
|
|
|
use Task\TaskBundle\Executor\SeparateProcessExecutor; |
24
|
|
|
|
25
|
|
|
class SeparateProcessExecutorTest extends \PHPUnit_Framework_TestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var TaskHandlerFactoryInterface |
29
|
|
|
*/ |
30
|
|
|
private $handlerFactory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var TaskExecutionRepositoryInterface |
34
|
|
|
*/ |
35
|
|
|
private $executionRepository; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ExecutionProcessFactory |
39
|
|
|
*/ |
40
|
|
|
private $processFactory; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var SeparateProcessExecutor |
44
|
|
|
*/ |
45
|
|
|
private $executor; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var TaskHandlerInterface |
49
|
|
|
*/ |
50
|
|
|
private $handler; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var RetryTaskHandlerInterface |
54
|
|
|
*/ |
55
|
|
|
private $retryHandler; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var TaskExecutionInterface |
59
|
|
|
*/ |
60
|
|
|
private $execution; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var Process |
64
|
|
|
*/ |
65
|
|
|
private $process; |
66
|
|
|
|
67
|
|
|
protected function setUp() |
68
|
|
|
{ |
69
|
|
|
$this->handlerFactory = $this->prophesize(TaskHandlerFactoryInterface::class); |
|
|
|
|
70
|
|
|
$this->executionRepository = $this->prophesize(TaskExecutionRepositoryInterface::class); |
|
|
|
|
71
|
|
|
$this->processFactory = $this->prophesize(ExecutionProcessFactory::class); |
|
|
|
|
72
|
|
|
|
73
|
|
|
$this->executor = new SeparateProcessExecutor( |
74
|
|
|
$this->handlerFactory->reveal(), $this->executionRepository->reveal(), $this->processFactory->reveal() |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$this->handler = $this->prophesize(TaskHandlerInterface::class); |
|
|
|
|
78
|
|
|
$this->retryHandler = $this->prophesize(TaskHandlerInterface::class); |
|
|
|
|
79
|
|
|
$this->retryHandler->willImplement(RetryTaskHandlerInterface::class); |
80
|
|
|
|
81
|
|
|
$this->handlerFactory->create('TaskHandler')->willReturn($this->handler->reveal()); |
82
|
|
|
$this->handlerFactory->create('RetryTaskHandler')->willReturn($this->retryHandler->reveal()); |
83
|
|
|
|
84
|
|
|
$this->execution = $this->prophesize(TaskExecutionInterface::class); |
|
|
|
|
85
|
|
|
$this->execution->getUuid()->willReturn('123-123-123'); |
86
|
|
|
$this->execution->getAttempts()->willReturn(1); |
87
|
|
|
|
88
|
|
|
$this->process = $this->prophesize(Process::class); |
|
|
|
|
89
|
|
|
$this->processFactory->create('123-123-123')->willReturn($this->process->reveal()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testExecute() |
93
|
|
|
{ |
94
|
|
|
$this->execution->getHandlerClass()->willReturn('TaskHandler'); |
|
|
|
|
95
|
|
|
|
96
|
|
|
$this->process->run()->shouldBeCalled(); |
|
|
|
|
97
|
|
|
$this->process->isSuccessful()->willReturn(true); |
|
|
|
|
98
|
|
|
|
99
|
|
|
$this->process->getOutput()->willReturn('TEST'); |
|
|
|
|
100
|
|
|
|
101
|
|
|
$result = $this->executor->execute($this->execution->reveal()); |
|
|
|
|
102
|
|
|
$this->assertEquals('TEST', $result); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testExecuteException() |
106
|
|
|
{ |
107
|
|
|
$this->execution->getHandlerClass()->willReturn('TaskHandler'); |
|
|
|
|
108
|
|
|
|
109
|
|
|
$this->process->run()->shouldBeCalled(); |
|
|
|
|
110
|
|
|
$this->process->isSuccessful()->willReturn(false); |
|
|
|
|
111
|
|
|
|
112
|
|
|
$this->process->getErrorOutput()->willReturn('TEST'); |
|
|
|
|
113
|
|
|
|
114
|
|
|
try { |
115
|
|
|
$this->executor->execute($this->execution->reveal()); |
|
|
|
|
116
|
|
|
|
117
|
|
|
$this->fail('No FailedException was thrown'); |
118
|
|
|
} catch (\Exception $exception) { |
119
|
|
|
$this->assertInstanceOf(FailedException::class, $exception); |
120
|
|
|
$this->assertInstanceOf(SeparateProcessException::class, $exception->getPrevious()); |
121
|
|
|
$this->assertEquals('TEST', $exception->getPrevious()->__toString()); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testExecuteFailedException() |
126
|
|
|
{ |
127
|
|
|
$this->execution->getHandlerClass()->willReturn('TaskHandler'); |
|
|
|
|
128
|
|
|
|
129
|
|
|
$this->process->run()->shouldBeCalled(); |
|
|
|
|
130
|
|
|
$this->process->isSuccessful()->willReturn(false); |
|
|
|
|
131
|
|
|
|
132
|
|
|
$this->process->getErrorOutput()->willReturn(FailedException::class . PHP_EOL . 'TEST'); |
|
|
|
|
133
|
|
|
|
134
|
|
|
try { |
135
|
|
|
$this->executor->execute($this->execution->reveal()); |
|
|
|
|
136
|
|
|
|
137
|
|
|
$this->fail('No FailedException was thrown'); |
138
|
|
|
} catch (\Exception $exception) { |
139
|
|
|
$this->assertInstanceOf(FailedException::class, $exception); |
140
|
|
|
$this->assertInstanceOf(SeparateProcessException::class, $exception->getPrevious()); |
141
|
|
|
$this->assertEquals('TEST', $exception->getPrevious()->__toString()); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testExecuteRetryFailedException() |
146
|
|
|
{ |
147
|
|
|
$this->execution->getHandlerClass()->willReturn('RetryTaskHandler'); |
|
|
|
|
148
|
|
|
$this->retryHandler->getMaximumAttempts()->willReturn(3); |
|
|
|
|
149
|
|
|
|
150
|
|
|
$this->execution->incrementAttempts()->shouldNotBeCalled(); |
|
|
|
|
151
|
|
|
|
152
|
|
|
$this->process->run()->shouldBeCalledTimes(1); |
|
|
|
|
153
|
|
|
$this->process->isSuccessful()->willReturn(false); |
|
|
|
|
154
|
|
|
|
155
|
|
|
$this->process->getErrorOutput()->willReturn(FailedException::class . PHP_EOL . 'TEST'); |
|
|
|
|
156
|
|
|
|
157
|
|
|
try { |
158
|
|
|
$this->executor->execute($this->execution->reveal()); |
|
|
|
|
159
|
|
|
|
160
|
|
|
$this->fail('No FailedException was thrown'); |
161
|
|
|
} catch (\Exception $exception) { |
162
|
|
|
$this->assertInstanceOf(FailedException::class, $exception); |
163
|
|
|
$this->assertInstanceOf(SeparateProcessException::class, $exception->getPrevious()); |
164
|
|
|
$this->assertEquals('TEST', $exception->getPrevious()->__toString()); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testExecuteRetryException() |
169
|
|
|
{ |
170
|
|
|
$this->execution->getHandlerClass()->willReturn('RetryTaskHandler'); |
|
|
|
|
171
|
|
|
$this->retryHandler->getMaximumAttempts()->willReturn(3); |
|
|
|
|
172
|
|
|
|
173
|
|
|
$attempts = 1; |
174
|
|
|
$this->execution->incrementAttempts()->will( |
|
|
|
|
175
|
|
|
function () use (&$attempts) { |
176
|
|
|
++$attempts; |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
); |
181
|
|
|
$this->execution->getAttempts()->will( |
|
|
|
|
182
|
|
|
function () use (&$attempts) { |
183
|
|
|
return $attempts; |
184
|
|
|
} |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
$this->executionRepository->save($this->execution->reveal())->shouldBeCalled(2); |
|
|
|
|
188
|
|
|
|
189
|
|
|
$this->process->run()->shouldBeCalledTimes(3); |
|
|
|
|
190
|
|
|
$this->process->isSuccessful()->willReturn(false); |
|
|
|
|
191
|
|
|
|
192
|
|
|
$this->process->getErrorOutput()->willReturn('TEST'); |
|
|
|
|
193
|
|
|
|
194
|
|
|
try { |
195
|
|
|
$this->executor->execute($this->execution->reveal()); |
|
|
|
|
196
|
|
|
|
197
|
|
|
$this->fail('No FailedException was thrown'); |
198
|
|
|
} catch (\Exception $exception) { |
199
|
|
|
$this->assertInstanceOf(FailedException::class, $exception); |
200
|
|
|
$this->assertInstanceOf(SeparateProcessException::class, $exception->getPrevious()); |
201
|
|
|
$this->assertEquals('TEST', $exception->getPrevious()->__toString()); |
202
|
|
|
$this->assertEquals(3, $attempts); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..