1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Task\TaskBundle\Unit\Command; |
4
|
|
|
|
5
|
|
|
use Cron\CronExpression; |
6
|
|
|
use Prophecy\Argument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Task\Execution\TaskExecutionInterface; |
10
|
|
|
use Task\Scheduler\TaskSchedulerInterface; |
11
|
|
|
use Task\Storage\TaskExecutionRepositoryInterface; |
12
|
|
|
use Task\TaskBundle\Builder\TaskBuilder; |
13
|
|
|
use Task\TaskBundle\Command\ScheduleSystemTasksCommand; |
14
|
|
|
use Task\TaskBundle\Entity\Task; |
15
|
|
|
use Task\TaskBundle\Entity\TaskRepository; |
16
|
|
|
use Task\TaskBundle\Tests\Functional\TestHandler; |
17
|
|
|
use Task\TaskStatus; |
18
|
|
|
|
19
|
|
|
class ScheduleSystemTasksCommandTest extends \PHPUnit_Framework_TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var TaskSchedulerInterface |
23
|
|
|
*/ |
24
|
|
|
private $scheduler; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var TaskRepository |
28
|
|
|
*/ |
29
|
|
|
private $taskRepository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var TaskExecutionRepositoryInterface |
33
|
|
|
*/ |
34
|
|
|
private $taskExecutionRepository; |
35
|
|
|
|
36
|
|
|
protected function setUp() |
37
|
|
|
{ |
38
|
|
|
$this->scheduler = $this->prophesize(TaskSchedulerInterface::class); |
|
|
|
|
39
|
|
|
$this->taskRepository = $this->prophesize(TaskRepository::class); |
|
|
|
|
40
|
|
|
$this->taskExecutionRepository = $this->prophesize(TaskExecutionRepositoryInterface::class); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $systemTasks |
45
|
|
|
* |
46
|
|
|
* @return ScheduleSystemTasksCommand |
47
|
|
|
*/ |
48
|
|
|
protected function createCommand(array $systemTasks) |
49
|
|
|
{ |
50
|
|
|
return new ScheduleSystemTasksCommand( |
51
|
|
|
'task:schedule:system-tasks', |
52
|
|
|
$systemTasks, |
53
|
|
|
$this->scheduler->reveal(), |
|
|
|
|
54
|
|
|
$this->taskRepository->reveal(), |
|
|
|
|
55
|
|
|
$this->taskExecutionRepository->reveal() |
|
|
|
|
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testExecute() |
60
|
|
|
{ |
61
|
|
|
$command = $this->createCommand( |
62
|
|
|
[ |
63
|
|
|
'testing' => [ |
64
|
|
|
'enabled' => true, |
65
|
|
|
'handler_class' => TestHandler::class, |
66
|
|
|
'workload' => 'test', |
67
|
|
|
'cron_expression' => '* * * * *', |
68
|
|
|
], |
69
|
|
|
] |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$task = $this->prophesize(Task::class); |
73
|
|
|
$task->getSystemKey()->willReturn('testing'); |
74
|
|
|
|
75
|
|
|
$taskBuilder = $this->prophesize(TaskBuilder::class); |
76
|
|
|
|
77
|
|
|
$this->taskRepository->findBySystemKey('testing')->willReturn(null); |
|
|
|
|
78
|
|
|
$this->taskRepository->findSystemTasks()->willReturn([$task->reveal()]); |
|
|
|
|
79
|
|
|
|
80
|
|
|
$this->scheduler->createTask(TestHandler::class, 'test')->shouldBeCalled()->willReturn($taskBuilder->reveal()); |
|
|
|
|
81
|
|
|
|
82
|
|
|
$taskBuilder->setSystemKey('testing')->shouldBeCalled(); |
83
|
|
|
$taskBuilder->cron('* * * * *')->shouldBeCalled(); |
84
|
|
|
$taskBuilder->schedule()->shouldBeCalled(); |
85
|
|
|
|
86
|
|
|
$command->run( |
87
|
|
|
$this->prophesize(InputInterface::class)->reveal(), |
88
|
|
|
$this->prophesize(OutputInterface::class)->reveal() |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testExecuteMultiple() |
93
|
|
|
{ |
94
|
|
|
$command = $this->createCommand( |
95
|
|
|
[ |
96
|
|
|
'testing-1' => [ |
97
|
|
|
'enabled' => true, |
98
|
|
|
'handler_class' => TestHandler::class, |
99
|
|
|
'workload' => 'test-1', |
100
|
|
|
'cron_expression' => '* * * * *', |
101
|
|
|
], |
102
|
|
|
'testing-2' => [ |
103
|
|
|
'enabled' => true, |
104
|
|
|
'handler_class' => TestHandler::class, |
105
|
|
|
'workload' => 'test-2', |
106
|
|
|
'cron_expression' => '* * * * *', |
107
|
|
|
], |
108
|
|
|
] |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$task1 = $this->prophesize(Task::class); |
112
|
|
|
$task1->getSystemKey()->willReturn('testing-1'); |
113
|
|
|
$task2 = $this->prophesize(Task::class); |
114
|
|
|
$task2->getSystemKey()->willReturn('testing-2'); |
115
|
|
|
|
116
|
|
|
$this->taskRepository->findBySystemKey('testing-1')->willReturn(null); |
|
|
|
|
117
|
|
|
$this->taskRepository->findBySystemKey('testing-2')->willReturn(null); |
|
|
|
|
118
|
|
|
$this->taskRepository->findSystemTasks()->willReturn([$task1->reveal(), $task2->reveal()]); |
|
|
|
|
119
|
|
|
|
120
|
|
|
$taskBuilder1 = $this->prophesize(TaskBuilder::class); |
121
|
|
|
$this->scheduler->createTask(TestHandler::class, 'test-1')->shouldBeCalled()->willReturn( |
|
|
|
|
122
|
|
|
$taskBuilder1->reveal() |
123
|
|
|
); |
124
|
|
|
$taskBuilder1->setSystemKey('testing-1')->shouldBeCalled(); |
125
|
|
|
$taskBuilder1->cron('* * * * *')->shouldBeCalled(); |
126
|
|
|
$taskBuilder1->schedule()->shouldBeCalled(); |
127
|
|
|
|
128
|
|
|
$taskBuilder2 = $this->prophesize(TaskBuilder::class); |
129
|
|
|
$this->scheduler->createTask(TestHandler::class, 'test-2')->shouldBeCalled()->willReturn( |
|
|
|
|
130
|
|
|
$taskBuilder2->reveal() |
131
|
|
|
); |
132
|
|
|
$taskBuilder2->setSystemKey('testing-2')->shouldBeCalled(); |
133
|
|
|
$taskBuilder2->cron('* * * * *')->shouldBeCalled(); |
134
|
|
|
$taskBuilder2->schedule()->shouldBeCalled(); |
135
|
|
|
|
136
|
|
|
$command->run( |
137
|
|
|
$this->prophesize(InputInterface::class)->reveal(), |
138
|
|
|
$this->prophesize(OutputInterface::class)->reveal() |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testExecuteDisable() |
143
|
|
|
{ |
144
|
|
|
$command = $this->createCommand( |
145
|
|
|
[ |
146
|
|
|
'testing' => [ |
147
|
|
|
'enabled' => false, |
148
|
|
|
'handler_class' => TestHandler::class, |
149
|
|
|
'workload' => 'test', |
150
|
|
|
'cron_expression' => '* * * * *', |
151
|
|
|
], |
152
|
|
|
] |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
$task = $this->prophesize(Task::class); |
156
|
|
|
$task->getInterval()->willReturn(CronExpression::factory('* * * * *')); |
157
|
|
|
$task->getFirstExecution()->willReturn(new \DateTime()); |
158
|
|
|
$task->getSystemKey()->willReturn('testing'); |
159
|
|
|
|
160
|
|
|
$task->setInterval( |
161
|
|
|
$task->reveal()->getInterval(), |
162
|
|
|
$task->reveal()->getFirstExecution(), |
163
|
|
|
Argument::that( |
164
|
|
|
function ($date) { |
165
|
|
|
return $date <= new \DateTime('+1 Minute'); |
166
|
|
|
} |
167
|
|
|
) |
168
|
|
|
)->shouldBeCalled(); |
169
|
|
|
|
170
|
|
|
$this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal()); |
|
|
|
|
171
|
|
|
$this->taskRepository->findSystemTasks()->willReturn([$task->reveal()]); |
|
|
|
|
172
|
|
|
|
173
|
|
|
$execution = $this->prophesize(TaskExecutionInterface::class); |
174
|
|
|
$execution->setStatus(TaskStatus::ABORTED); |
175
|
|
|
|
176
|
|
|
$this->taskExecutionRepository->findPending($task->reveal())->willReturn($execution->reveal()); |
|
|
|
|
177
|
|
|
$this->taskExecutionRepository->save($execution->reveal())->shouldBeCalled(); |
|
|
|
|
178
|
|
|
|
179
|
|
|
$command->run( |
180
|
|
|
$this->prophesize(InputInterface::class)->reveal(), |
181
|
|
|
$this->prophesize(OutputInterface::class)->reveal() |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function testExecuteUpdate() |
186
|
|
|
{ |
187
|
|
|
$command = $this->createCommand( |
188
|
|
|
[ |
189
|
|
|
'testing' => [ |
190
|
|
|
'enabled' => true, |
191
|
|
|
'handler_class' => TestHandler::class, |
192
|
|
|
'workload' => 'test', |
193
|
|
|
'cron_expression' => '* * * * *', |
194
|
|
|
], |
195
|
|
|
] |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
$task = $this->prophesize(Task::class); |
199
|
|
|
$task->getSystemKey()->willReturn('testing'); |
200
|
|
|
$task->getHandlerClass()->willReturn(TestHandler::class); |
201
|
|
|
$task->getWorkload()->willReturn('test'); |
202
|
|
|
$task->getInterval()->willReturn(CronExpression::factory('@daily')); |
203
|
|
|
$task->getFirstExecution()->willReturn(new \DateTime()); |
204
|
|
|
|
205
|
|
|
$task->setInterval(CronExpression::factory('* * * * *'), $task->reveal()->getFirstExecution())->shouldBeCalled( |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
$this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal()); |
|
|
|
|
209
|
|
|
$this->taskRepository->findSystemTasks()->willReturn([$task->reveal()]); |
|
|
|
|
210
|
|
|
|
211
|
|
|
$execution = $this->prophesize(TaskExecutionInterface::class); |
212
|
|
|
$execution->setStatus(TaskStatus::ABORTED); |
213
|
|
|
|
214
|
|
|
$this->taskExecutionRepository->findPending($task->reveal())->willReturn($execution->reveal()); |
|
|
|
|
215
|
|
|
$this->taskExecutionRepository->save($execution->reveal())->shouldBeCalled(); |
|
|
|
|
216
|
|
|
|
217
|
|
|
$this->scheduler->scheduleTasks()->shouldBeCalled(); |
218
|
|
|
|
219
|
|
|
$command->run( |
220
|
|
|
$this->prophesize(InputInterface::class)->reveal(), |
221
|
|
|
$this->prophesize(OutputInterface::class)->reveal() |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function testExecuteUpdateNotSupported() |
226
|
|
|
{ |
227
|
|
|
$command = $this->createCommand( |
228
|
|
|
[ |
229
|
|
|
'testing' => [ |
230
|
|
|
'enabled' => true, |
231
|
|
|
'handler_class' => TestHandler::class, |
232
|
|
|
'workload' => 'test', |
233
|
|
|
'cron_expression' => '* * * * *', |
234
|
|
|
], |
235
|
|
|
] |
236
|
|
|
); |
237
|
|
|
|
238
|
|
|
$task = $this->prophesize(Task::class); |
239
|
|
|
$task->getSystemKey()->willReturn('testing'); |
240
|
|
|
$task->getHandlerClass()->willReturn('not-existing'); |
241
|
|
|
$task->getWorkload()->willReturn('new-workload'); |
242
|
|
|
$task->getInterval()->willReturn(CronExpression::factory('@daily')); |
243
|
|
|
$task->getFirstExecution()->willReturn(new \DateTime()); |
244
|
|
|
|
245
|
|
|
$task->setInterval(Argument::cetera())->shouldNotBeCalled(); |
246
|
|
|
|
247
|
|
|
$this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal()); |
|
|
|
|
248
|
|
|
$this->taskRepository->findSystemTasks()->willReturn([$task->reveal()]); |
|
|
|
|
249
|
|
|
|
250
|
|
|
$this->taskExecutionRepository->save(Argument::cetera())->shouldNotBeCalled(); |
|
|
|
|
251
|
|
|
|
252
|
|
|
$this->scheduler->scheduleTasks()->shouldNotBeCalled(); |
253
|
|
|
|
254
|
|
|
$command->run( |
255
|
|
|
$this->prophesize(InputInterface::class)->reveal(), |
256
|
|
|
$this->prophesize(OutputInterface::class)->reveal() |
257
|
|
|
); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function testExecuteRemove() |
261
|
|
|
{ |
262
|
|
|
$command = $this->createCommand([]); |
263
|
|
|
|
264
|
|
|
$task = $this->prophesize(Task::class); |
265
|
|
|
$task->getInterval()->willReturn(CronExpression::factory('* * * * *')); |
266
|
|
|
$task->getFirstExecution()->willReturn(new \DateTime()); |
267
|
|
|
$task->getSystemKey()->willReturn('testing'); |
268
|
|
|
|
269
|
|
|
$task->setInterval( |
270
|
|
|
$task->reveal()->getInterval(), |
271
|
|
|
$task->reveal()->getFirstExecution(), |
272
|
|
|
Argument::that( |
273
|
|
|
function ($date) { |
274
|
|
|
return $date <= new \DateTime('+1 Minute'); |
275
|
|
|
} |
276
|
|
|
) |
277
|
|
|
)->shouldBeCalled(); |
278
|
|
|
|
279
|
|
|
$this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal()); |
|
|
|
|
280
|
|
|
$this->taskRepository->findSystemTasks()->willReturn([$task->reveal()]); |
|
|
|
|
281
|
|
|
|
282
|
|
|
$execution = $this->prophesize(TaskExecutionInterface::class); |
283
|
|
|
$execution->setStatus(TaskStatus::ABORTED); |
284
|
|
|
|
285
|
|
|
$this->taskExecutionRepository->findPending($task->reveal())->willReturn($execution->reveal()); |
|
|
|
|
286
|
|
|
$this->taskExecutionRepository->save($execution->reveal())->shouldBeCalled(); |
|
|
|
|
287
|
|
|
|
288
|
|
|
$command->run( |
289
|
|
|
$this->prophesize(InputInterface::class)->reveal(), |
290
|
|
|
$this->prophesize(OutputInterface::class)->reveal() |
291
|
|
|
); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
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..