1 | <?php |
||
2 | |||
3 | namespace Zenstruck\ScheduleBundle\Tests; |
||
4 | |||
5 | use PHPUnit\Framework\TestCase; |
||
0 ignored issues
–
show
|
|||
6 | use Symfony\Component\Mime\Email; |
||
7 | use Symfony\Component\Process\Process; |
||
8 | use Zenstruck\ScheduleBundle\Schedule; |
||
9 | use Zenstruck\ScheduleBundle\Schedule\Extension\SingleServerExtension; |
||
10 | use Zenstruck\ScheduleBundle\Schedule\Task; |
||
11 | use Zenstruck\ScheduleBundle\Schedule\Task\CallbackTask; |
||
12 | use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask; |
||
13 | use Zenstruck\ScheduleBundle\Tests\Fixture\MockTask; |
||
14 | |||
15 | /** |
||
16 | * @author Kevin Bond <[email protected]> |
||
17 | */ |
||
18 | class ScheduleTest extends TestCase |
||
19 | { |
||
20 | /** |
||
21 | * @test |
||
22 | */ |
||
23 | public function can_add_tasks() |
||
24 | { |
||
25 | $schedule = new Schedule(); |
||
26 | |||
27 | $schedule->add(new CallbackTask(function() {}))->description('task1'); |
||
28 | $schedule->addCallback(function() {})->description('task2'); |
||
29 | $schedule->addProcess('php -v')->description('task3'); |
||
30 | $schedule->addProcess(new Process(['php -v']))->description('task4'); |
||
31 | $schedule->addCommand('my:command')->description('task5'); |
||
32 | $schedule->addPing('https://example.com')->description('task6'); |
||
33 | $schedule->addMessage(new \stdClass())->description('task7'); |
||
34 | |||
35 | $this->assertCount(7, $schedule->all()); |
||
36 | $this->assertSame(['task1', 'task2', 'task3', 'task4', 'task5', 'task6', 'task7'], \array_map( |
||
37 | function(Task $task) { |
||
38 | return $task->getDescription(); |
||
39 | }, |
||
40 | $schedule->all() |
||
41 | )); |
||
42 | |||
43 | $this->assertCount(7, $schedule->all(), 'Caches the tasks'); |
||
44 | |||
45 | $schedule->addCommand('another:command'); |
||
46 | |||
47 | $this->assertCount(8, $schedule->all(), 'Resets the task cache on add'); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @test |
||
52 | */ |
||
53 | public function can_add_compound_tasks() |
||
54 | { |
||
55 | $schedule = new Schedule(); |
||
56 | |||
57 | $schedule->addCommand('my:command')->description('task1')->tuesdays(); |
||
58 | $schedule->addCompound() |
||
59 | ->addCommand('another:command', [], 'task2') |
||
60 | ->addCallback(function() {}, 'task3') |
||
61 | ->addProcess('php -v', 'task4') |
||
62 | ->addProcess(new Process(['php -v']), 'task5') |
||
63 | ->add((new CommandTask('yet:another:command')) |
||
64 | ->description('task6') |
||
65 | ->sundays() |
||
66 | ->timezone('America/Los_Angeles') |
||
67 | ) |
||
68 | ->addPing('https://example.com', 'GET', [], 'task7') |
||
69 | ->addMessage(new \stdClass(), [], 'task8') |
||
70 | ->timezone('UTC') |
||
71 | ->mondays() |
||
72 | ->onSingleServer() |
||
73 | ; |
||
74 | |||
75 | $this->assertCount(8, $schedule->all()); |
||
76 | $this->assertSame('task1', $schedule->all()[0]->getDescription()); |
||
77 | $this->assertSame('* * * * 2', (string) $schedule->all()[0]->getExpression()); |
||
78 | $this->assertNull($schedule->all()[0]->getTimezone()); |
||
79 | $this->assertCount(0, $schedule->all()[0]->getExtensions()); |
||
80 | $this->assertSame('task2', $schedule->all()[1]->getDescription()); |
||
81 | $this->assertSame('* * * * 1', (string) $schedule->all()[1]->getExpression()); |
||
82 | $this->assertSame('UTC', $schedule->all()[1]->getTimezone()->getName()); |
||
83 | $this->assertCount(1, $schedule->all()[1]->getExtensions()); |
||
84 | $this->assertSame('task3', $schedule->all()[2]->getDescription()); |
||
85 | $this->assertSame('* * * * 1', (string) $schedule->all()[2]->getExpression()); |
||
86 | $this->assertSame('UTC', $schedule->all()[2]->getTimezone()->getName()); |
||
87 | $this->assertCount(1, $schedule->all()[2]->getExtensions()); |
||
88 | $this->assertSame('task4', $schedule->all()[3]->getDescription()); |
||
89 | $this->assertSame('* * * * 1', (string) $schedule->all()[3]->getExpression()); |
||
90 | $this->assertSame('UTC', $schedule->all()[3]->getTimezone()->getName()); |
||
91 | $this->assertCount(1, $schedule->all()[3]->getExtensions()); |
||
92 | $this->assertSame('task5', $schedule->all()[4]->getDescription()); |
||
93 | $this->assertSame('* * * * 1', (string) $schedule->all()[4]->getExpression()); |
||
94 | $this->assertSame('UTC', $schedule->all()[4]->getTimezone()->getName()); |
||
95 | $this->assertCount(1, $schedule->all()[4]->getExtensions()); |
||
96 | $this->assertSame('task6', $schedule->all()[5]->getDescription()); |
||
97 | $this->assertSame('* * * * 1', (string) $schedule->all()[5]->getExpression()); |
||
98 | $this->assertSame('UTC', $schedule->all()[5]->getTimezone()->getName()); |
||
99 | $this->assertCount(1, $schedule->all()[5]->getExtensions()); |
||
100 | $this->assertSame('task7', $schedule->all()[6]->getDescription()); |
||
101 | $this->assertSame('* * * * 1', (string) $schedule->all()[6]->getExpression()); |
||
102 | $this->assertSame('UTC', $schedule->all()[6]->getTimezone()->getName()); |
||
103 | $this->assertCount(1, $schedule->all()[6]->getExtensions()); |
||
104 | $this->assertSame('task8', $schedule->all()[7]->getDescription()); |
||
105 | $this->assertSame('* * * * 1', (string) $schedule->all()[7]->getExpression()); |
||
106 | $this->assertSame('UTC', $schedule->all()[7]->getTimezone()->getName()); |
||
107 | $this->assertCount(1, $schedule->all()[7]->getExtensions()); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @test |
||
112 | */ |
||
113 | public function can_get_due_tasks() |
||
114 | { |
||
115 | $schedule = new Schedule(); |
||
116 | |||
117 | $schedule->addCallback(function() {})->description('task1'); |
||
118 | $notDueTask = $schedule->addProcess('php -v')->description('task2')->sundays(); |
||
119 | |||
120 | if ('Sun' === \date('D')) { |
||
121 | $notDueTask->mondays(); |
||
122 | } |
||
123 | |||
124 | $this->assertCount(2, $schedule->all()); |
||
125 | $this->assertCount(1, $schedule->due(new \DateTime())); |
||
126 | $this->assertCount(1, $schedule->due(new \DateTime()), 'Due tasks are cached'); |
||
127 | $this->assertSame('task1', $schedule->due(new \DateTime())[0]->getDescription()); |
||
128 | |||
129 | $schedule->addCommand('my:command')->description('task3'); |
||
130 | |||
131 | $this->assertCount(2, $schedule->due(new \DateTime()), 'Resets the due task cache'); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @test |
||
136 | */ |
||
137 | public function due_tasks_are_ordered_by_when_they_are_defined() |
||
138 | { |
||
139 | $schedule = new Schedule(); |
||
140 | |||
141 | $schedule->addCommand('my:command')->description('task1'); |
||
142 | $schedule->addCompound() |
||
143 | ->addCommand('another:command', [], 'task2') |
||
144 | ->addCallback(function() {}, 'task3') |
||
145 | ->addProcess('php -v', 'task4') |
||
146 | ->addProcess(new Process(['php -v']), 'task5') |
||
147 | ->onSingleServer() |
||
148 | ; |
||
149 | $schedule->addCommand('my:command')->description('task6'); |
||
150 | |||
151 | $this->assertSame( |
||
152 | [ |
||
153 | 'task1', |
||
154 | 'task2', |
||
155 | 'task3', |
||
156 | 'task4', |
||
157 | 'task5', |
||
158 | 'task6', |
||
159 | ], \array_map(function(Task $task) { |
||
160 | return $task->getDescription(); |
||
161 | }, $schedule->due(new \DateTime())) |
||
162 | ); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @test |
||
167 | */ |
||
168 | public function has_unique_id_based_on_tasks() |
||
169 | { |
||
170 | $schedule1 = new Schedule(); |
||
171 | $schedule1->addCommand('my:command'); |
||
172 | $schedule2 = new Schedule(); |
||
173 | $schedule2->addCommand('my:command'); |
||
174 | $schedule3 = new Schedule(); |
||
175 | $schedule3->addCommand('another:command'); |
||
176 | $schedule4 = new Schedule(); |
||
177 | $schedule4->addCommand('my:command'); |
||
178 | $schedule4->addCommand('another:command'); |
||
179 | |||
180 | $this->assertSame((new Schedule())->getId(), (new Schedule())->getId()); |
||
181 | $this->assertSame($schedule1->getId(), $schedule2->getId()); |
||
182 | $this->assertNotSame($schedule2->getId(), $schedule3->getId()); |
||
183 | $this->assertNotSame($schedule2->getId(), $schedule4->getId()); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @test |
||
188 | */ |
||
189 | public function can_add_single_server_extension() |
||
190 | { |
||
191 | $schedule = new Schedule(); |
||
192 | $schedule->addCommand('my:command'); |
||
193 | $schedule->onSingleServer(); |
||
194 | |||
195 | $this->assertInstanceOf(SingleServerExtension::class, $schedule->getExtensions()[0]); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @test |
||
200 | */ |
||
201 | public function can_add_email_on_failure_extension() |
||
202 | { |
||
203 | $schedule = new Schedule(); |
||
204 | $schedule->emailOnFailure('[email protected]', 'my subject', function(Email $email) { |
||
205 | $email->cc('[email protected]'); |
||
206 | }); |
||
207 | |||
208 | $this->assertTrue($schedule->getExtensions()[0]->isHook(Schedule::FAILURE)); |
||
209 | $this->assertSame('[email protected]', $schedule->getExtensions()[0]->getEmail()->getTo()[0]->toString()); |
||
210 | $this->assertSame('[email protected]', $schedule->getExtensions()[0]->getEmail()->getCc()[0]->toString()); |
||
211 | $this->assertSame('my subject', $schedule->getExtensions()[0]->getEmail()->getSubject()); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @test |
||
216 | */ |
||
217 | public function can_add_environment_extension() |
||
218 | { |
||
219 | $schedule = new Schedule(); |
||
220 | $schedule->environments('prod', 'stage'); |
||
221 | |||
222 | $this->assertSame(['prod', 'stage'], $schedule->getExtensions()[0]->getRunEnvironments()); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @test |
||
227 | */ |
||
228 | public function can_set_timezone() |
||
229 | { |
||
230 | $schedule = new Schedule(); |
||
231 | $schedule->add((new MockTask())->description('task1')); |
||
232 | $schedule->add((new MockTask())->description('task2')->timezone('America/Toronto')); |
||
233 | |||
234 | $this->assertNull($schedule->getTimezone()); |
||
235 | $this->assertNull($schedule->all()[0]->getTimezone()); |
||
236 | $this->assertNull($schedule->due(new \DateTime())[0]->getTimezone()); |
||
237 | $this->assertSame('America/Toronto', $schedule->all()[1]->getTimezone()->getName()); |
||
238 | $this->assertSame('America/Toronto', $schedule->due(new \DateTime())[1]->getTimezone()->getName()); |
||
239 | |||
240 | $schedule->timezone('UTC'); |
||
241 | |||
242 | $this->assertSame('UTC', $schedule->getTimezone()->getName()); |
||
243 | $this->assertSame('UTC', $schedule->all()[0]->getTimezone()->getName()); |
||
244 | $this->assertSame('UTC', $schedule->due(new \DateTime())[0]->getTimezone()->getName()); |
||
245 | $this->assertSame('America/Toronto', $schedule->all()[1]->getTimezone()->getName()); |
||
246 | $this->assertSame('America/Toronto', $schedule->due(new \DateTime())[1]->getTimezone()->getName()); |
||
247 | |||
248 | $schedule->timezone(new \DateTimeZone('America/Los_Angeles')); |
||
249 | |||
250 | $this->assertSame('America/Los_Angeles', $schedule->getTimezone()->getName()); |
||
251 | $this->assertSame('UTC', $schedule->all()[0]->getTimezone()->getName()); |
||
252 | $this->assertSame('UTC', $schedule->due(new \DateTime())[0]->getTimezone()->getName()); |
||
253 | $this->assertSame('America/Toronto', $schedule->all()[1]->getTimezone()->getName()); |
||
254 | $this->assertSame('America/Toronto', $schedule->due(new \DateTime())[1]->getTimezone()->getName()); |
||
255 | } |
||
256 | } |
||
257 |
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