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\Functional\Command; |
13
|
|
|
|
14
|
|
|
use Task\TaskBundle\Tests\Functional\BaseCommandTestCase; |
15
|
|
|
use Task\TaskBundle\Tests\Functional\TestHandler; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Tests for ScheduleTaskCommand. |
19
|
|
|
*/ |
20
|
|
|
class ScheduleTaskCommandTest extends BaseCommandTestCase |
21
|
|
|
{ |
22
|
|
|
public function testExecute() |
23
|
|
|
{ |
24
|
|
|
$this->commandTester->execute( |
25
|
|
|
[ |
26
|
|
|
'command' => $this->command->getName(), |
27
|
|
|
'handlerClass' => TestHandler::class, |
28
|
|
|
] |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
$tasks = $this->taskRepository->findAll(); |
32
|
|
|
$this->assertCount(1, $tasks); |
33
|
|
|
|
34
|
|
|
$this->assertEquals(TestHandler::class, $tasks[0]->getHandlerClass()); |
35
|
|
|
|
36
|
|
|
$executions = $this->taskExecutionRepository->findAll(); |
37
|
|
|
$this->assertCount(1, $executions); |
38
|
|
|
|
39
|
|
|
$this->assertEquals(TestHandler::class, $executions[0]->getHandlerClass()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testExecuteWithWorkload() |
43
|
|
|
{ |
44
|
|
|
$this->commandTester->execute( |
45
|
|
|
[ |
46
|
|
|
'command' => $this->command->getName(), |
47
|
|
|
'handlerClass' => TestHandler::class, |
48
|
|
|
'workload' => 'Test workload 1', |
49
|
|
|
] |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$tasks = $this->taskRepository->findAll(); |
53
|
|
|
$this->assertCount(1, $tasks); |
54
|
|
|
|
55
|
|
|
$this->assertEquals(TestHandler::class, $tasks[0]->getHandlerClass()); |
56
|
|
|
$this->assertEquals('Test workload 1', $tasks[0]->getWorkload()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testExecuteWithWorkloadAndInterval() |
60
|
|
|
{ |
61
|
|
|
$this->commandTester->execute( |
62
|
|
|
[ |
63
|
|
|
'command' => $this->command->getName(), |
64
|
|
|
'handlerClass' => TestHandler::class, |
65
|
|
|
'workload' => 'Test workload 1', |
66
|
|
|
'--cron-expression' => '0 * * * *', |
67
|
|
|
] |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$tasks = $this->taskRepository->findAll(); |
71
|
|
|
$this->assertCount(1, $tasks); |
72
|
|
|
|
73
|
|
|
$this->assertEquals(TestHandler::class, $tasks[0]->getHandlerClass()); |
74
|
|
|
$this->assertEquals('Test workload 1', $tasks[0]->getWorkload()); |
75
|
|
|
$this->assertEquals('0 * * * *', $tasks[0]->getInterval()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testExecuteWithWorkloadAndIntervalAndEndDate() |
79
|
|
|
{ |
80
|
|
|
$date = new \DateTime('+1 day'); |
81
|
|
|
$this->commandTester->execute( |
82
|
|
|
[ |
83
|
|
|
'command' => $this->command->getName(), |
84
|
|
|
'handlerClass' => TestHandler::class, |
85
|
|
|
'workload' => 'Test workload 1', |
86
|
|
|
'--cron-expression' => '0 * * * *', |
87
|
|
|
'--end-date' => $date->format(\DateTime::RFC3339), |
88
|
|
|
] |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$tasks = $this->taskRepository->findAll(); |
92
|
|
|
$this->assertCount(1, $tasks); |
93
|
|
|
|
94
|
|
|
$this->assertEquals(TestHandler::class, $tasks[0]->getHandlerClass()); |
95
|
|
|
$this->assertEquals('Test workload 1', $tasks[0]->getWorkload()); |
96
|
|
|
$this->assertEquals('0 * * * *', $tasks[0]->getInterval()); |
97
|
|
|
$this->assertEquals($date, $tasks[0]->getLastExecution()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testExecuteWithExecutionDate() |
101
|
|
|
{ |
102
|
|
|
$date = new \DateTime('+1 day'); |
103
|
|
|
$this->commandTester->execute( |
104
|
|
|
[ |
105
|
|
|
'command' => $this->command->getName(), |
106
|
|
|
'handlerClass' => TestHandler::class, |
107
|
|
|
'workload' => 'Test workload 1', |
108
|
|
|
'--execution-date' => '+1 day', |
109
|
|
|
] |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$tasks = $this->taskRepository->findAll(); |
113
|
|
|
$this->assertCount(1, $tasks); |
114
|
|
|
|
115
|
|
|
$this->assertEquals(TestHandler::class, $tasks[0]->getHandlerClass()); |
116
|
|
|
$this->assertEquals($date, $tasks[0]->getFirstExecution()); |
117
|
|
|
|
118
|
|
|
$executions = $this->taskExecutionRepository->findAll(); |
119
|
|
|
$this->assertCount(1, $executions); |
120
|
|
|
|
121
|
|
|
$this->assertEquals(TestHandler::class, $executions[0]->getHandlerClass()); |
122
|
|
|
$this->assertEquals($date, $executions[0]->getScheduleTime(), '', 2); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
protected function getCommand() |
129
|
|
|
{ |
130
|
|
|
return self::$kernel->getContainer()->get('task.command.schedule_task'); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|