Passed
Pull Request — master (#2)
by Kevin
03:06 queued 01:22
created

ConfigureTasksTest::can_configure_process_tasks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 14
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Tests\Functional;
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\Config\Definition\Processor;
7
use Zenstruck\ScheduleBundle\DependencyInjection\Configuration;
8
use Zenstruck\ScheduleBundle\EventListener\ConfigureTasksSubscriber;
9
use Zenstruck\ScheduleBundle\Schedule;
10
use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask;
11
use Zenstruck\ScheduleBundle\Schedule\Task\ProcessTask;
12
use Zenstruck\ScheduleBundle\Tests\Fixture\MockScheduleBuilder;
13
14
/**
15
 * @author Kevin Bond <[email protected]>
16
 */
17
final class ConfigureTasksTest extends TestCase
18
{
19
    /**
20
     * @test
21
     */
22
    public function minimal_task_configuration()
23
    {
24
        $schedule = $this->createSchedule([
25
            [
26
                'command' => 'my:command',
27
                'frequency' => '0 * * * *',
28
            ],
29
            [
30
                'command' => 'another:command',
31
                'frequency' => '0 0 * * *',
32
            ],
33
        ]);
34
35
        $this->assertCount(2, $schedule->all());
36
37
        [$task1, $task2] = $schedule->all();
38
39
        $this->assertInstanceOf(CommandTask::class, $task1);
40
        $this->assertSame('my:command', $task1->getDescription());
41
        $this->assertSame('0 * * * *', $task1->getExpression());
42
        $this->assertCount(0, $task1->getExtensions());
43
44
        $this->assertInstanceOf(CommandTask::class, $task2);
45
        $this->assertSame('another:command', $task2->getDescription());
46
        $this->assertSame('0 0 * * *', $task2->getExpression());
47
        $this->assertCount(0, $task2->getExtensions());
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function can_configure_process_tasks()
54
    {
55
        $schedule = $this->createSchedule([
56
            [
57
                'command' => 'bash: /bin/script',
58
                'frequency' => '0 * * * *',
59
            ],
60
        ]);
61
62
        $this->assertCount(1, $schedule->all());
63
        $this->assertInstanceOf(ProcessTask::class, $schedule->all()[0]);
64
        $this->assertSame('/bin/script', $schedule->all()[0]->getDescription());
65
        $this->assertSame('0 * * * *', $schedule->all()[0]->getExpression());
66
        $this->assertCount(0, $schedule->all()[0]->getExtensions());
67
    }
68
69
    /**
70
     * @test
71
     */
72
    public function can_configure_compound_task()
73
    {
74
        $schedule = $this->createSchedule([
75
            [
76
                'command' => [
77
                    'my:command arg --option=foo',
78
                    'bash:/my-script',
79
                ],
80
                'frequency' => '0 * * * *',
81
                'without_overlapping' => null,
82
            ],
83
        ]);
84
85
        $this->assertCount(2, $schedule->all());
86
87
        [$task1, $task2] = $schedule->all();
88
89
        $this->assertInstanceOf(CommandTask::class, $task1);
90
        $this->assertSame('my:command', $task1->getDescription());
91
        $this->assertSame('0 * * * *', $task1->getExpression());
92
        $this->assertCount(1, $task1->getExtensions());
93
        $this->assertSame('Without overlapping', (string) $task1->getExtensions()[0]);
94
95
        $this->assertInstanceOf(ProcessTask::class, $task2);
96
        $this->assertSame('/my-script', $task2->getDescription());
97
        $this->assertSame('0 * * * *', $task2->getExpression());
98
        $this->assertCount(1, $task2->getExtensions());
99
        $this->assertSame('Without overlapping', (string) $task2->getExtensions()[0]);
100
    }
101
102
    /**
103
     * @test
104
     */
105
    public function can_configure_compound_task_with_descriptions()
106
    {
107
        $schedule = $this->createSchedule([
108
            [
109
                'command' => [
110
                    'my command' => 'my:command arg --option=foo',
111
                    'another command' => 'bash:/my-script',
112
                ],
113
                'frequency' => '0 * * * *',
114
                'without_overlapping' => null,
115
            ],
116
        ]);
117
118
        $this->assertCount(2, $schedule->all());
119
120
        [$task1, $task2] = $schedule->all();
121
122
        $this->assertInstanceOf(CommandTask::class, $task1);
123
        $this->assertSame('my command', $task1->getDescription());
124
        $this->assertSame('0 * * * *', $task1->getExpression());
125
        $this->assertCount(1, $task1->getExtensions());
126
        $this->assertSame('Without overlapping', (string) $task1->getExtensions()[0]);
127
128
        $this->assertInstanceOf(ProcessTask::class, $task2);
129
        $this->assertSame('another command', $task2->getDescription());
130
        $this->assertSame('0 * * * *', $task2->getExpression());
131
        $this->assertCount(1, $task2->getExtensions());
132
        $this->assertSame('Without overlapping', (string) $task2->getExtensions()[0]);
133
    }
134
135
    /**
136
     * @test
137
     */
138
    public function full_task_configuration()
139
    {
140
        $schedule = $this->createSchedule([
141
            [
142
                'command' => 'my:command --option',
143
                'frequency' => '0 0 * * *',
144
                'description' => 'my description',
145
                'without_overlapping' => null,
146
                'between' => [
147
                    'start' => 9,
148
                    'end' => 17,
149
                ],
150
                'unless_between' => [
151
                    'start' => 12,
152
                    'end' => '13:30',
153
                ],
154
                'ping_before' => [
155
                    'url' => 'https://example.com/before',
156
                ],
157
                'ping_after' => [
158
                    'url' => 'https://example.com/after',
159
                ],
160
                'ping_on_success' => [
161
                    'url' => 'https://example.com/success',
162
                ],
163
                'ping_on_failure' => [
164
                    'url' => 'https://example.com/failure',
165
                    'method' => 'POST',
166
                ],
167
                'email_after' => null,
168
                'email_on_failure' => [
169
                    'to' => '[email protected]',
170
                    'subject' => 'my subject',
171
                ],
172
            ],
173
        ]);
174
175
        $task = $schedule->all()[0];
176
        $extensions = $task->getExtensions();
177
178
        $this->assertSame('my description', $task->getDescription());
179
        $this->assertCount(9, $extensions);
180
        $this->assertSame('Without overlapping', (string) $extensions[0]);
181
        $this->assertSame('Only run between 9:00 and 17:00', (string) $extensions[1]);
182
        $this->assertSame('Only run if not between 12:00 and 13:30', (string) $extensions[2]);
183
        $this->assertSame('Before Task, ping "https://example.com/before"', (string) $extensions[3]);
184
        $this->assertSame('After Task, ping "https://example.com/after"', (string) $extensions[4]);
185
        $this->assertSame('On Task Success, ping "https://example.com/success"', (string) $extensions[5]);
186
        $this->assertSame('On Task Failure, ping "https://example.com/failure"', (string) $extensions[6]);
187
        $this->assertSame('After Task, email output', (string) $extensions[7]);
188
        $this->assertSame('On Task Failure, email output to "[email protected]"', (string) $extensions[8]);
189
    }
190
191
    private function createSchedule(array $taskConfig): Schedule
192
    {
193
        $processor = new Processor();
194
        $config = $processor->processConfiguration(new Configuration(), [['tasks' => $taskConfig]]);
195
196
        return (new MockScheduleBuilder())
197
            ->addSubscriber(new ConfigureTasksSubscriber($config['tasks']))
198
            ->getRunner()
199
            ->buildSchedule()
200
        ;
201
    }
202
}
203