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

TaskTest   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 467
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 25
eloc 200
c 0
b 0
f 0
dl 0
loc 467
rs 10

56 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ callback_returning_true_skip_filter_skips_task() 0 10 1
unless_between_extension_run() 0 10 ?
false_when_filter_skips_task() 0 10 ?
A hp$0 ➔ between_extension_run() 0 10 1
callback_returning_false_skip_filter_allows_task_to_run() 0 9 ?
can_add_email_on_failure_extension() 0 11 ?
A hp$0 ➔ can_add_ping_extensions() 0 24 1
A hp$0 ➔ can_add_email_after_extension() 0 11 1
A frequencyProvider() 0 37 1
A hp$0 ➔ emailAfterMethodProvider() 0 5 1
A hp$0 ➔ callback_returning_false_skip_filter_allows_task_to_run() 0 9 1
A hp$0 ➔ betweenExtensionRunProvider() 0 6 1
betweenExtensionSkipProvider() 0 6 ?
between_extension_run() 0 10 ?
A can_fluently_create_frequency() 0 3 1
A hp$0 ➔ has_unique_id_based_on_description_and_frequency() 0 7 1
can_add_ping_extensions() 0 24 ?
can_add_without_overlapping_extension() 0 11 ?
A hp$0 ➔ callback_returning_true_when_filter_allows_task_to_run() 0 9 1
can_add_email_after_extension() 0 11 ?
A can_set_description() 0 6 1
has_unique_id_based_on_description_and_frequency() 0 7 ?
between_extension_skip() 0 11 ?
betweenExtensionRunProvider() 0 6 ?
A hp$0 ➔ unless_between_extension_skip() 0 11 1
A hp$0 ➔ false_when_filter_skips_task() 0 10 1
A hp$0 ➔ can_add_without_overlapping_extension() 0 11 1
A hp$0 ➔ unless_between_extension_run() 0 10 1
true_skip_filter_skips_task() 0 10 ?
A hp$0 ➔ true_skip_filter_skips_task() 0 10 1
unless_between_extension_skip() 0 11 ?
A hp$0 ➔ between_extension_skip() 0 11 1
createTask() 0 3 ?
A hp$0 ➔ false_skip_filter_allows_task_to_run() 0 9 1
A can_get_next_run() 0 5 1
A hp$0 ➔ can_add_single_server_extension() 0 13 1
A hp$0 ➔ can_add_callback_extensions() 0 27 1
callback_returning_false_when_filter_skips_task() 0 10 ?
false_skip_filter_allows_task_to_run() 0 9 ?
A hp$0 ➔ callback_returning_false_when_filter_skips_task() 0 10 1
emailAfterMethodProvider() 0 5 ?
can_add_single_server_extension() 0 13 ?
callback_returning_true_when_filter_allows_task_to_run() 0 9 ?
unlessBetweenExtensionRunProvider() 0 6 ?
A hp$0 ➔ unlessBetweenExtensionSkipProvider() 0 6 1
A hp$0 ➔ betweenExtensionSkipProvider() 0 6 1
callback_returning_true_skip_filter_skips_task() 0 10 ?
A can_set_timezone() 0 9 1
A cannot_set_invalid_cron_expression() 0 6 1
A hp$0 ➔ createTask() 0 3 1
A hp$0 ➔ unlessBetweenExtensionRunProvider() 0 6 1
A hp$0 ➔ can_add_email_on_failure_extension() 0 11 1
unlessBetweenExtensionSkipProvider() 0 6 ?
can_add_callback_extensions() 0 27 ?
true_when_filter_allows_task_to_run() 0 9 ?
A hp$0 ➔ true_when_filter_allows_task_to_run() 0 9 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Tests\Schedule;
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\AfterTaskEvent;
11
use Zenstruck\ScheduleBundle\Event\BeforeScheduleEvent;
12
use Zenstruck\ScheduleBundle\Event\BeforeTaskEvent;
13
use Zenstruck\ScheduleBundle\Schedule;
14
use Zenstruck\ScheduleBundle\Schedule\Exception\SkipTask;
15
use Zenstruck\ScheduleBundle\Schedule\Extension;
16
use Zenstruck\ScheduleBundle\Schedule\Task;
17
use Zenstruck\ScheduleBundle\Schedule\Task\Result;
18
use Zenstruck\ScheduleBundle\Tests\Fixture\MockTask;
19
20
/**
21
 * @author Kevin Bond <[email protected]>
22
 */
