Passed
Pull Request — master (#2)
by Kevin
02:04
created

ConfigureTasksTest::full_task_configuration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 37
c 1
b 0
f 1
dl 0
loc 51
rs 9.328
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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' => '/bin/script',
58
                'frequency' => '0 * * * *',
59
                'type' => 'process',
60
            ],
61
        ]);
62
63
        $this->assertCount(1, $schedule->all());
64
        $this->assertInstanceOf(ProcessTask::class, $schedule->all()[0]);
65
        $this->assertSame('/bin/script', $schedule->all()[0]->getDescription());
66
        $this->assertSame('0 * * * *', $schedule->all()[0]->getExpression());
67
        $this->assertCount(0, $schedule->all()[0]->getExtensions());
68
    }
69
70
    /**
71
     * @test
72
     */
73
    public function full_task_configuration()
74
    {
75
        $schedule = $this->createSchedule([
76
            [
77
                'command' => 'my:command --option',
78
                'frequency' => '0 0 * * *',
79
                'description' => 'my description',
80
                'without_overlapping' => null,
81
                'between' => [
82
                    'start' => 9,
83
                    'end' => 17,
84
                ],
85
                'unless_between' => [
86
                    'start' => 12,
87
                    'end' => '13:30',
88
                ],
89
                'ping_before' => [
90
                    'url' => 'https://example.com/before',
91
                ],
92
                'ping_after' => [
93
                    'url' => 'https://example.com/after',
94
                ],
95
                'ping_on_success' => [
96
                    'url' => 'https://example.com/success',
97
                ],
98
                'ping_on_failure' => [
99
                    'url' => 'https://example.com/failure',
100
                    'method' => 'POST',
101
                ],
102
                'email_after' => null,
103
                'email_on_failure' => [
104
                    'to' => '[email protected]',
105
                    'subject' => 'my subject',
106
                ],
107
            ],
108
        ]);
109
110
        $task = $schedule->all()[0];
111
        $extensions = $task->getExtensions();
112
113
        $this->assertSame('my description', $task->getDescription());
114
        $this->assertCount(9, $extensions);
115
        $this->assertSame('Without overlapping', (string) $extensions[0]);
116
        $this->assertSame('Only run between 9:00 and 17:00', (string) $extensions[1]);
117
        $this->assertSame('Only run if not between 12:00 and 13:30', (string) $extensions[2]);
118
        $this->assertSame('Before Task, ping "https://example.com/before"', (string) $extensions[3]);
119
        $this->assertSame('After Task, ping "https://example.com/after"', (string) $extensions[4]);
120
        $this->assertSame('On Task Success, ping "https://example.com/success"', (string) $extensions[5]);
121
        $this->assertSame('On Task Failure, ping "https://example.com/failure"', (string) $extensions[6]);
122
        $this->assertSame('After Task, email output', (string) $extensions[7]);
123
        $this->assertSame('On Task Failure, email output to "[email protected]"', (string) $extensions[8]);
124
    }
125
126
    private function createSchedule(array $taskConfig): Schedule
127
    {
128
        $processor = new Processor();
129
        $config = $processor->processConfiguration(new Configuration(), [['tasks' => $taskConfig]]);
130
131
        return (new MockScheduleBuilder())
132
            ->addSubscriber(new ConfigureTasksSubscriber($config['tasks']))
133
            ->getRunner()
134
            ->buildSchedule()
135
        ;
136
    }
137
}
138