|
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 Doctrine\Common\DataFixtures\Executor\ORMExecutor; |
|
16
|
|
|
use Doctrine\Common\DataFixtures\ProxyReferenceRepository; |
|
17
|
|
|
use Doctrine\Common\DataFixtures\Purger\ORMPurger; |
|
18
|
|
|
use Doctrine\DBAL\Driver\PDOMySql\Driver; |
|
19
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
20
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
|
21
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
|
22
|
|
|
use Symfony\Component\Console\Command\Command; |
|
23
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
24
|
|
|
use Task\Execution\TaskExecutionInterface; |
|
25
|
|
|
use Task\Runner\TaskRunnerInterface; |
|
26
|
|
|
use Task\Scheduler\TaskSchedulerInterface; |
|
27
|
|
|
use Task\Storage\TaskExecutionRepositoryInterface; |
|
28
|
|
|
use Task\Storage\TaskRepositoryInterface; |
|
29
|
|
|
use Task\TaskInterface; |
|
30
|
|
|
use Task\TaskStatus; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Base class for testing commands. |
|
34
|
|
|
*/ |
|
35
|
|
|
abstract class BaseCommandTestCase extends KernelTestCase |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var Application |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $application; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var TaskRepositoryInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $taskRepository; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var TaskExecutionRepositoryInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $taskExecutionRepository; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var TaskSchedulerInterface |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $taskScheduler; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var TaskRunnerInterface |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $taskRunner; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var Command |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $command; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var CommandTester |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $commandTester; |
|
71
|
|
|
|
|
72
|
|
|
public function setUp() |
|
73
|
|
|
{ |
|
74
|
|
|
self::bootKernel(); |
|
75
|
|
|
|
|
76
|
|
|
$this->taskRunner = self::$kernel->getContainer()->get('task.runner'); |
|
77
|
|
|
$this->taskScheduler = self::$kernel->getContainer()->get('task.scheduler'); |
|
78
|
|
|
$this->taskRepository = self::$kernel->getContainer()->get('task.storage.task'); |
|
79
|
|
|
$this->taskExecutionRepository = self::$kernel->getContainer()->get('task.storage.task_execution'); |
|
80
|
|
|
|
|
81
|
|
|
$command = $this->getCommand(); |
|
82
|
|
|
|
|
83
|
|
|
$this->application = new Application(self::$kernel); |
|
84
|
|
|
$this->application->add($command); |
|
85
|
|
|
|
|
86
|
|
|
$this->command = $this->application->find($command->getName()); |
|
87
|
|
|
$this->commandTester = new CommandTester($this->command); |
|
88
|
|
|
|
|
89
|
|
|
$this->purgeDatabase(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Create new task. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $workload |
|
96
|
|
|
* @param CronExpression $cronExpression |
|
97
|
|
|
* @param string $handlerClass |
|
98
|
|
|
* |
|
99
|
|
|
* @return TaskInterface |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function createTask($workload, CronExpression $cronExpression = null, $handlerClass = TestHandler::class) |
|
102
|
|
|
{ |
|
103
|
|
|
$task = $this->taskRepository->create($handlerClass, $workload); |
|
104
|
|
|
if ($cronExpression) { |
|
105
|
|
|
$task->setInterval($cronExpression, new \DateTime(), new \DateTime('+1 year')); |
|
106
|
|
|
} |
|
107
|
|
|
$this->taskRepository->persist($task); |
|
108
|
|
|
$this->taskRepository->flush(); |
|
109
|
|
|
|
|
110
|
|
|
return $task; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Create task-execution. |
|
115
|
|
|
* |
|
116
|
|
|
* @param TaskInterface $task |
|
117
|
|
|
* @param \DateTime $scheduleTime |
|
118
|
|
|
* @param string $status |
|
119
|
|
|
* |
|
120
|
|
|
* @return TaskExecutionInterface |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function createTaskExecution(TaskInterface $task, \DateTime $scheduleTime, $status = TaskStatus::PLANNED) |
|
123
|
|
|
{ |
|
124
|
|
|
$execution = $this->taskExecutionRepository->create($task, $scheduleTime); |
|
125
|
|
|
$execution->setStatus($status); |
|
126
|
|
|
$this->taskExecutionRepository->persist($execution); |
|
127
|
|
|
$this->taskExecutionRepository->flush(); |
|
128
|
|
|
|
|
129
|
|
|
return $execution; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Purge the Doctrine ORM database. |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function purgeDatabase() |
|
136
|
|
|
{ |
|
137
|
|
|
$manager = $this->getEntityManager(); |
|
138
|
|
|
$connection = $manager->getConnection(); |
|
139
|
|
|
|
|
140
|
|
|
if ($connection->getDriver() instanceof Driver) { |
|
141
|
|
|
$connection->executeUpdate('SET foreign_key_checks = 0;'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$purger = new ORMPurger(); |
|
145
|
|
|
$executor = new ORMExecutor($manager, $purger); |
|
146
|
|
|
$referenceRepository = new ProxyReferenceRepository($manager); |
|
147
|
|
|
$executor->setReferenceRepository($referenceRepository); |
|
148
|
|
|
$executor->purge(); |
|
149
|
|
|
|
|
150
|
|
|
if ($connection->getDriver() instanceof Driver) { |
|
151
|
|
|
$connection->executeUpdate('SET foreign_key_checks = 1;'); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Returns entity-manager. |
|
157
|
|
|
* |
|
158
|
|
|
* @return EntityManagerInterface |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function getEntityManager() |
|
161
|
|
|
{ |
|
162
|
|
|
return self::$kernel->getContainer()->get('doctrine')->getManager(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Returns command. |
|
167
|
|
|
* |
|
168
|
|
|
* @return Command |
|
169
|
|
|
*/ |
|
170
|
|
|
abstract protected function getCommand(); |
|
171
|
|
|
} |
|
172
|
|
|
|