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; |
13
|
|
|
|
14
|
|
|
use Cron\CronExpression; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
16
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
17
|
|
|
use Symfony\Component\Console\Command\Command; |
18
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
19
|
|
|
use Task\Execution\TaskExecutionInterface; |
20
|
|
|
use Task\Runner\TaskRunnerInterface; |
21
|
|
|
use Task\Scheduler\TaskSchedulerInterface; |
22
|
|
|
use Task\Storage\TaskExecutionRepositoryInterface; |
23
|
|
|
use Task\Storage\TaskRepositoryInterface; |
24
|
|
|
use Task\TaskInterface; |
25
|
|
|
use Task\TaskStatus; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Base class for testing commands. |
29
|
|
|
*/ |
30
|
|
|
abstract class BaseCommandTestCase extends KernelTestCase |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var Application |
34
|
|
|
*/ |
35
|
|
|
protected $application; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var TaskRepositoryInterface |
39
|
|
|
*/ |
40
|
|
|
protected $taskRepository; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var TaskExecutionRepositoryInterface |
44
|
|
|
*/ |
45
|
|
|
protected $taskExecutionRepository; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var TaskSchedulerInterface |
49
|
|
|
*/ |
50
|
|
|
protected $taskScheduler; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var TaskRunnerInterface |
54
|
|
|
*/ |
55
|
|
|
protected $taskRunner; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var Command |
59
|
|
|
*/ |
60
|
|
|
protected $command; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var CommandTester |
64
|
|
|
*/ |
65
|
|
|
protected $commandTester; |
66
|
|
|
|
67
|
|
|
public function setUp() |
68
|
|
|
{ |
69
|
|
|
self::bootKernel(); |
70
|
|
|
|
71
|
|
|
$this->taskRunner = self::$kernel->getContainer()->get('task.runner'); |
72
|
|
|
$this->taskScheduler = self::$kernel->getContainer()->get('task.scheduler'); |
73
|
|
|
$this->taskRepository = self::$kernel->getContainer()->get('task.storage.task'); |
74
|
|
|
$this->taskExecutionRepository = self::$kernel->getContainer()->get('task.storage.task_execution'); |
75
|
|
|
|
76
|
|
|
$command = $this->getCommand(); |
77
|
|
|
|
78
|
|
|
$this->application = new Application(self::$kernel); |
79
|
|
|
$this->application->add($command); |
80
|
|
|
|
81
|
|
|
$this->command = $this->application->find($command->getName()); |
82
|
|
|
$this->commandTester = new CommandTester($this->command); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Create new task. |
87
|
|
|
* |
88
|
|
|
* @param string $workload |
89
|
|
|
* @param CronExpression $cronExpression |
90
|
|
|
* @param string $handlerClass |
91
|
|
|
* |
92
|
|
|
* @return TaskInterface |
93
|
|
|
*/ |
94
|
|
|
protected function createTask($workload, CronExpression $cronExpression = null, $handlerClass = TestHandler::class) |
95
|
|
|
{ |
96
|
|
|
$task = $this->taskRepository->create($handlerClass, $workload); |
97
|
|
|
if ($cronExpression) { |
98
|
|
|
$task->setInterval($cronExpression, new \DateTime(), new \DateTime('+1 year')); |
99
|
|
|
} |
100
|
|
|
$this->taskRepository->persist($task); |
101
|
|
|
$this->taskRepository->flush(); |
102
|
|
|
|
103
|
|
|
return $task; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Create task-execution. |
108
|
|
|
* |
109
|
|
|
* @param TaskInterface $task |
110
|
|
|
* @param \DateTime $scheduleTime |
111
|
|
|
* @param string $status |
112
|
|
|
* |
113
|
|
|
* @return TaskExecutionInterface |
114
|
|
|
*/ |
115
|
|
|
protected function createTaskExecution(TaskInterface $task, \DateTime $scheduleTime, $status = TaskStatus::PLANNED) |
116
|
|
|
{ |
117
|
|
|
$execution = $this->taskExecutionRepository->create($task, $scheduleTime); |
118
|
|
|
$execution->setStatus($status); |
119
|
|
|
$this->taskExecutionRepository->persist($execution); |
120
|
|
|
$this->taskExecutionRepository->flush(); |
121
|
|
|
|
122
|
|
|
return $execution; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns command. |
127
|
|
|
* |
128
|
|
|
* @return Command |
129
|
|
|
*/ |
130
|
|
|
abstract protected function getCommand(); |
131
|
|
|
} |
132
|
|
|
|