Passed
Pull Request — master (#2)
by Kevin
01:48
created

ConfigureTasksTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
eloc 107
c 1
b 0
f 1
dl 0
loc 187
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A can_configure_process_tasks() 0 14 1
A full_task_configuration() 0 53 1
A can_configure_compound_task() 0 28 1
A createSchedule() 0 9 1
A can_configure_compound_task_with_descriptions() 0 28 1
A minimal_task_configuration() 0 28 1
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->assertNull($task1->getTimezone());
42
        $this->assertSame('0 * * * *', $task1->getExpression());
43
        $this->assertCount(0, $task1->getExtensions());
44
45
        $this->assertInstanceOf(CommandTask::class, $task2);
46
        $this->assertSame('another:command', $task2->getDescription());
47
        $this->assertNull($task1->getTimezone());
48
        $this->assertSame('0 0 * * *', $task2->getExpression());
49
        $this->assertCount(0, $task2->getExtensions());
50
    }
51
52
    /**
53
     * @test
54
     */
55
    public function can_configure_process_tasks()
56
    {
57
        $schedule = $this->createSchedule([
58
            [
59
                'command' => 'bash: /bin/script',
60
                'frequency' => '0 * * * *',
61
            ],
62
        ]);
63
64
        $this->assertCount(1, $schedule->all());
65
        $this->assertInstanceOf(ProcessTask::class, $schedule->all()[0]);
66
        $this->assertSame('/bin/script', $schedule->all()[0]->getDescription());
67
        $this->assertSame('0 * * * *', $schedule->all()[0]->getExpression());
68
        $this->assertCount(0, $schedule->all()[0]->getExtensions());
69
    }
70
71
    /**
72
     * @test
73
     */
74
    public function can_configure_compound_task()
75
    {
76
        $schedule = $this->createSchedule([
77
            [
78
                'command' => [
79
                    'my:command arg --option=foo',
80
                    'bash:/my-script',
81
                ],
82
                'frequency' => '0 * * * *',
83
                'without_overlapping' => null,
84
            ],
85
        ]);
86
87
        $this->assertCount(2, $schedule->all());
88
89
        [$task1, $task2] = $schedule->all();
90
91
        $this->assertInstanceOf(CommandTask::class, $task1);
92
        $this->assertSame('my:command', $task1->getDescription());
93
        $this->assertSame('0 * * * *', $task1->getExpression());
94
        $this->assertCount(1, $task1->getExtensions());
95
        $this->assertSame('Without overlapping', (string) $task1->getExtensions()[0]);
96
97
        $this->assertInstanceOf(ProcessTask::class, $task2);
98
        $this->assertSame('/my-script', $task2->getDescription());
99
        $this->assertSame('0 * * * *', $task2->getExpression());
100
        $this->assertCount(1, $task2->getExtensions());
101
        $this->assertSame('Without overlapping', (string) $task2->getExtensions()[0]);
102
    }
103
104
    /**
105
     * @test
106
     */
107
    public function can_configure_compound_task_with_descriptions()
108
    {
109
        $schedule = $this->createSchedule([
110
            [
111
                'command' => [
112
                    'my command' => 'my:command arg --option=foo',
113
                    'another command' => 'bash:/my-script',
114
                ],
115
                'frequency' => '0 * * * *',
116
                'without_overlapping' => null,
117
            ],
118
        ]);
119
120
        $this->assertCount(2, $schedule->all());
121
122
        [$task1, $task2] = $schedule->all();
123
124
        $this->assertInstanceOf(CommandTask::class, $task1);
125
        $this->assertSame('my command', $task1->getDescription());
126
        $this->assertSame('0 * * * *', $task1->getExpression());
127
        $this->assertCount(1, $task1->getExtensions());
128
        $this->assertSame('Without overlapping', (string) $task1->getExtensions()[0]);
129
130
        $this->assertInstanceOf(ProcessTask::class, $task2);
131
        $this->assertSame('another command', $task2->getDescription());
132
        $this->assertSame('0 * * * *', $task2->getExpression());
133
        $this->assertCount(1, $task2->getExtensions());
134
        $this->assertSame('Without overlapping', (string) $task2->getExtensions()[0]);
135
    }
136
137
    /**
138
     * @test
139
     */
140
    public function full_task_configuration()
141
    {
142
        $schedule = $this->createSchedule([
143
            [
144
                'command' => 'my:command --option',
145
                'frequency' => '0 0 * * *',
146
                'description' => 'my description',
147
                'timezone' => 'UTC',
148
                'without_overlapping' => null,
149
                'between' => [
150
                    'start' => 9,
151
                    'end' => 17,
152
                ],
153
                'unless_between' => [
154
                    'start' => 12,
155
                    'end' => '13:30',
156
                ],
157
                'ping_before' => [
158
                    'url' => 'https://example.com/before',
159
                ],
160
                'ping_after' => [
161
                    'url' => 'https://example.com/after',
162
                ],
163
                'ping_on_success' => [
164
                    'url' => 'https://example.com/success',
165
                ],
166
                'ping_on_failure' => [
167
                    'url' => 'https://example.com/failure',
168
                    'method' => 'POST',
169
                ],
170
                'email_after' => null,
171
                'email_on_failure' => [
172
                    'to' => '[email protected]',
173
                    'subject' => 'my subject',
174
                ],
175
            ],
176
        ]);
177
178
        $task = $schedule->all()[0];
179
        $extensions = $task->getExtensions();
180
181
        $this->assertSame('my description', $task->getDescription());
182
        $this->assertSame('UTC', $task->getTimezone()->getName());
183
        $this->assertCount(9, $extensions);
184
        $this->assertSame('Without overlapping', (string) $extensions[0]);
185
        $this->assertSame('Only run between 9:00 and 17:00', (string) $extensions[1]);
186
        $this->assertSame('Only run if not between 12:00 and 13:30', (string) $extensions[2]);
187
        $this->assertSame('Before Task, ping "https://example.com/before"', (string) $extensions[3]);
188
        $this->assertSame('After Task, ping "https://example.com/after"', (string) $extensions[4]);
189
        $this->assertSame('On Task Success, ping "https://example.com/success"', (string) $extensions[5]);
190
        $this->assertSame('On Task Failure, ping "https://example.com/failure"', (string) $extensions[6]);
191
        $this->assertSame('After Task, email output', (string) $extensions[7]);
192
        $this->assertSame('On Task Failure, email output to "[email protected]"', (string) $extensions[8]);
193
    }
194
195
    private function createSchedule(array $taskConfig): Schedule
196
    {
197
        $processor = new Processor();
198
        $config = $processor->processConfiguration(new Configuration(), [['tasks' => $taskConfig]]);
199
200
        return (new MockScheduleBuilder())
201
            ->addSubscriber(new ConfigureTasksSubscriber($config['tasks']))
202
            ->getRunner()
203
            ->buildSchedule()
204
        ;
205
    }
206
}
207