Passed
Push — master ( 68072d...6506ec )
by Kevin
01:47
created

ScheduleTest::has_unique_id_based_on_tasks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 16
rs 9.8333
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Tests;
4
5
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Symfony\Component\Lock\LockFactory;
7
use Symfony\Component\Lock\Store\FlockStore;
8
use Symfony\Component\Mime\Email;
9
use Symfony\Component\Process\Process;
10
use Symfony\Contracts\HttpClient\HttpClientInterface;
11
use Zenstruck\ScheduleBundle\Event\AfterScheduleEvent;
12
use Zenstruck\ScheduleBundle\Event\BeforeScheduleEvent;
13
use Zenstruck\ScheduleBundle\Schedule;
14
use Zenstruck\ScheduleBundle\Schedule\Exception\SkipSchedule;
15
use Zenstruck\ScheduleBundle\Schedule\Extension;
16
use Zenstruck\ScheduleBundle\Schedule\Task;
17
use Zenstruck\ScheduleBundle\Schedule\Task\CallbackTask;
18
use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask;
19
20
/**
21
 * @author Kevin Bond <[email protected]>
22
 */
23
class ScheduleTest extends TestCase
24
{
25
    /**
26
     * @test
27
     */
28
    public function can_add_tasks()
29
    {
30
        $schedule = new Schedule();
31
32
        $schedule->add(new CallbackTask(function () {}))->description('task1');
33
        $schedule->addCallback(function () {})->description('task2');
34
        $schedule->addProcess('php -v')->description('task3');
35
        $schedule->addProcess(new Process(['php -v']))->description('task4');
36
        $schedule->addCommand('my:command')->description('task5');
37
38
        $this->assertCount(5, $schedule->all());
39
        $this->assertSame(['task1', 'task2', 'task3', 'task4', 'task5'], \array_map(
40
            function (Task $task) {
41
                return $task->getDescription();
42
            },
43
            $schedule->all()
44
        ));
45
    }
46
47
    /**
48
     * @test
49
     */
50
    public function can_add_compound_tasks()
51
    {
52
        $schedule = new Schedule();
53
54
        $schedule->addCommand('my:command')->description('task1')->tuesdays();
55
        $schedule->addCompound()
56
            ->addCommand('another:command', [], 'task2')
57
            ->addCallback(function () {}, 'task3')
58
            ->addProcess('php -v', 'task4')
59
            ->addProcess(new Process(['php -v']), 'task5')
60
            ->add((new CommandTask('yet:another:command'))
61
                ->description('task6')
62
                ->sundays()
63
                ->timezone('America/Los_Angeles')
64
            )
65
            ->timezone('UTC')
66
            ->mondays()
67
            ->onSingleServer()
68
        ;
69
70
        $this->assertCount(6, $schedule->all());
71
        $this->assertSame('task1', $schedule->all()[0]->getDescription());
72
        $this->assertSame('* * * * 2', $schedule->all()[0]->getExpression());
73
        $this->assertNull($schedule->all()[0]->getTimezone());
74
        $this->assertCount(0, $schedule->all()[0]->getExtensions());
75
        $this->assertSame('task2', $schedule->all()[1]->getDescription());
76
        $this->assertSame('* * * * 1', $schedule->all()[1]->getExpression());
77
        $this->assertSame('UTC', $schedule->all()[1]->getTimezone()->getName());
78
        $this->assertCount(1, $schedule->all()[1]->getExtensions());
79
        $this->assertSame('task3', $schedule->all()[2]->getDescription());
80
        $this->assertSame('* * * * 1', $schedule->all()[2]->getExpression());
81
        $this->assertSame('UTC', $schedule->all()[2]->getTimezone()->getName());
82
        $this->assertCount(1, $schedule->all()[2]->getExtensions());
83
        $this->assertSame('task4', $schedule->all()[3]->getDescription());
84
        $this->assertSame('* * * * 1', $schedule->all()[3]->getExpression());
85
        $this->assertSame('UTC', $schedule->all()[3]->getTimezone()->getName());
86
        $this->assertCount(1, $schedule->all()[3]->getExtensions());
87
        $this->assertSame('task5', $schedule->all()[4]->getDescription());
88
        $this->assertSame('* * * * 1', $schedule->all()[4]->getExpression());
89
        $this->assertSame('UTC', $schedule->all()[4]->getTimezone()->getName());
90
        $this->assertCount(1, $schedule->all()[4]->getExtensions());
91
        $this->assertSame('task6', $schedule->all()[5]->getDescription());
92
        $this->assertSame('* * * * 1', $schedule->all()[5]->getExpression());
93
        $this->assertSame('UTC', $schedule->all()[5]->getTimezone()->getName());
94
        $this->assertCount(1, $schedule->all()[5]->getExtensions());
95
    }
96
97
    /**
98
     * @test
99
     */
100
    public function can_get_due_tasks()
101
    {
102
        $schedule = new Schedule();
103
104
        $schedule->addCallback(function () {})->description('task1');
105
        $notDueTask = $schedule->addProcess('php -v')->description('task2')->sundays();
106
107
        if ('Sun' === \date('D')) {
108
            $notDueTask->mondays();
109
        }
110
111
        $this->assertCount(2, $schedule->all());
112
        $this->assertCount(1, $schedule->due());
113
        $this->assertCount(1, $schedule->due(), 'Due tasks are cached');
114
        $this->assertSame('task1', $schedule->due()[0]->getDescription());
115
    }
116
117
    /**
118
     * @test
119
     */
120
    public function has_unique_id_based_on_tasks()
121
    {
122
        $schedule1 = new Schedule();
123
        $schedule1->addCommand('my:command');
124
        $schedule2 = new Schedule();
125
        $schedule2->addCommand('my:command');
126
        $schedule3 = new Schedule();
127
        $schedule3->addCommand('another:command');
128
        $schedule4 = new Schedule();
129
        $schedule4->addCommand('my:command');
130
        $schedule4->addCommand('another:command');
131
132
        $this->assertSame((new Schedule())->getId(), (new Schedule())->getId());
133
        $this->assertSame($schedule1->getId(), $schedule2->getId());
134
        $this->assertNotSame($schedule2->getId(), $schedule3->getId());
135
        $this->assertNotSame($schedule2->getId(), $schedule4->getId());
136
    }
137
138
    /**
139
     * @test
140
     */
141
    public function false_when_filter_skips_schedule()
142
    {
143
        $schedule = new Schedule();
144
145
        $schedule->when('boolean value', false);
146
147
        $this->expectException(SkipSchedule::class);
148
        $this->expectExceptionMessage('boolean value');
149
150
        $schedule->getExtensions()[0]->filterSchedule(new BeforeScheduleEvent($schedule));
0 ignored issues
show
Bug introduced by
The method filterSchedule() does not exist on Zenstruck\ScheduleBundle\Schedule\Extension. It seems like you code against a sub-type of Zenstruck\ScheduleBundle\Schedule\Extension such as Zenstruck\ScheduleBundle...n\SelfHandlingExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

150
        $schedule->getExtensions()[0]->/** @scrutinizer ignore-call */ 
151
                                       filterSchedule(new BeforeScheduleEvent($schedule));
Loading history...
151
    }
152
153
    /**
154
     * @test
155
     */
156
    public function callback_returning_false_when_filter_skips_schedule()
157
    {
158
        $schedule = new Schedule();
159
160
        $schedule->when('callback value', function () { return false; });
161
162
        $this->expectException(SkipSchedule::class);
163
        $this->expectExceptionMessage('callback value');
164
165
        $schedule->getExtensions()[0]->filterSchedule(new BeforeScheduleEvent($schedule));
166
    }
167
168
    /**
169
     * @test
170
     */
171
    public function true_when_filter_allows_schedule_to_run()
172
    {
173
        $schedule = new Schedule();
174
175
        $schedule->when('boolean value', true);
176
177
        $schedule->getExtensions()[0]->filterSchedule(new BeforeScheduleEvent($schedule));
178
179
        $this->assertTrue(true);
180
    }
181
182
    /**
183
     * @test
184
     */
185
    public function callback_returning_true_when_filter_allows_schedule_to_run()
186
    {
187
        $schedule = new Schedule();
188
189
        $schedule->when('callback value', function () { return true; });
190
191
        $schedule->getExtensions()[0]->filterSchedule(new BeforeScheduleEvent($schedule));
192
193
        $this->assertTrue(true);
194
    }
195
196
    /**
197
     * @test
198
     */
199
    public function true_skip_filter_skips_schedule()
200
    {
201
        $schedule = new Schedule();
202
203
        $schedule->skip('boolean value', true);
204
205
        $this->expectException(SkipSchedule::class);
206
        $this->expectExceptionMessage('boolean value');
207
208
        $schedule->getExtensions()[0]->filterSchedule(new BeforeScheduleEvent($schedule));
209
    }
210
211
    /**
212
     * @test
213
     */
214
    public function callback_returning_true_skip_filter_skips_schedule()
215
    {
216
        $schedule = new Schedule();
217
218
        $schedule->skip('callback value', function () { return true; });
219
220
        $this->expectException(SkipSchedule::class);
221
        $this->expectExceptionMessage('callback value');
222
223
        $schedule->getExtensions()[0]->filterSchedule(new BeforeScheduleEvent($schedule));
224
    }
225
226
    /**
227
     * @test
228
     */
229
    public function false_skip_filter_allows_schedule_to_run()
230
    {
231
        $schedule = new Schedule();
232
233
        $schedule->skip('boolean value', false);
234
235
        $schedule->getExtensions()[0]->filterSchedule(new BeforeScheduleEvent($schedule));
236
237
        $this->assertTrue(true);
238
    }
239
240
    /**
241
     * @test
242
     */
243
    public function callback_returning_false_skip_filter_allows_schedule_to_run()
244
    {
245
        $schedule = new Schedule();
246
247
        $schedule->skip('callback value', function () { return false; });
248
249
        $schedule->getExtensions()[0]->filterSchedule(new BeforeScheduleEvent($schedule));
250
251
        $this->assertTrue(true);
252
    }
253
254
    /**
255
     * @test
256
     */
257
    public function can_add_callback_extensions()
258
    {
259
        $schedule = new Schedule();
260
        $calls = [];
261
262
        $schedule->filter(function () use (&$calls) { $calls[] = 'filter'; });
263
        $schedule->before(function () use (&$calls) { $calls[] = 'before'; });
264
        $schedule->after(function () use (&$calls) { $calls[] = 'after'; });
265
        $schedule->then(function () use (&$calls) { $calls[] = 'then'; });
266
        $schedule->onSuccess(function () use (&$calls) { $calls[] = 'onSuccess'; });
267
        $schedule->onFailure(function () use (&$calls) { $calls[] = 'onFailure'; });
268
269
        $schedule->getExtensions()[0]->filterSchedule($event = new BeforeScheduleEvent($schedule));
270
        $schedule->getExtensions()[1]->beforeSchedule(new BeforeScheduleEvent($schedule));
0 ignored issues
show
Bug introduced by
The method beforeSchedule() does not exist on Zenstruck\ScheduleBundle\Schedule\Extension. It seems like you code against a sub-type of Zenstruck\ScheduleBundle\Schedule\Extension such as Zenstruck\ScheduleBundle...n\SelfHandlingExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

270
        $schedule->getExtensions()[1]->/** @scrutinizer ignore-call */ 
271
                                       beforeSchedule(new BeforeScheduleEvent($schedule));
Loading history...
271
        $schedule->getExtensions()[2]->afterSchedule(new AfterScheduleEvent($event, []));
0 ignored issues
show
Bug introduced by
The method afterSchedule() does not exist on Zenstruck\ScheduleBundle\Schedule\Extension. It seems like you code against a sub-type of Zenstruck\ScheduleBundle\Schedule\Extension such as Zenstruck\ScheduleBundle...n\SelfHandlingExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

271
        $schedule->getExtensions()[2]->/** @scrutinizer ignore-call */ 
272
                                       afterSchedule(new AfterScheduleEvent($event, []));
Loading history...
272
        $schedule->getExtensions()[3]->afterSchedule(new AfterScheduleEvent($event, []));
273
        $schedule->getExtensions()[4]->onScheduleSuccess(new AfterScheduleEvent($event, []));
0 ignored issues
show
Bug introduced by
The method onScheduleSuccess() does not exist on Zenstruck\ScheduleBundle\Schedule\Extension. It seems like you code against a sub-type of Zenstruck\ScheduleBundle\Schedule\Extension such as Zenstruck\ScheduleBundle...n\SelfHandlingExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

273
        $schedule->getExtensions()[4]->/** @scrutinizer ignore-call */ 
274
                                       onScheduleSuccess(new AfterScheduleEvent($event, []));
Loading history...
274
        $schedule->getExtensions()[5]->onScheduleFailure(new AfterScheduleEvent($event, []));
0 ignored issues
show
Bug introduced by
The method onScheduleFailure() does not exist on Zenstruck\ScheduleBundle\Schedule\Extension. It seems like you code against a sub-type of Zenstruck\ScheduleBundle\Schedule\Extension such as Zenstruck\ScheduleBundle...n\SelfHandlingExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

274
        $schedule->getExtensions()[5]->/** @scrutinizer ignore-call */ 
275
                                       onScheduleFailure(new AfterScheduleEvent($event, []));
Loading history...
275
276
        $this->assertSame([
277
            'filter',
278
            'before',
279
            'after',
280
            'then',
281
            'onSuccess',
282
            'onFailure',
283
        ], $calls);
284
    }
285
286
    /**
287
     * @test
288
     */
289
    public function can_add_single_server_extension()
290
    {
291
        $schedule1 = new Schedule();
292
        $schedule1->addCommand('my:command');
293
        $schedule1->onSingleServer();
294
295
        $schedule2 = new Schedule();
296
        $schedule2->addCommand('my:command');
297
        $schedule2->onSingleServer();
298
299
        $lockFactory = new LockFactory(new FlockStore());
300
301
        $schedule1->getExtensions()[0]->aquireScheduleLock($lockFactory, $schedule1, \time());
0 ignored issues
show
Bug introduced by
The method aquireScheduleLock() does not exist on Zenstruck\ScheduleBundle\Schedule\Extension. It seems like you code against a sub-type of Zenstruck\ScheduleBundle\Schedule\Extension such as Zenstruck\ScheduleBundle...n\SingleServerExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

301
        $schedule1->getExtensions()[0]->/** @scrutinizer ignore-call */ 
302
                                        aquireScheduleLock($lockFactory, $schedule1, \time());
Loading history...
302
303
        $this->expectException(SkipSchedule::class);
304
        $this->expectExceptionMessage('Schedule running on another server.');
305
306
        $schedule2->getExtensions()[0]->aquireScheduleLock($lockFactory, $schedule2, \time());
307
    }
308
309
    /**
310
     * @test
311
     */
312
    public function can_add_ping_extensions()
313
    {
314
        $schedule = new Schedule();
315
316
        $schedule->pingBefore('http://before.com');
317
        $schedule->pingAfter('http://after.com', 'POST');
318
        $schedule->thenPing('http://then.com');
319
        $schedule->pingOnSuccess('http://success.com');
320
        $schedule->pingOnFailure('http://failure.com');
321
322
        $client = $this->createMock(HttpClientInterface::class);
323
        $client->expects($this->exactly(5))->method('request')->withConsecutive(
324
            [$this->equalTo('GET'), $this->equalTo('http://before.com'), $this->isType('array')],
325
            [$this->equalTo('POST'), $this->equalTo('http://after.com'), $this->isType('array')],
326
            [$this->equalTo('GET'), $this->equalTo('http://then.com'), $this->isType('array')],
327
            [$this->equalTo('GET'), $this->equalTo('http://success.com'), $this->isType('array')],
328
            [$this->equalTo('GET'), $this->equalTo('http://failure.com'), $this->isType('array')]
329
        );
330
331
        $schedule->getExtensions()[0]->setHttpClient($client)->beforeSchedule($event = new BeforeScheduleEvent($schedule));
0 ignored issues
show
Bug introduced by
The method setHttpClient() does not exist on Zenstruck\ScheduleBundle\Schedule\Extension. It seems like you code against a sub-type of Zenstruck\ScheduleBundle\Schedule\Extension such as Zenstruck\ScheduleBundle...Extension\PingExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

331
        $schedule->getExtensions()[0]->/** @scrutinizer ignore-call */ 
332
                                       setHttpClient($client)->beforeSchedule($event = new BeforeScheduleEvent($schedule));
Loading history...
332
        $schedule->getExtensions()[1]->setHttpClient($client)->afterSchedule(new AfterScheduleEvent($event, []));
333
        $schedule->getExtensions()[2]->setHttpClient($client)->afterSchedule(new AfterScheduleEvent($event, []));
334
        $schedule->getExtensions()[3]->setHttpClient($client)->onScheduleSuccess(new AfterScheduleEvent($event, []));
335
        $schedule->getExtensions()[4]->setHttpClient($client)->onScheduleFailure(new AfterScheduleEvent($event, []));
336
    }
337
338
    /**
339
     * @test
340
     */
341
    public function can_add_email_on_failure_extension()
342
    {
343
        $schedule = new Schedule();
344
        $schedule->emailOnFailure('[email protected]', 'my subject', function (Email $email) {
345
            $email->cc('[email protected]');
346
        });
347
348
        $this->assertTrue($schedule->getExtensions()[0]->isHook(Extension::SCHEDULE_FAILURE));
0 ignored issues
show
Bug introduced by
The method isHook() does not exist on Zenstruck\ScheduleBundle\Schedule\Extension. It seems like you code against a sub-type of Zenstruck\ScheduleBundle\Schedule\Extension such as Zenstruck\ScheduleBundle...xtension\EmailExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

348
        $this->assertTrue($schedule->getExtensions()[0]->/** @scrutinizer ignore-call */ isHook(Extension::SCHEDULE_FAILURE));
Loading history...
349
        $this->assertSame('[email protected]', $schedule->getExtensions()[0]->getEmail()->getTo()[0]->toString());
0 ignored issues
show
Bug introduced by
The method getEmail() does not exist on Zenstruck\ScheduleBundle\Schedule\Extension. It seems like you code against a sub-type of Zenstruck\ScheduleBundle\Schedule\Extension such as Zenstruck\ScheduleBundle...xtension\EmailExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

349
        $this->assertSame('[email protected]', $schedule->getExtensions()[0]->/** @scrutinizer ignore-call */ getEmail()->getTo()[0]->toString());
Loading history...
350
        $this->assertSame('[email protected]', $schedule->getExtensions()[0]->getEmail()->getCc()[0]->toString());
351
        $this->assertSame('my subject', $schedule->getExtensions()[0]->getEmail()->getSubject());
352
    }
353
354
    /**
355
     * @test
356
     */
357
    public function can_add_environment_extension()
358
    {
359
        $schedule = new Schedule();
360
        $schedule->environments('prod', 'stage');
361
362
        $this->assertSame(['prod', 'stage'], $schedule->getExtensions()[0]->getRunEnvironments());
0 ignored issues
show
Bug introduced by
The method getRunEnvironments() does not exist on Zenstruck\ScheduleBundle\Schedule\Extension. It seems like you code against a sub-type of Zenstruck\ScheduleBundle\Schedule\Extension such as Zenstruck\ScheduleBundle...on\EnvironmentExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

362
        $this->assertSame(['prod', 'stage'], $schedule->getExtensions()[0]->/** @scrutinizer ignore-call */ getRunEnvironments());
Loading history...
363
    }
364
365
    /**
366
     * @test
367
     */
368
    public function can_set_timezone()
369
    {
370
        $schedule = new Schedule();
371
372
        $this->assertNull($schedule->getTimezone());
373
374
        $schedule->timezone('UTC');
375
376
        $this->assertSame('UTC', $schedule->getTimezone()->getName());
377
378
        $schedule->timezone(new \DateTimeZone('America/Los_Angeles'));
379
380
        $this->assertSame('America/Los_Angeles', $schedule->getTimezone()->getName());
381
    }
382
}
383