23
final class TaskTest extends TestCase
24
{
25
    /**
26
     * @test
27
     */
28
    public function can_set_description()
29
    {
30
        $task = self::createTask()->description('my description');
31
32
        $this->assertSame('my description', $task->getDescription());
33
        $this->assertSame('my description', (string) $task);
34
    }
35
36
    /**
37
     * @test
38
     */
39
    public function can_set_timezone()
40
    {
41
        $task = self::createTask();
42
43
        $this->assertNull($task->getTimezone());
44
45
        $task->timezone('UTC');
46
47
        $this->assertSame('UTC', $task->getTimezone()->getName());
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function can_get_next_run()
54
    {
55
        $this->assertSame(
56
            (new \DateTime('1st Jan next year'))->getTimestamp(),
57
            self::createTask()->yearly()->getNextRun()->getTimestamp()
58
        );
59
    }
60
61
    /**
62
     * @test
63
     * @dataProvider frequencyProvider
64
     */
65
    public function can_fluently_create_frequency(callable $createTask, string $expectedExpression)
66
    {
67
        $this->assertSame($expectedExpression, $createTask()->getExpression());
68
    }
69
70
    public static function frequencyProvider(): array
71
    {
72
        return [
73
            [function () { return self::createTask()->daily(); }, '0 0 * * *'],
74
            [function () { return self::createTask()->cron('0 0 * * *')->everyMinute(); }, '* 0 * * *'],
75
            [function () { return self::createTask()->everyFiveMinutes(); }, '*/5 * * * *'],
76
            [function () { return self::createTask()->everyTenMinutes(); }, '*/10 * * * *'],
77
            [function () { return self::createTask()->everyFifteenMinutes(); }, '*/15 * * * *'],
78
            [function () { return self::createTask()->everyThirtyMinutes(); }, '0,30 * * * *'],
79
            [function () { return self::createTask()->hourly(); }, '0 * * * *'],
80
            [function () { return self::createTask()->hourlyAt(6); }, '6 * * * *'],
81
            [function () { return self::createTask()->at('3'); }, '0 3 * * *'],
82
            [function () { return self::createTask()->at('3:16'); }, '16 3 * * *'],
83
            [function () { return self::createTask()->dailyAt('3'); }, '0 3 * * *'],
84
            [function () { return self::createTask()->twiceDaily(); }, '0 1,13 * * *'],
85
            [function () { return self::createTask()->twiceDaily(2, 14); }, '0 2,14 * * *'],
86
            [function () { return self::createTask()->weekdays(); }, '* * * * 1-5'],
87
            [function () { return self::createTask()->weekdays()->at(2); }, '0 2 * * 1-5'],
88
            [function () { return self::createTask()->weekends(); }, '* * * * 0,6'],
89
            [function () { return self::createTask()->mondays(); }, '* * * * 1'],
90
            [function () { return self::createTask()->tuesdays(); }, '* * * * 2'],
91
            [function () { return self::createTask()->wednesdays(); }, '* * * * 3'],
92
            [function () { return self::createTask()->thursdays(); }, '* * * * 4'],
93
            [function () { return self::createTask()->fridays(); }, '* * * * 5'],
94
            [function () { return self::createTask()->saturdays(); }, '* * * * 6'],
95
            [function () { return self::createTask()->sundays(); }, '* * * * 0'],
96
            [function () { return self::createTask()->days(1, 2, 3); }, '* * * * 1,2,3'],
97
            [function () { return self::createTask()->weekly(); }, '0 0 * * 0'],
98
            [function () { return self::createTask()->weekly()->at('3:15'); }, '15 3 * * 0'],
99
            [function () { return self::createTask()->monthly(); }, '0 0 1 * *'],
100
            [function () { return self::createTask()->monthlyOn(3); }, '0 0 3 * *'],
101
            [function () { return self::createTask()->monthlyOn(3, '4:15'); }, '15 4 3 * *'],
102
            [function () { return self::createTask()->twiceMonthly(); }, '0 0 1,16 * *'],
103
            [function () { return self::createTask()->twiceMonthly(3, 17); }, '0 0 3,17 * *'],
104
            [function () { return self::createTask()->twiceMonthly()->at('3:15'); }, '15 3 1,16 * *'],
105
            [function () { return self::createTask()->quarterly(); }, '0 0 1 1-12/3 *'],
106
            [function () { return self::createTask()->yearly(); }, '0 0 1 1 *'],
107
        ];
108
    }
109
110
    /**
111
     * @test
112
     */
113
    public function cannot_set_invalid_cron_expression()
114
    {
115
        $this->expectException(\InvalidArgumentException::class);
116
        $this->expectExceptionMessage('"* *" is an invalid cron expression.');
117
118
        self::createTask()->cron('* *');
119
    }
120
121
    /**
122
     * @test
123
     */
124
    public function has_unique_id_based_on_description_and_frequency()
125
    {
126
        $this->assertSame(self::createTask()->getId(), self::createTask()->getId());
127
        $this->assertNotSame(self::createTask()->daily()->getId(), self::createTask()->getId());
128
        $this->assertNotSame(self::createTask('task1')->getId(), self::createTask('task2')->getId());
129
        $this->assertNotSame((new class('task') extends Task {
130
        })->getId(), self::createTask('task')->getId());
131
    }
132
133
    /**
134
     * @test
135
     */
136
    public function false_when_filter_skips_task()
137
    {
138
        $task = self::createTask();
139
140
        $task->when('boolean value', false);
141
142
        $this->expectException(SkipTask::class);
143
        $this->expectExceptionMessage('boolean value');
144
145
        $task->getExtensions()[0]->filterTask(new BeforeTaskEvent(new BeforeScheduleEvent(new Schedule()), $task));
0 ignored issues
show
Bug introduced by
The method filterTask() 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

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

265
        $task->getExtensions()[1]->/** @scrutinizer ignore-call */ 
266
                                   beforeTask($event);
Loading history...
266
        $task->getExtensions()[2]->afterTask($event = new AfterTaskEvent($event, Result::successful($task)));
0 ignored issues
show
Bug introduced by
The method afterTask() 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
        $task->getExtensions()[2]->/** @scrutinizer ignore-call */ 
267
                                   afterTask($event = new AfterTaskEvent($event, Result::successful($task)));
Loading history...
267
        $task->getExtensions()[3]->afterTask($event);
268
        $task->getExtensions()[4]->onTaskSuccess($event);
0 ignored issues
show
Bug introduced by
The method onTaskSuccess() 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

268
        $task->getExtensions()[4]->/** @scrutinizer ignore-call */ 
269
                                   onTaskSuccess($event);
Loading history...
269
        $task->getExtensions()[5]->onTaskFailure($event);
0 ignored issues
show
Bug introduced by
The method onTaskFailure() 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

269
        $task->getExtensions()[5]->/** @scrutinizer ignore-call */ 
270
                                   onTaskFailure($event);
Loading history...
270
271
        $this->assertSame([
272
            'filter',
273
            'before',
274
            'after',
275
            'then',
276
            'onSuccess',
277
            'onFailure',
278
        ], $calls);
279
    }
280
281
    /**
282
     * @test
283
     */
284
    public function can_add_ping_extensions()
285
    {
286
        $task = self::createTask();
287
288
        $task->pingBefore('http://before.com');
289
        $task->pingAfter('http://after.com', 'POST');
290
        $task->thenPing('http://then.com');
291
        $task->pingOnSuccess('http://success.com');
292
        $task->pingOnFailure('http://failure.com');
293
294
        $client = $this->createMock(HttpClientInterface::class);
295
        $client->expects($this->exactly(5))->method('request')->withConsecutive(
296
            [$this->equalTo('GET'), $this->equalTo('http://before.com'), $this->isType('array')],
297
            [$this->equalTo('POST'), $this->equalTo('http://after.com'), $this->isType('array')],
298
            [$this->equalTo('GET'), $this->equalTo('http://then.com'), $this->isType('array')],
299
            [$this->equalTo('GET'), $this->equalTo('http://success.com'), $this->isType('array')],
300
            [$this->equalTo('GET'), $this->equalTo('http://failure.com'), $this->isType('array')]
301
        );
302
303
        $task->getExtensions()[0]->setHttpClient($client)->beforeTask($event = new BeforeTaskEvent(new BeforeScheduleEvent(new Schedule()), $task));
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

303
        $task->getExtensions()[0]->/** @scrutinizer ignore-call */ 
304
                                   setHttpClient($client)->beforeTask($event = new BeforeTaskEvent(new BeforeScheduleEvent(new Schedule()), $task));
Loading history...
304
        $task->getExtensions()[1]->setHttpClient($client)->afterTask($event = new AfterTaskEvent($event, Result::successful($task)));
305
        $task->getExtensions()[2]->setHttpClient($client)->afterTask($event);
306
        $task->getExtensions()[3]->setHttpClient($client)->onTaskSuccess($event);
307
        $task->getExtensions()[4]->setHttpClient($client)->onTaskFailure($event);
308
    }
309
310
    /**
311
     * @test
312
     * @dataProvider emailAfterMethodProvider
313
     */
314
    public function can_add_email_after_extension($method)
315
    {
316
        $task = self::createTask();
317
        $task->{$method}('[email protected]', 'my subject', function (Email $email) {
318
            $email->cc('[email protected]');
319
        });
320
321
        $this->assertTrue($task->getExtensions()[0]->isHook(Extension::TASK_AFTER));
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

321
        $this->assertTrue($task->getExtensions()[0]->/** @scrutinizer ignore-call */ isHook(Extension::TASK_AFTER));
Loading history...
322
        $this->assertSame('[email protected]', $task->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

322
        $this->assertSame('[email protected]', $task->getExtensions()[0]->/** @scrutinizer ignore-call */ getEmail()->getTo()[0]->toString());
Loading history...
323
        $this->assertSame('[email protected]', $task->getExtensions()[0]->getEmail()->getCc()[0]->toString());
324
        $this->assertSame('my subject', $task->getExtensions()[0]->getEmail()->getSubject());
325
    }
326
327
    public static function emailAfterMethodProvider()
328
    {
329
        return [
330
            ['emailAfter'],
331
            ['thenEmail'],
332
        ];
333
    }
334
335
    /**
336
     * @test
337
     */
338
    public function can_add_email_on_failure_extension()
339
    {
340
        $task = self::createTask();
341
        $task->emailOnFailure('[email protected]', 'my subject', function (Email $email) {
342
            $email->cc('[email protected]');
343
        });
344
345
        $this->assertTrue($task->getExtensions()[0]->isHook(Extension::TASK_FAILURE));
346
        $this->assertSame('[email protected]', $task->getExtensions()[0]->getEmail()->getTo()[0]->toString());
347
        $this->assertSame('[email protected]', $task->getExtensions()[0]->getEmail()->getCc()[0]->toString());
348
        $this->assertSame('my subject', $task->getExtensions()[0]->getEmail()->getSubject());
349
    }
350
351
    /**
352
     * @test
353
     */
354
    public function can_add_single_server_extension()
355
    {
356
        $task1 = self::createTask('task')->onSingleServer();
357
        $task2 = self::createTask('task')->onSingleServer();
358
359
        $lockFactory = new LockFactory(new FlockStore());
360
361
        $task1->getExtensions()[0]->aquireTaskLock($lockFactory, $task1, \time());
0 ignored issues
show
Bug introduced by
The method aquireTaskLock() 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

361
        $task1->getExtensions()[0]->/** @scrutinizer ignore-call */ 
362
                                    aquireTaskLock($lockFactory, $task1, \time());
Loading history...
362
363
        $this->expectException(SkipTask::class);
364
        $this->expectExceptionMessage('Task running on another server.');
365
366
        $task2->getExtensions()[0]->aquireTaskLock($lockFactory, $task2, \time());
367
    }
368
369
    /**
370
     * @test
371
     */
372
    public function can_add_without_overlapping_extension()
373
    {
374
        $task1 = self::createTask('task')->withoutOverlapping();
375
        $task2 = self::createTask('task')->withoutOverlapping();
376
377
        $task1->getExtensions()[0]->filterTask(new BeforeTaskEvent(new BeforeScheduleEvent(new Schedule()), $task1));
378
379
        $this->expectException(SkipTask::class);
380
        $this->expectExceptionMessage('Task running in another process.');
381
382
        $task2->getExtensions()[0]->filterTask(new BeforeTaskEvent(new BeforeScheduleEvent(new Schedule()), $task2));
383
    }
384
385
    /**
386
     * @test
387
     * @dataProvider betweenExtensionSkipProvider
388
     */
389
    public function between_extension_skip($start, $end, $inclusive)
390
    {
391
        $start = (new \DateTime($start))->format('H:i');
392
        $end = (new \DateTime($end))->format('H:i');
393
394
        $task = self::createTask()->between($start, $end, $inclusive);
395
396
        $this->expectException(SkipTask::class);
397
        $this->expectExceptionMessage("Only runs between {$start} and {$end}");
398
399
        $task->getExtensions()[0]->filterTask(new BeforeTaskEvent(new BeforeScheduleEvent(new Schedule()), $task));
400
    }
401
402
    public static function betweenExtensionSkipProvider()
403
    {
404
        return [
405
            ['+2 minutes', '+3 minutes', true],
406
            ['now', '+3 minutes', false],
407
            ['+5 minutes', '+23 hours', true],
408
        ];
409
    }
410
411
    /**
412
     * @test
413
     * @dataProvider betweenExtensionRunProvider
414
     */
415
    public function between_extension_run($start, $end, $inclusive)
416
    {
417
        $start = (new \DateTime($start))->format('H:i');
418
        $end = (new \DateTime($end))->format('H:i');
419
420
        $task = self::createTask()->between($start, $end, $inclusive);
421
422
        $task->getExtensions()[0]->filterTask(new BeforeTaskEvent(new BeforeScheduleEvent(new Schedule()), $task));
423
424
        $this->assertTrue(true);
425
    }
426
427
    public static function betweenExtensionRunProvider()
428
    {
429
        return [
430
            ['now', '+3 minutes', true],
431
            ['-1 minute', '+3 minutes', false],
432
            ['-1 minutes', '+23 hours', true],
433
        ];
434
    }
435
436
    /**
437
     * @test
438
     * @dataProvider unlessBetweenExtensionSkipProvider
439
     */
440
    public function unless_between_extension_skip($start, $end, $inclusive)
441
    {
442
        $start = (new \DateTime($start))->format('H:i');
443
        $end = (new \DateTime($end))->format('H:i');
444
445
        $task = self::createTask()->unlessBetween($start, $end, $inclusive);
446
447
        $this->expectException(SkipTask::class);
448
        $this->expectExceptionMessage("Only runs if not between {$start} and {$end}");
449
450
        $task->getExtensions()[0]->filterTask(new BeforeTaskEvent(new BeforeScheduleEvent(new Schedule()), $task));
451
    }
452
453
    public static function unlessBetweenExtensionSkipProvider()
454
    {
455
        return [
456
            ['-1 minute', '+3 minutes', false],
457
            ['now', '+3 minutes', true],
458
            ['-1 minutes', '+23 hours', true],
459
        ];
460
    }
461
462
    /**
463
     * @test
464
     * @dataProvider unlessBetweenExtensionRunProvider
465
     */
466
    public function unless_between_extension_run($start, $end, $inclusive)
467
    {
468
        $start = (new \DateTime($start))->format('H:i');
469
        $end = (new \DateTime($end))->format('H:i');
470
471
        $task = self::createTask()->unlessBetween($start, $end, $inclusive);
472
473
        $task->getExtensions()[0]->filterTask(new BeforeTaskEvent(new BeforeScheduleEvent(new Schedule()), $task));
474
475
        $this->assertTrue(true);
476
    }
477
478
    public static function unlessBetweenExtensionRunProvider()
479
    {
480
        return [
481
            ['now', '+3 minutes', false],
482
            ['+1 minute', '+3 minutes', true],
483
            ['+5 minutes', '+23 hours', true],
484
        ];
485
    }
486
487
    private static function createTask(string $description = 'task description'): Task
488
    {
489
        return new MockTask($description);
490
    }
491
}
492