1 | <?php |
||
2 | |||
3 | namespace Zenstruck\ScheduleBundle\Tests\Command; |
||
4 | |||
5 | use PHPUnit\Framework\TestCase; |
||
0 ignored issues
–
show
|
|||
6 | use Symfony\Component\Console\Output\OutputInterface; |
||
7 | use Symfony\Component\Console\Tester\CommandTester; |
||
8 | use Symfony\Component\EventDispatcher\EventDispatcher; |
||
9 | use Zenstruck\ScheduleBundle\Command\ScheduleRunCommand; |
||
10 | use Zenstruck\ScheduleBundle\Schedule; |
||
11 | use Zenstruck\ScheduleBundle\Schedule\ScheduleBuilder; |
||
12 | use Zenstruck\ScheduleBundle\Tests\Fixture\MockScheduleBuilder; |
||
13 | use Zenstruck\ScheduleBundle\Tests\Fixture\MockTask; |
||
14 | |||
15 | /** |
||
16 | * @author Kevin Bond <[email protected]> |
||
17 | */ |
||
18 | final class ScheduleRunCommandTest extends TestCase |
||
19 | { |
||
20 | /** |
||
21 | * @test |
||
22 | */ |
||
23 | public function no_tasks_defined(): void |
||
24 | { |
||
25 | $dispatcher = new EventDispatcher(); |
||
26 | $runner = (new MockScheduleBuilder())->getRunner($dispatcher); |
||
27 | $commandTester = new CommandTester(new ScheduleRunCommand($runner, $dispatcher)); |
||
28 | |||
29 | $exit = $commandTester->execute([]); |
||
30 | |||
31 | $this->assertSame(0, $exit); |
||
32 | $this->assertSame('', $commandTester->getDisplay()); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @test |
||
37 | */ |
||
38 | public function no_tasks_defined_debug() |
||
39 | { |
||
40 | $dispatcher = new EventDispatcher(); |
||
41 | $runner = (new MockScheduleBuilder())->getRunner($dispatcher); |
||
42 | $commandTester = new CommandTester(new ScheduleRunCommand($runner, $dispatcher)); |
||
43 | |||
44 | $exit = $commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_DEBUG]); |
||
45 | |||
46 | $this->assertSame(0, $exit); |
||
47 | $this->assertStringContainsString('No tasks due to run. (0 total tasks)', $commandTester->getDisplay()); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @test |
||
52 | */ |
||
53 | public function skipped_schedule() |
||
54 | { |
||
55 | $dispatcher = new EventDispatcher(); |
||
56 | $runner = (new MockScheduleBuilder()) |
||
57 | ->addTask(MockTask::success('my task 1')) |
||
58 | ->addBuilder(new class() implements ScheduleBuilder { |
||
59 | public function buildSchedule(Schedule $schedule): void |
||
60 | { |
||
61 | $schedule->skip('This schedule was skipped.', true); |
||
62 | } |
||
63 | }) |
||
64 | ->getRunner($dispatcher) |
||
65 | ; |
||
66 | $commandTester = new CommandTester(new ScheduleRunCommand($runner, $dispatcher)); |
||
67 | |||
68 | $exit = $commandTester->execute([]); |
||
69 | |||
70 | $this->assertSame(0, $exit); |
||
71 | $this->assertStringNotContainsString('my task 1', $commandTester->getDisplay()); |
||
72 | $this->assertStringContainsString('Running 1 due task. (1 total tasks)', $commandTester->getDisplay()); |
||
73 | $this->assertStringContainsString('This schedule was skipped.', $commandTester->getDisplay()); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @test |
||
78 | */ |
||
79 | public function skipped_task() |
||
80 | { |
||
81 | $dispatcher = new EventDispatcher(); |
||
82 | $runner = (new MockScheduleBuilder()) |
||
83 | ->addTask(MockTask::skipped('this task skipped', 'my task 1')) |
||
84 | ->addTask(MockTask::success('my task 2')) |
||
85 | ->getRunner($dispatcher) |
||
86 | ; |
||
87 | $commandTester = new CommandTester(new ScheduleRunCommand($runner, $dispatcher)); |
||
88 | |||
89 | $exit = $commandTester->execute([]); |
||
90 | |||
91 | $this->assertSame(0, $exit); |
||
92 | $this->assertStringContainsString('Running 2 due tasks. (2 total tasks)', $commandTester->getDisplay()); |
||
93 | $this->assertStringContainsString('1/2 tasks ran, 1 succeeded, 1 skipped.', $commandTester->getDisplay()); |
||
94 | $this->assertStringContainsString("Running MockTask: my task 1\n Skipped: this task skipped", $commandTester->getDisplay()); |
||
95 | $this->assertStringContainsString("Running MockTask: my task 2\n Success.", $commandTester->getDisplay()); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @test |
||
100 | */ |
||
101 | public function successful_task() |
||
102 | { |
||
103 | $dispatcher = new EventDispatcher(); |
||
104 | $runner = (new MockScheduleBuilder()) |
||
105 | ->addTask(MockTask::success('my task 1')) |
||
106 | ->addTask(MockTask::success('my task 2', 'task 2 output')) |
||
107 | ->getRunner($dispatcher) |
||
108 | ; |
||
109 | $commandTester = new CommandTester(new ScheduleRunCommand($runner, $dispatcher)); |
||
110 | |||
111 | $exit = $commandTester->execute([]); |
||
112 | |||
113 | $this->assertSame(0, $exit); |
||
114 | $this->assertStringContainsString('Running 2 due tasks. (2 total tasks)', $commandTester->getDisplay()); |
||
115 | $this->assertStringContainsString('2/2 tasks ran, 2 succeeded.', $commandTester->getDisplay()); |
||
116 | $this->assertStringContainsString("Running MockTask: my task 1\n Success", $commandTester->getDisplay()); |
||
117 | $this->assertStringContainsString("Running MockTask: my task 2\n Success.", $commandTester->getDisplay()); |
||
118 | $this->assertStringNotContainsString('task 2 output', $commandTester->getDisplay()); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @test |
||
123 | */ |
||
124 | public function successful_task_verbose() |
||
125 | { |
||
126 | $dispatcher = new EventDispatcher(); |
||
127 | $runner = (new MockScheduleBuilder()) |
||
128 | ->addTask(MockTask::success('my task 1')) |
||
129 | ->addTask(MockTask::success('my task 2', 'task 2 output')) |
||
130 | ->getRunner($dispatcher) |
||
131 | ; |
||
132 | $commandTester = new CommandTester(new ScheduleRunCommand($runner, $dispatcher)); |
||
133 | |||
134 | $exit = $commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]); |
||
135 | |||
136 | $this->assertSame(0, $exit); |
||
137 | $this->assertStringContainsString('Running 2 due tasks. (2 total tasks)', $commandTester->getDisplay()); |
||
138 | $this->assertStringContainsString('2/2 tasks ran, 2 succeeded.', $commandTester->getDisplay()); |
||
139 | $this->assertStringContainsString("Running MockTask: my task 1\n Success", $commandTester->getDisplay()); |
||
140 | $this->assertStringContainsString("Running MockTask: my task 2\n ---begin output---\ntask 2 output\n ---end output---\n Success.", $commandTester->getDisplay()); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @test |
||
145 | */ |
||
146 | public function failed_task() |
||
147 | { |
||
148 | $dispatcher = new EventDispatcher(); |
||
149 | $runner = (new MockScheduleBuilder()) |
||
150 | ->addTask(MockTask::failure('task 1 failure', 'my task 1', 'task 1 output')) |
||
151 | ->addTask(MockTask::success('my task 2')) |
||
152 | ->getRunner($dispatcher) |
||
153 | ; |
||
154 | $commandTester = new CommandTester(new ScheduleRunCommand($runner, $dispatcher)); |
||
155 | |||
156 | $exit = $commandTester->execute([]); |
||
157 | |||
158 | $this->assertSame(1, $exit); |
||
159 | $this->assertStringContainsString('Running 2 due tasks. (2 total tasks)', $commandTester->getDisplay()); |
||
160 | $this->assertStringContainsString('2/2 tasks ran, 1 succeeded, 1 failed.', $commandTester->getDisplay()); |
||
161 | $this->assertStringContainsString("Running MockTask: my task 1\n Failure: task 1 failure", $commandTester->getDisplay()); |
||
162 | $this->assertStringContainsString("Running MockTask: my task 2\n Success.", $commandTester->getDisplay()); |
||
163 | $this->assertStringNotContainsString('task 1 output', $commandTester->getDisplay()); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @test |
||
168 | */ |
||
169 | public function failed_task_verbose() |
||
170 | { |
||
171 | $dispatcher = new EventDispatcher(); |
||
172 | $runner = (new MockScheduleBuilder()) |
||
173 | ->addTask(MockTask::failure('task 1 failure', 'my task 1', 'task 1 output')) |
||
174 | ->addTask(MockTask::success('my task 2')) |
||
175 | ->getRunner($dispatcher) |
||
176 | ; |
||
177 | $commandTester = new CommandTester(new ScheduleRunCommand($runner, $dispatcher)); |
||
178 | |||
179 | $exit = $commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]); |
||
180 | |||
181 | $this->assertSame(1, $exit); |
||
182 | $this->assertStringContainsString('Running 2 due tasks. (2 total tasks)', $commandTester->getDisplay()); |
||
183 | $this->assertStringContainsString('2/2 tasks ran, 1 succeeded, 1 failed.', $commandTester->getDisplay()); |
||
184 | $this->assertStringContainsString("Running MockTask: my task 1\n ---begin output---\ntask 1 output\n ---end output---\n Failure: task 1 failure", $commandTester->getDisplay()); |
||
185 | $this->assertStringContainsString("Running MockTask: my task 2\n Success.", $commandTester->getDisplay()); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @test |
||
190 | */ |
||
191 | public function failed_task_via_exception() |
||
192 | { |
||
193 | $dispatcher = new EventDispatcher(); |
||
194 | $runner = (new MockScheduleBuilder()) |
||
195 | ->addTask(MockTask::exception(new \RuntimeException('task 1 exception message'), 'my task 1', 'task 1 output')) |
||
196 | ->addTask(MockTask::success('my task 2')) |
||
197 | ->getRunner($dispatcher) |
||
198 | ; |
||
199 | $commandTester = new CommandTester(new ScheduleRunCommand($runner, $dispatcher)); |
||
200 | |||
201 | $exit = $commandTester->execute([]); |
||
202 | |||
203 | $this->assertSame(1, $exit); |
||
204 | $this->assertStringContainsString('Running 2 due tasks. (2 total tasks)', $commandTester->getDisplay()); |
||
205 | $this->assertStringContainsString('2/2 tasks ran, 1 succeeded, 1 failed.', $commandTester->getDisplay()); |
||
206 | $this->assertStringContainsString("Running MockTask: my task 1\n Exception: RuntimeException: task 1 exception message", $commandTester->getDisplay()); |
||
207 | $this->assertStringContainsString("Running MockTask: my task 2\n Success.", $commandTester->getDisplay()); |
||
208 | $this->assertStringNotContainsString('task 1 output', $commandTester->getDisplay()); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @test |
||
213 | */ |
||
214 | public function failed_task_via_exception_verbose() |
||
215 | { |
||
216 | $dispatcher = new EventDispatcher(); |
||
217 | $runner = (new MockScheduleBuilder()) |
||
218 | ->addTask(MockTask::exception(new \RuntimeException('task 1 exception message'), 'my task 1', 'task 1 output')) |
||
219 | ->addTask(MockTask::success('my task 2')) |
||
220 | ->getRunner($dispatcher) |
||
221 | ; |
||
222 | $commandTester = new CommandTester(new ScheduleRunCommand($runner, $dispatcher)); |
||
223 | |||
224 | $exit = $commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]); |
||
225 | |||
226 | $this->assertSame(1, $exit); |
||
227 | $this->assertStringContainsString('Running 2 due tasks. (2 total tasks)', $commandTester->getDisplay()); |
||
228 | $this->assertStringContainsString('2/2 tasks ran, 1 succeeded, 1 failed.', $commandTester->getDisplay()); |
||
229 | $this->assertStringContainsString("Running MockTask: my task 1\n ---begin output---\ntask 1 output\n ---end output---\n Exception: RuntimeException: task 1 exception message", $commandTester->getDisplay()); |
||
230 | $this->assertStringContainsString("Running MockTask: my task 2\n Success.", $commandTester->getDisplay()); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @test |
||
235 | */ |
||
236 | public function can_force_run_tasks() |
||
237 | { |
||
238 | $dispatcher = new EventDispatcher(); |
||
239 | $runner = (new MockScheduleBuilder()) |
||
240 | ->addTask(MockTask::success('my task 1')) |
||
241 | ->addTask($task2 = MockTask::success('my task 2')->cron('@yearly')) |
||
242 | ->addTask($task3 = MockTask::success('my task 3')->cron('@yearly')) |
||
243 | ->getRunner($dispatcher) |
||
244 | ; |
||
245 | $commandTester = new CommandTester(new ScheduleRunCommand($runner, $dispatcher)); |
||
246 | |||
247 | $exit = $commandTester->execute([ |
||
248 | 'id' => [$task2->getId(), $task3->getId()], |
||
249 | ]); |
||
250 | |||
251 | $this->assertSame(0, $exit); |
||
252 | $this->assertStringContainsString('Force Running 2 tasks. (3 total tasks)', $commandTester->getDisplay()); |
||
253 | $this->assertStringContainsString('2/2 tasks ran, 2 succeeded.', $commandTester->getDisplay()); |
||
254 | $this->assertStringContainsString("Force Running MockTask: my task 2\n Success", $commandTester->getDisplay()); |
||
255 | $this->assertStringContainsString("Force Running MockTask: my task 3\n Success.", $commandTester->getDisplay()); |
||
256 | $this->assertStringNotContainsString("MockTask: my task 1\n Success.", $commandTester->getDisplay()); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @test |
||
261 | */ |
||
262 | public function force_running_an_invalid_task_throws_exception() |
||
263 | { |
||
264 | $dispatcher = new EventDispatcher(); |
||
265 | $runner = (new MockScheduleBuilder()) |
||
266 | ->addTask(MockTask::success('my task 1')) |
||
267 | ->getRunner($dispatcher) |
||
268 | ; |
||
269 | $commandTester = new CommandTester(new ScheduleRunCommand($runner, $dispatcher)); |
||
270 | |||
271 | $this->expectException(\InvalidArgumentException::class); |
||
272 | $this->expectExceptionMessage('Task with ID "invalid-id" not found.'); |
||
273 | |||
274 | $commandTester->execute(['id' => ['invalid-id']]); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @test |
||
279 | */ |
||
280 | public function force_running_a_task_with_a_duplicate_id_throws_exception() |
||
281 | { |
||
282 | $dispatcher = new EventDispatcher(); |
||
283 | $runner = (new MockScheduleBuilder()) |
||
284 | ->addTask($task = MockTask::success('my task')) |
||
285 | ->addTask(MockTask::success('my task')) |
||
286 | ->getRunner($dispatcher) |
||
287 | ; |
||
288 | $commandTester = new CommandTester(new ScheduleRunCommand($runner, $dispatcher)); |
||
289 | |||
290 | $this->expectException(\RuntimeException::class); |
||
291 | $this->expectExceptionMessage("Task ID \"{$task->getId()}\" is ambiguous, there are 2 tasks this id."); |
||
292 | |||
293 | $commandTester->execute(['id' => [$task->getId()]]); |
||
294 | } |
||
295 | } |
||
296 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths