Passed
Pull Request — master (#8)
by Kevin
22:03
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 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 27
rs 9.568
eloc 22
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\Mime\Email;
7
use Symfony\Component\Process\Process;
8
use Zenstruck\ScheduleBundle\Schedule;
9
use Zenstruck\ScheduleBundle\Schedule\Extension\SingleServerExtension;
10
use Zenstruck\ScheduleBundle\Schedule\Task;
11
use Zenstruck\ScheduleBundle\Schedule\Task\CallbackTask;
12
use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask;
13
use Zenstruck\ScheduleBundle\Tests\Fixture\MockTask;
14
15
/**
16
 * @author Kevin Bond <[email protected]>
17
 */
18
class ScheduleTest extends TestCase
19
{
20
    /**
21
     * @test
22
     */
23
    public function can_add_tasks()
24
    {
25
        $schedule = new Schedule();
26
27
        $schedule->add(new CallbackTask(function () {}))->description('task1');
28
        $schedule->addCallback(function () {})->description('task2');
29
        $schedule->addProcess('php -v')->description('task3');
30
        $schedule->addProcess(new Process(['php -v']))->description('task4');
31
        $schedule->addCommand('my:command')->description('task5');
32
33
        $this->assertCount(5, $schedule->all());
34
        $this->assertSame(['task1', 'task2', 'task3', 'task4', 'task5'], \array_map(
35
            function (Task $task) {
36
                return $task->getDescription();
37
            },
38
            $schedule->all()
39
        ));
40
41
        $this->assertCount(5, $schedule->all(), 'Caches the tasks');
42
43
        $schedule->addCommand('another:command');
44
45
        $this->assertCount(6, $schedule->all(), 'Resets the task cache on add');
46
    }
47
48
    /**
49
     * @test
50
     */
51
    public function can_add_compound_tasks()
52
    {
53
        $schedule = new Schedule();
54
55
        $schedule->addCommand('my:command')->description('task1')->tuesdays();
56
        $schedule->addCompound()
57
            ->addCommand('another:command', [], 'task2')
58
            ->addCallback(function () {}, 'task3')
59
            ->addProcess('php -v', 'task4')
60
            ->addProcess(new Process(['php -v']), 'task5')
61
            ->add((new CommandTask('yet:another:command'))
62
                ->description('task6')
63
                ->sundays()
64
                ->timezone('America/Los_Angeles')
65
            )
66
            ->timezone('UTC')
67
            ->mondays()
68
            ->onSingleServer()
69
        ;
70
71
        $this->assertCount(6, $schedule->all());
72
        $this->assertSame('task1', $schedule->all()[0]->getDescription());
73
        $this->assertSame('* * * * 2', (string) $schedule->all()[0]->getExpression());
74
        $this->assertNull($schedule->all()[0]->getTimezone());
75
        $this->assertCount(0, $schedule->all()[0]->getExtensions());
76
        $this->assertSame('task2', $schedule->all()[1]->getDescription());
77
        $this->assertSame('* * * * 1', (string) $schedule->all()[1]->getExpression());
78
        $this->assertSame('UTC', $schedule->all()[1]->getTimezone()->getName());
79
        $this->assertCount(1, $schedule->all()[1]->getExtensions());
80
        $this->assertSame('task3', $schedule->all()[2]->getDescription());
81
        $this->assertSame('* * * * 1', (string) $schedule->all()[2]->getExpression());
82
        $this->assertSame('UTC', $schedule->all()[2]->getTimezone()->getName());
83
        $this->assertCount(1, $schedule->all()[2]->getExtensions());
84
        $this->assertSame('task4', $schedule->all()[3]->getDescription());
85
        $this->assertSame('* * * * 1', (string) $schedule->all()[3]->getExpression());
86
        $this->assertSame('UTC', $schedule->all()[3]->getTimezone()->getName());
87
        $this->assertCount(1, $schedule->all()[3]->getExtensions());
88
        $this->assertSame('task5', $schedule->all()[4]->getDescription());
89
        $this->assertSame('* * * * 1', (string) $schedule->all()[4]->getExpression());
90
        $this->assertSame('UTC', $schedule->all()[4]->getTimezone()->getName());
91
        $this->assertCount(1, $schedule->all()[4]->getExtensions());
92
        $this->assertSame('task6', $schedule->all()[5]->getDescription());
93
        $this->assertSame('* * * * 1', (string) $schedule->all()[5]->getExpression());
94
        $this->assertSame('UTC', $schedule->all()[5]->getTimezone()->getName());
95
        $this->assertCount(1, $schedule->all()[5]->getExtensions());
96
    }
97
98
    /**
99
     * @test
100
     */
101
    public function can_get_due_tasks()
102
    {
103
        $schedule = new Schedule();
104
105
        $schedule->addCallback(function () {})->description('task1');
106
        $notDueTask = $schedule->addProcess('php -v')->description('task2')->sundays();
107
108
        if ('Sun' === \date('D')) {
109
            $notDueTask->mondays();
110
        }
111
112
        $this->assertCount(2, $schedule->all());
113
        $this->assertCount(1, $schedule->due(new \DateTime()));
114
        $this->assertCount(1, $schedule->due(new \DateTime()), 'Due tasks are cached');
115
        $this->assertSame('task1', $schedule->due(new \DateTime())[0]->getDescription());
116
117
        $schedule->addCommand('my:command')->description('task3');
118
119
        $this->assertCount(2, $schedule->due(new \DateTime()), 'Resets the due task cache');
120
    }
121
122
    /**
123
     * @test
124
     */
125
    public function due_tasks_are_ordered_by_when_they_are_defined()
126
    {
127
        $schedule = new Schedule();
128
129
        $schedule->addCommand('my:command')->description('task1');
130
        $schedule->addCompound()
131
            ->addCommand('another:command', [], 'task2')
132
            ->addCallback(function () {}, 'task3')
133
            ->addProcess('php -v', 'task4')
134
            ->addProcess(new Process(['php -v']), 'task5')
135
            ->onSingleServer()
136
        ;
137
        $schedule->addCommand('my:command')->description('task6');
138
139
        $this->assertSame(
140
            [
141
                'task1',
142
                'task2',
143
                'task3',
144
                'task4',
145
                'task5',
146
                'task6',
147
            ], \array_map(function (Task $task) {
148
                return $task->getDescription();
149
            }, $schedule->due(new \DateTime()))
150
        );
151
    }
152
153
    /**
154
     * @test
155
     */
156
    public function has_unique_id_based_on_tasks()
157
    {
158
        $schedule1 = new Schedule();
159
        $schedule1->addCommand('my:command');
160
        $schedule2 = new Schedule();
161
        $schedule2->addCommand('my:command');
162
        $schedule3 = new Schedule();
163
        $schedule3->addCommand('another:command');
164
        $schedule4 = new Schedule();
165
        $schedule4->addCommand('my:command');
166
        $schedule4->addCommand('another:command');
167
168
        $this->assertSame((new Schedule())->getId(), (new Schedule())->getId());
169
        $this->assertSame($schedule1->getId(), $schedule2->getId());
170
        $this->assertNotSame($schedule2->getId(), $schedule3->getId());
171
        $this->assertNotSame($schedule2->getId(), $schedule4->getId());
172
    }
173
174
    /**
175
     * @test
176
     */
177
    public function can_add_single_server_extension()
178
    {
179
        $schedule = new Schedule();
180
        $schedule->addCommand('my:command');
181
        $schedule->onSingleServer();
182
183
        $this->assertInstanceOf(SingleServerExtension::class, $schedule->getExtensions()[0]);
184
    }
185
186
    /**
187
     * @test
188
     */
189
    public function can_add_email_on_failure_extension()
190
    {
191
        $schedule = new Schedule();
192
        $schedule->emailOnFailure('[email protected]', 'my subject', function (Email $email) {
193
            $email->cc('[email protected]');
194
        });
195
196
        $this->assertTrue($schedule->getExtensions()[0]->isHook(Schedule::FAILURE));
197
        $this->assertSame('[email protected]', $schedule->getExtensions()[0]->getEmail()->getTo()[0]->toString());
198
        $this->assertSame('[email protected]', $schedule->getExtensions()[0]->getEmail()->getCc()[0]->toString());
199
        $this->assertSame('my subject', $schedule->getExtensions()[0]->getEmail()->getSubject());
200
    }
201
202
    /**
203
     * @test
204
     */
205
    public function can_add_environment_extension()
206
    {
207
        $schedule = new Schedule();
208
        $schedule->environments('prod', 'stage');
209
210
        $this->assertSame(['prod', 'stage'], $schedule->getExtensions()[0]->getRunEnvironments());
211
    }
212
213
    /**
214
     * @test
215
     */
216
    public function can_set_timezone()
217
    {
218
        $schedule = new Schedule();
219
        $schedule->add((new MockTask())->description('task1'));
220
        $schedule->add((new MockTask())->description('task2')->timezone('America/Toronto'));
221
222
        $this->assertNull($schedule->getTimezone());
223
        $this->assertNull($schedule->all()[0]->getTimezone());
224
        $this->assertNull($schedule->due(new \DateTime())[0]->getTimezone());
225
        $this->assertSame('America/Toronto', $schedule->all()[1]->getTimezone()->getName());
226
        $this->assertSame('America/Toronto', $schedule->due(new \DateTime())[1]->getTimezone()->getName());
227
228
        $schedule->timezone('UTC');
229
230
        $this->assertSame('UTC', $schedule->getTimezone()->getName());
231
        $this->assertSame('UTC', $schedule->all()[0]->getTimezone()->getName());
232
        $this->assertSame('UTC', $schedule->due(new \DateTime())[0]->getTimezone()->getName());
233
        $this->assertSame('America/Toronto', $schedule->all()[1]->getTimezone()->getName());
234
        $this->assertSame('America/Toronto', $schedule->due(new \DateTime())[1]->getTimezone()->getName());
235
236
        $schedule->timezone(new \DateTimeZone('America/Los_Angeles'));
237
238
        $this->assertSame('America/Los_Angeles', $schedule->getTimezone()->getName());
239
        $this->assertSame('UTC', $schedule->all()[0]->getTimezone()->getName());
240
        $this->assertSame('UTC', $schedule->due(new \DateTime())[0]->getTimezone()->getName());
241
        $this->assertSame('America/Toronto', $schedule->all()[1]->getTimezone()->getName());
242
        $this->assertSame('America/Toronto', $schedule->due(new \DateTime())[1]->getTimezone()->getName());
243
    }
244
}
245