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\Functional\Command; |
13
|
|
|
|
14
|
|
|
use Cron\CronExpression; |
15
|
|
|
use Task\Execution\TaskExecutionInterface; |
16
|
|
|
use Task\TaskBundle\Command\RunCommand; |
17
|
|
|
use Task\TaskBundle\Tests\Functional\BaseCommandTestCase; |
18
|
|
|
use Task\TaskBundle\Tests\Functional\FailTestHandler; |
19
|
|
|
use Task\TaskBundle\Tests\Functional\TestHandler; |
20
|
|
|
use Task\TaskStatus; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Tests for RunCommand. |
24
|
|
|
*/ |
25
|
|
|
class RunCommandTest extends BaseCommandTestCase |
26
|
|
|
{ |
27
|
|
|
public function testExecute() |
28
|
|
|
{ |
29
|
|
|
$singleTask = $this->createTask('Test workload 1'); |
30
|
|
|
$laterTask = $this->createTask('Test workload 2'); |
31
|
|
|
$intervalTask = $this->createTask('Test workload 3', CronExpression::factory('@daily')); |
32
|
|
|
|
33
|
|
|
/** @var TaskExecutionInterface[] $executions */ |
34
|
|
|
$executions = [ |
35
|
|
|
$this->createTaskExecution($singleTask, new \DateTime('-1 hour')), |
36
|
|
|
$this->createTaskExecution($laterTask, new \DateTime('+1 hour')), |
37
|
|
|
$this->createTaskExecution($intervalTask, new \DateTime('-2 hour')), |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
sleep(1); |
41
|
|
|
|
42
|
|
|
$this->commandTester->execute( |
43
|
|
|
[ |
44
|
|
|
'command' => $this->command->getName(), |
45
|
|
|
] |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
$this->assertEquals(TaskStatus::COMPLETE, $executions[0]->getStatus()); |
49
|
|
|
$this->assertEquals(strrev('Test workload 1'), $executions[0]->getResult()); |
50
|
|
|
$this->assertGreaterThan(0, $executions[0]->getDuration()); |
51
|
|
|
$this->assertGreaterThanOrEqual($executions[0]->getStartTime(), $executions[0]->getEndTime()); |
52
|
|
|
|
53
|
|
|
$this->assertEquals(TaskStatus::PLANNED, $executions[1]->getStatus()); |
54
|
|
|
$this->assertNull($executions[1]->getResult()); |
55
|
|
|
$this->assertNull($executions[1]->getDuration()); |
56
|
|
|
$this->assertNull($executions[1]->getStartTime()); |
57
|
|
|
$this->assertNull($executions[1]->getEndTime()); |
58
|
|
|
|
59
|
|
|
$this->assertEquals(TaskStatus::COMPLETE, $executions[2]->getStatus()); |
60
|
|
|
$this->assertEquals(strrev('Test workload 3'), $executions[2]->getResult()); |
61
|
|
|
$this->assertGreaterThan(0, $executions[2]->getDuration()); |
62
|
|
|
$this->assertGreaterThanOrEqual($executions[2]->getStartTime(), $executions[2]->getEndTime()); |
63
|
|
|
|
64
|
|
|
$result = $this->taskExecutionRepository->findAll(2, 3); |
65
|
|
|
$this->assertCount(1, $result); |
66
|
|
|
|
67
|
|
|
$this->assertEquals($intervalTask, $result[0]->getTask()); |
68
|
|
|
$this->assertEquals(TaskStatus::PLANNED, $result[0]->getStatus()); |
69
|
|
|
$this->assertEquals(TestHandler::class, $result[0]->getHandlerClass()); |
70
|
|
|
$this->assertEquals('Test workload 3', $result[0]->getWorkload()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testExecuteWithFail() |
74
|
|
|
{ |
75
|
|
|
$singleTask = $this->createTask('Test workload 1', null, FailTestHandler::class); |
76
|
|
|
$laterTask = $this->createTask('Test workload 2', null, FailTestHandler::class); |
77
|
|
|
$intervalTask = $this->createTask('Test workload 3', CronExpression::factory('@daily'), FailTestHandler::class); |
78
|
|
|
|
79
|
|
|
/** @var TaskExecutionInterface[] $executions */ |
80
|
|
|
$executions = [ |
81
|
|
|
$this->createTaskExecution($singleTask, new \DateTime('-1 hour')), |
82
|
|
|
$this->createTaskExecution($laterTask, new \DateTime('+1 hour')), |
83
|
|
|
$this->createTaskExecution($intervalTask, new \DateTime('-2 hour')), |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
sleep(1); |
87
|
|
|
|
88
|
|
|
$this->commandTester->execute( |
89
|
|
|
[ |
90
|
|
|
'command' => $this->command->getName(), |
91
|
|
|
] |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$this->assertEquals(TaskStatus::FAILED, $executions[0]->getStatus()); |
95
|
|
|
$this->assertNull($executions[0]->getResult()); |
96
|
|
|
$this->assertGreaterThan(0, $executions[0]->getDuration()); |
97
|
|
|
$this->assertGreaterThanOrEqual($executions[0]->getStartTime(), $executions[0]->getEndTime()); |
98
|
|
|
|
99
|
|
|
$this->assertEquals(TaskStatus::PLANNED, $executions[1]->getStatus()); |
100
|
|
|
$this->assertNull($executions[1]->getResult()); |
101
|
|
|
$this->assertNull($executions[1]->getDuration()); |
102
|
|
|
$this->assertNull($executions[1]->getStartTime()); |
103
|
|
|
$this->assertNull($executions[1]->getEndTime()); |
104
|
|
|
|
105
|
|
|
$this->assertEquals(TaskStatus::FAILED, $executions[2]->getStatus()); |
106
|
|
|
$this->assertNull($executions[2]->getResult()); |
107
|
|
|
$this->assertGreaterThan(0, $executions[2]->getDuration()); |
108
|
|
|
$this->assertGreaterThanOrEqual($executions[2]->getStartTime(), $executions[2]->getEndTime()); |
109
|
|
|
|
110
|
|
|
$result = $this->taskExecutionRepository->findAll(2, 3); |
111
|
|
|
$this->assertCount(1, $result); |
112
|
|
|
|
113
|
|
|
$this->assertEquals($intervalTask, $result[0]->getTask()); |
114
|
|
|
$this->assertEquals(TaskStatus::PLANNED, $result[0]->getStatus()); |
115
|
|
|
$this->assertEquals(FailTestHandler::class, $result[0]->getHandlerClass()); |
116
|
|
|
$this->assertEquals('Test workload 3', $result[0]->getWorkload()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
protected function getCommand() |
123
|
|
|
{ |
124
|
|
|
return new RunCommand('task:run', $this->taskRunner, $this->taskScheduler); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|