Passed
Push — master ( cbdf83...8b3ece )
by Kevin
02:05
created

CronExpressionTest::standardExpressionProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 11
rs 9.9666
nc 1
nop 0
cc 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 Zenstruck\ScheduleBundle\Schedule\CronExpression;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
final class CronExpressionTest extends TestCase
12
{
13
    /**
14
     * @test
15
     * @dataProvider standardExpressionProvider
16
     */
17
    public function can_handle_standard_expressions($expression)
18
    {
19
        $expressionA = new CronExpression($expression, 'my task');
20
        $expressionB = new CronExpression($expression, 'my task');
21
        $expressionC = new CronExpression($expression, 'another task');
22
23
        $this->assertFalse($expressionA->isHashed());
24
        $this->assertSame($expression, $expressionA->getRawValue());
25
        $this->assertSame($expression, $expressionA->getParsedValue());
26
        $this->assertSame((string) $expressionA, $expressionA->getParsedValue());
27
        $this->assertSame((string) $expressionA, (string) $expressionB);
28
        $this->assertSame((string) $expressionA, (string) $expressionC);
29
        $this->assertInstanceOf(\DateTimeInterface::class, $expressionA->getNextRun());
30
    }
31
32
    public static function standardExpressionProvider(): array
33
    {
34
        return [
35
            ['0 * * * *'],
36
            ['0 0 * * 1'],
37
            ['@hourly'],
38
            ['@daily'],
39
            ['@weekly'],
40
            ['@monthly'],
41
            ['@yearly'],
42
            ['@annually'],
43
        ];
44
    }
45
46
    /**
47
     * @test
48
     * @dataProvider hashedExpressionProvider
49
     */
50
    public function can_handle_hashed_expressions($value, $expected)
51
    {
52
        $expressionA = new CronExpression($value, 'my task');
53
        $expressionB = new CronExpression($value, 'my task');
54
        $expressionC = new CronExpression($value, 'another task');
55
56
        $this->assertTrue($expressionA->isHashed());
57
        $this->assertSame($value, $expressionA->getRawValue());
58
        $this->assertSame((string) $expressionA, $expressionA->getParsedValue());
59
        $this->assertSame((string) $expressionA, (string) $expressionB);
60
        $this->assertNotSame((string) $expressionA, (string) $expressionC);
61
        $this->assertSame($expected, (string) $expressionA);
62
        $this->assertInstanceOf(\DateTimeInterface::class, $expressionA->getNextRun());
63
    }
64
65
    public static function hashedExpressionProvider(): array
66
    {
67
        return [
68
            ['# * * * *', '56 * * * *'],
69
            ['# # * * *', '56 20 * * *'],
70
            ['# # # * *', '56 20 1 * *'],
71
            ['# # # # *', '56 20 1 9 *'],
72
            ['# # # # #', '56 20 1 9 0'],
73
            ['# # 1,15 1-11 *', '56 20 1,15 1-11 *'],
74
            ['# # 1,15 * *', '56 20 1,15 * *'],
75
            ['#hourly', '56 * * * *'],
76
            ['#daily', '56 20 * * *'],
77
            ['#weekly', '56 20 * * 0'],
78
            ['#monthly', '56 20 1 * *'],
79
            ['#yearly', '56 20 1 9 *'],
80
            ['#annually', '56 20 1 9 *'],
81
            ['#midnight', '56 2 * * *'],
82
            ['#(1-15) * * * *', '12 * * * *'],
83
            ['#(1-15) * * * #(3-5)', '12 * * * 5'],
84
            ['#(1-15) * # * #(3-5)', '12 * 1 * 5'],
85
        ];
86
    }
87
88
    /**
89
     * @test
90
     * @dataProvider invalidExpressionProvider
91
     */
92
    public function cannot_set_invalid_cron_expression($value)
93
    {
94
        $this->expectException(\InvalidArgumentException::class);
95
        $this->expectExceptionMessage("\"{$value}\" is an invalid cron expression.");
96
97
        new CronExpression($value, 'context');
98
    }
99
100
    public static function invalidExpressionProvider(): array
101
    {
102
        return [
103
            ['* *'],
104
            ['*****'],
105
            ['* * * * * *'],
106
            ['daily'],
107
            ['# daily'],
108
            ['#everyday'],
109
            ['@ daily'],
110
        ];
111
    }
112
}
113