|
2 | 2 |
|
3 | 3 | namespace Task\TaskBundle\Unit\Command;
|
4 | 4 |
|
| 5 | +use Cron\CronExpression; |
| 6 | +use Prophecy\Argument; |
5 | 7 | use Symfony\Component\Console\Input\InputInterface;
|
6 | 8 | use Symfony\Component\Console\Output\OutputInterface;
|
| 9 | +use Task\Execution\TaskExecutionInterface; |
7 | 10 | use Task\Scheduler\TaskSchedulerInterface;
|
8 | 11 | use Task\Storage\TaskExecutionRepositoryInterface;
|
9 | 12 | use Task\TaskBundle\Builder\TaskBuilder;
|
10 | 13 | use Task\TaskBundle\Command\ScheduleSystemTasksCommand;
|
11 | 14 | use Task\TaskBundle\Entity\TaskRepository;
|
12 | 15 | use Task\TaskBundle\Tests\Functional\TestHandler;
|
| 16 | +use Task\TaskInterface; |
| 17 | +use Task\TaskStatus; |
13 | 18 |
|
14 | 19 | class ScheduleSystemTasksCommandTest extends \PHPUnit_Framework_TestCase
|
15 | 20 | {
|
@@ -119,5 +124,115 @@ public function testExecuteMultiple()
|
119 | 124 | );
|
120 | 125 | }
|
121 | 126 |
|
122 |
| - // TODO Tests for update and disable. |
| 127 | + public function testExecuteDisable() |
| 128 | + { |
| 129 | + $command = $this->createCommand( |
| 130 | + [ |
| 131 | + 'testing' => [ |
| 132 | + 'enabled' => false, |
| 133 | + 'handler_class' => TestHandler::class, |
| 134 | + 'workload' => 'test', |
| 135 | + 'cron_expression' => '* * * * *', |
| 136 | + ], |
| 137 | + ] |
| 138 | + ); |
| 139 | + |
| 140 | + $task = $this->prophesize(TaskInterface::class); |
| 141 | + $task->getInterval()->willReturn(CronExpression::factory('* * * * *')); |
| 142 | + $task->getFirstExecution()->willReturn(new \DateTime()); |
| 143 | + |
| 144 | + $task->setInterval( |
| 145 | + $task->reveal()->getInterval(), |
| 146 | + $task->reveal()->getFirstExecution(), |
| 147 | + Argument::that( |
| 148 | + function ($date) { |
| 149 | + return $date <= new \DateTime('+1 Minute'); |
| 150 | + } |
| 151 | + ) |
| 152 | + )->shouldBeCalled(); |
| 153 | + |
| 154 | + $this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal()); |
| 155 | + |
| 156 | + $execution = $this->prophesize(TaskExecutionInterface::class); |
| 157 | + $execution->setStatus(TaskStatus::ABORTED); |
| 158 | + |
| 159 | + $this->taskExecutionRepository->findPending($task->reveal())->willReturn($execution->reveal()); |
| 160 | + $this->taskExecutionRepository->save($execution->reveal())->shouldBeCalled(); |
| 161 | + |
| 162 | + $command->run( |
| 163 | + $this->prophesize(InputInterface::class)->reveal(), |
| 164 | + $this->prophesize(OutputInterface::class)->reveal() |
| 165 | + ); |
| 166 | + } |
| 167 | + |
| 168 | + public function testExecuteUpdate() |
| 169 | + { |
| 170 | + $command = $this->createCommand( |
| 171 | + [ |
| 172 | + 'testing' => [ |
| 173 | + 'enabled' => true, |
| 174 | + 'handler_class' => TestHandler::class, |
| 175 | + 'workload' => 'test', |
| 176 | + 'cron_expression' => '* * * * *', |
| 177 | + ], |
| 178 | + ] |
| 179 | + ); |
| 180 | + |
| 181 | + $task = $this->prophesize(TaskInterface::class); |
| 182 | + $task->getHandlerClass()->willReturn(TestHandler::class); |
| 183 | + $task->getWorkload()->willReturn('test'); |
| 184 | + $task->getInterval()->willReturn(CronExpression::factory('@daily')); |
| 185 | + $task->getFirstExecution()->willReturn(new \DateTime()); |
| 186 | + |
| 187 | + $task->setInterval(CronExpression::factory('* * * * *'), $task->reveal()->getFirstExecution()) |
| 188 | + ->shouldBeCalled(); |
| 189 | + |
| 190 | + $this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal()); |
| 191 | + |
| 192 | + $execution = $this->prophesize(TaskExecutionInterface::class); |
| 193 | + $execution->setStatus(TaskStatus::ABORTED); |
| 194 | + |
| 195 | + $this->taskExecutionRepository->findPending($task->reveal())->willReturn($execution->reveal()); |
| 196 | + $this->taskExecutionRepository->save($execution->reveal())->shouldBeCalled(); |
| 197 | + |
| 198 | + $this->scheduler->scheduleTasks()->shouldBeCalled(); |
| 199 | + |
| 200 | + $command->run( |
| 201 | + $this->prophesize(InputInterface::class)->reveal(), |
| 202 | + $this->prophesize(OutputInterface::class)->reveal() |
| 203 | + ); |
| 204 | + } |
| 205 | + |
| 206 | + public function testExecuteUpdateNotSupported() |
| 207 | + { |
| 208 | + $command = $this->createCommand( |
| 209 | + [ |
| 210 | + 'testing' => [ |
| 211 | + 'enabled' => true, |
| 212 | + 'handler_class' => TestHandler::class, |
| 213 | + 'workload' => 'test', |
| 214 | + 'cron_expression' => '* * * * *', |
| 215 | + ], |
| 216 | + ] |
| 217 | + ); |
| 218 | + |
| 219 | + $task = $this->prophesize(TaskInterface::class); |
| 220 | + $task->getHandlerClass()->willReturn('not-existing'); |
| 221 | + $task->getWorkload()->willReturn('new-workload'); |
| 222 | + $task->getInterval()->willReturn(CronExpression::factory('@daily')); |
| 223 | + $task->getFirstExecution()->willReturn(new \DateTime()); |
| 224 | + |
| 225 | + $task->setInterval(Argument::cetera())->shouldNotBeCalled(); |
| 226 | + |
| 227 | + $this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal()); |
| 228 | + |
| 229 | + $this->taskExecutionRepository->save(Argument::cetera())->shouldNotBeCalled(); |
| 230 | + |
| 231 | + $this->scheduler->scheduleTasks()->shouldNotBeCalled(); |
| 232 | + |
| 233 | + $command->run( |
| 234 | + $this->prophesize(InputInterface::class)->reveal(), |
| 235 | + $this->prophesize(OutputInterface::class)->reveal() |
| 236 | + ); |
| 237 | + } |
123 | 238 | }
|
0 commit comments