Passed
Push — master ( 18c542...46352d )
by Kevin
02:34
created

ScheduleTest::can_add_callback_extensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
c 0
b 0
f 0
dl 0
loc 27
rs 9.568
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\Contracts\HttpClient\HttpClientInterface;
10
use Zenstruck\ScheduleBundle\Event\AfterScheduleEvent;
11
use Zenstruck\ScheduleBundle\Event\BeforeScheduleEvent;
12
use Zenstruck\ScheduleBundle\Schedule;
13
use Zenstruck\ScheduleBundle\Schedule\Exception\SkipSchedule;
14
use Zenstruck\ScheduleBundle\Schedule\Extension;
15
use Zenstruck\ScheduleBundle\Schedule\Task;
16
use Zenstruck\ScheduleBundle\Schedule\Task\CallbackTask;
17
use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask;
18
19
/**
20
 * @author Kevin Bond <[email protected]>
21
 */
22
class ScheduleTest extends TestCase
23
{
24
    /**
25
     * @test
26
     */
27
    public function can_add_tasks()
28
    {
29
        $schedule = new Schedule();
30
31
        $schedule->add(new CallbackTask(function () {}))->description('task1');
32
        $schedule->addCallback(function () {})->description('task2');
33
        $schedule->addProcess('php -v')->description('task3');
34
        $schedule->addCommand('my:command')->description('task4');
35
36
        $this->assertCount(4, $schedule->all());
37
        $this->assertSame(['task1', 'task2', 'task3', 'task4'], \array_map(
38
            function (Task $task) {
39
                return $task->getDescription();
40
            },
41
            $schedule->all()
42
        ));
43
    }
44
45
    /**
46
     * @test
47
     */
48
    public function can_add_compound_tasks()
49
    {
50
        $schedule = new Schedule();
51
52
        $schedule->addCommand('my:command')->description('task1')->tuesdays();
53
        $schedule->addCompound()
54
            ->addCommand('another:command', [], 'task2')
55
            ->addCallback(function () {}, 'task3')
56
            ->addProcess('php -v', 'task4')
57
            ->add((new CommandTask('yet:another:command'))
58
                ->description('task5')
59
                ->sundays()
60
                ->timezone('America/Los_Angeles')
61
            )
62
            ->timezone('UTC')
63
            ->mondays()
64
            ->onSingleServer()
65
        ;
66
67
        $this->assertCount(5, $schedule->all());
68
        $this->assertSame('task1', $schedule->all()[0]->getDescription());
69
        $this->assertSame('* * * * 2', $schedule->all()[0]->getExpression());
70
        $this->assertNull($schedule->all()[0]->getTimezone());
71
        $this->assertCount(0, $schedule->all()[0]->getExtensions());
72
        $this->assertSame('task2', $schedule->all()[1]->getDescription());
73
        $this->assertSame('* * * * 1', $schedule->all()[1]->getExpression());
74
        $this->assertSame('UTC', $schedule->all()[1]->getTimezone()->getName());
75
        $this->assertCount(1, $schedule->all()[1]->getExtensions());
76
        $this->assertSame('task3', $schedule->all()[2]->getDescription());
77
        $this->assertSame('* * * * 1', $schedule->all()[2]->getExpression());
78
        $this->assertSame('UTC', $schedule->all()[2]->getTimezone()->getName());
79
        $this->assertCount(1, $schedule->all()[2]->getExtensions());
80
        $this->assertSame('task4', $schedule->all()[3]->getDescription());
81
        $this->assertSame('* * * * 1', $schedule->all()[3]->getExpression());
82
        $this->assertSame('UTC', $schedule->all()[3]->getTimezone()->getName());
83
        $this->assertCount(1, $schedule->all()[3]->getExtensions());
84
        $this->assertSame('task5', $schedule->all()[4]->getDescription());
85
        $this->assertSame('* * * * 1', $schedule->all()[4]->getExpression());
86
        $this->assertSame('UTC', $schedule->all()[4]->getTimezone()->getName());
87
        $this->assertCount(1, $schedule->all()[4]->getExtensions());
88
    }
89
90
    /**
91
     * @test
92
     */
93
    public function can_get_due_tasks()
94
    {
95
        $schedule = new Schedule();
96
97
        $schedule->addCallback(function () {})->description('task1');
98
        $notDueTask = $schedule->addProcess('php -v')->description('task2')->sundays();
99
100
        if ('Sun' === \date('D')) {
101
            $notDueTask->mondays();
102
        }
103
104
        $this->assertCount(2, $schedule->all());
105
        $this->assertCount(1, $schedule->due());
106
        $this->assertCount(1, $schedule->due(), 'Due tasks are cached');
107
        $this->assertSame('task1', $schedule->due()[0]->getDescription());
108
    }
109
110
    /**
111
     * @test
112
     */
113
    public function has_unique_id_based_on_tasks()
114
    {
115
        $schedule1 = new Schedule();
116
        $schedule1->addCommand('my:command');
117
        $schedule2 = new Schedule();
118
        $schedule2->addCommand('my:command');
119
        $schedule3 = new Schedule();
120
        $schedule3->addCommand('another:command');
121
        $schedule4 = new Schedule();
122
        $schedule4->addCommand('my:command');
123
        $schedule4->addCommand('another:command');
124
125
        $this->assertSame((new Schedule())->getId(), (new Schedule())->getId());
126
        $this->assertSame($schedule1->getId(), $schedule2->getId());
127
        $this->assertNotSame($schedule2->getId(), $schedule3->getId());
128
        $this->assertNotSame($schedule2->getId(), $schedule4->getId());
129
    }
130
131
    /**
132
     * @test
133
     */
134
    public function false_when_filter_skips_schedule()
135
    {
136
        $schedule = new Schedule();
137
138
        $schedule->when('boolean value', false);
139
140
        $this->expectException(SkipSchedule::class);
141
        $this->expectExceptionMessage('boolean value');
142
143
        $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

143
        $schedule->getExtensions()[0]->/** @scrutinizer ignore-call */ 
144
                                       filterSchedule(new BeforeScheduleEvent($schedule));
Loading history...
144
    }
145
146
    /**
147
     * @test
148
     */
149
    public function callback_returning_false_when_filter_skips_schedule()
150
    {
151
        $schedule = new Schedule();
152
153
        $schedule->when('callback value', function () { return false; });
154
155
        $this->expectException(SkipSchedule::class);
156
        $this->expectExceptionMessage('callback value');
157
158
        $schedule->getExtensions()[0]->filterSchedule(new BeforeScheduleEvent($schedule));
159
    }
160
161
    /**
162
     * @test
163
     */
164
    public function true_when_filter_allows_schedule_to_run()
165
    {
166
        $schedule = new Schedule();
167
168
        $schedule->when('boolean value', true);
169
170
        $schedule->getExtensions()[0]->filterSchedule(new BeforeScheduleEvent($schedule));
171
172
        $this->assertTrue(true);
173
    }
174
175
    /**
176
     * @test
177
     */
178
    public function callback_returning_true_when_filter_allows_schedule_to_run()
179
    {
180
        $schedule = new Schedule();
181
182
        $schedule->when('callback value', function () { return true; });
183
184
        $schedule->getExtensions()[0]->filterSchedule(new BeforeScheduleEvent($schedule));
185
186
        $this->assertTrue(true);
187
    }
188
189
    /**
190
     * @test
191
     */
192
    public function true_skip_filter_skips_schedule()
193
    {
194
        $schedule = new Schedule();
195
196
        $schedule->skip('boolean value', true);
197
198
        $this->expectException(SkipSchedule::class);
199
        $this->expectExceptionMessage('boolean value');
200
201
        $schedule->getExtensions()[0]->filterSchedule(new BeforeScheduleEvent($schedule));
202
    }
203
204
    /**
205
     * @test
206
     */
207
    public function callback_returning_true_skip_filter_skips_schedule()
208
    {
209
        $schedule = new Schedule();
210
211
        $schedule->skip('callback value', function () { return true; });
212
213
        $this->expectException(SkipSchedule::class);
214
        $this->expectExceptionMessage('callback value');
215
216
        $schedule->getExtensions()[0]->filterSchedule(new BeforeScheduleEvent($schedule));
217
    }
218
219
    /**
220
     * @test
221
     */
222
    public function false_skip_filter_allows_schedule_to_run()
223
    {
224
        $schedule = new Schedule();
225
226
        $schedule->skip('boolean value', false);
227
228
        $schedule->getExtensions()[0]->filterSchedule(new BeforeScheduleEvent($schedule));
229
230
        $this->assertTrue(true);
231
    }
232
233
    /**
234
     * @test
235
     */
236
    public function callback_returning_false_skip_filter_allows_schedule_to_run()
237
    {
238
        $schedule = new Schedule();
239
240
        $schedule->skip('callback value', function () { return false; });
241
242
        $schedule->getExtensions()[0]->filterSchedule(new BeforeScheduleEvent($schedule));
243
244
        $this->assertTrue(true);
245
    }
246
247
    /**
248
     * @test
249
     */
250
    public function can_add_callback_extensions()
251
    {
252
        $schedule = new Schedule();
253
        $calls = [];
254
255
        $schedule->filter(function () use (&$calls) { $calls[] = 'filter'; });
256
        $schedule->before(function () use (&$calls) { $calls[] = 'before'; });
257
        $schedule->after(function () use (&$calls) { $calls[] = 'after'; });
258
        $schedule->then(function () use (&$calls) { $calls[] = 'then'; });
259
        $schedule->onSuccess(function () use (&$calls) { $calls[] = 'onSuccess'; });
260
        $schedule->onFailure(function () use (&$calls) { $calls[] = 'onFailure'; });
261
262
        $schedule->getExtensions()[0]->filterSchedule($event = new BeforeScheduleEvent($schedule));
263
        $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

263
        $schedule->getExtensions()[1]->/** @scrutinizer ignore-call */ 
264
                                       beforeSchedule(new BeforeScheduleEvent($schedule));
Loading history...
264
        $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

264
        $schedule->getExtensions()[2]->/** @scrutinizer ignore-call */ 
265
                                       afterSchedule(new AfterScheduleEvent($event, []));
Loading history...
265
        $schedule->getExtensions()[3]->afterSchedule(new AfterScheduleEvent($event, []));
266
        $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

266
        $schedule->getExtensions()[4]->/** @scrutinizer ignore-call */ 
267
                                       onScheduleSuccess(new AfterScheduleEvent($event, []));
Loading history...
267
        $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

267
        $schedule->getExtensions()[5]->/** @scrutinizer ignore-call */ 
268
                                       onScheduleFailure(new AfterScheduleEvent($event, []));
Loading history...
268
269
        $this->assertSame([
270
            'filter',
271
            'before',
272
            'after',
273
            'then',
274
            'onSuccess',
275
            'onFailure',
276
        ], $calls);
277
    }
278
279
    /**
280
     * @test
281
     */
282
    public function can_add_single_server_extension()
283
    {
284
        $schedule1 = new Schedule();
285
        $schedule1->addCommand('my:command');
286
        $schedule1->onSingleServer();
287
288
        $schedule2 = new Schedule();
289
        $schedule2->addCommand('my:command');
290
        $schedule2->onSingleServer();
291
292
        $lockFactory = new LockFactory(new FlockStore());
293
294
        $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

294
        $schedule1->getExtensions()[0]->/** @scrutinizer ignore-call */ 
295
                                        aquireScheduleLock($lockFactory, $schedule1, \time());
Loading history...
295
296
        $this->expectException(SkipSchedule::class);
297
        $this->expectExceptionMessage('Schedule running on another server.');
298
299
        $schedule2->getExtensions()[0]->aquireScheduleLock($lockFactory, $schedule2, \time());
300
    }
301
302
    /**
303
     * @test
304
     */
305
    public function can_add_ping_extensions()
306
    {
307
        $schedule = new Schedule();
308
309
        $schedule->pingBefore('http://before.com');
310
        $schedule->pingAfter('http://after.com', 'POST');
311
        $schedule->thenPing('http://then.com');
312
        $schedule->pingOnSuccess('http://success.com');
313
        $schedule->pingOnFailure('http://failure.com');
314
315
        $client = $this->createMock(HttpClientInterface::class);
316
        $client->expects($this->exactly(5))->method('request')->withConsecutive(
317
            [$this->equalTo('GET'), $this->equalTo('http://before.com'), $this->isType('array')],
318
            [$this->equalTo('POST'), $this->equalTo('http://after.com'), $this->isType('array')],
319
            [$this->equalTo('GET'), $this->equalTo('http://then.com'), $this->isType('array')],
320
            [$this->equalTo('GET'), $this->equalTo('http://success.com'), $this->isType('array')],
321
            [$this->equalTo('GET'), $this->equalTo('http://failure.com'), $this->isType('array')]
322
        );
323
324
        $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

324
        $schedule->getExtensions()[0]->/** @scrutinizer ignore-call */ 
325
                                       setHttpClient($client)->beforeSchedule($event = new BeforeScheduleEvent($schedule));
Loading history...
325
        $schedule->getExtensions()[1]->setHttpClient($client)->afterSchedule(new AfterScheduleEvent($event, []));
326
        $schedule->getExtensions()[2]->setHttpClient($client)->afterSchedule(new AfterScheduleEvent($event, []));
327
        $schedule->getExtensions()[3]->setHttpClient($client)->onScheduleSuccess(new AfterScheduleEvent($event, []));
328
        $schedule->getExtensions()[4]->setHttpClient($client)->onScheduleFailure(new AfterScheduleEvent($event, []));
329
    }
330
331
    /**
332
     * @test
333
     */
334
    public function can_add_email_on_failure_extension()
335
    {
336
        $schedule = new Schedule();
337
        $schedule->emailOnFailure('[email protected]', 'my subject', function (Email $email) {
338
            $email->cc('[email protected]');
339
        });
340
341
        $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

341
        $this->assertTrue($schedule->getExtensions()[0]->/** @scrutinizer ignore-call */ isHook(Extension::SCHEDULE_FAILURE));
Loading history...
342
        $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

342
        $this->assertSame('[email protected]', $schedule->getExtensions()[0]->/** @scrutinizer ignore-call */ getEmail()->getTo()[0]->toString());
Loading history...
343
        $this->assertSame('[email protected]', $schedule->getExtensions()[0]->getEmail()->getCc()[0]->toString());
344
        $this->assertSame('my subject', $schedule->getExtensions()[0]->getEmail()->getSubject());
345
    }
346
347
    /**
348
     * @test
349
     */
350
    public function can_add_environment_extension()
351
    {
352
        $schedule = new Schedule();
353
        $schedule->environments('prod', 'stage');
354
355
        $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

355
        $this->assertSame(['prod', 'stage'], $schedule->getExtensions()[0]->/** @scrutinizer ignore-call */ getRunEnvironments());
Loading history...
356
    }
357
}
358