Passed
Push — master ( 0824af...ff55b8 )
by Kevin
18:16
created

ScheduleListCommandTest.php$1 ➔ buildSchedule()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 10
cc 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Tests\Command;
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\Console\Application;
7
use Symfony\Component\Console\Helper\FormatterHelper;
8
use Symfony\Component\Console\Helper\HelperSet;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Tester\CommandTester;
11
use Zenstruck\ScheduleBundle\Command\ScheduleListCommand;
12
use Zenstruck\ScheduleBundle\Schedule;
13
use Zenstruck\ScheduleBundle\Schedule\Extension\ExtensionHandlerRegistry;
14
use Zenstruck\ScheduleBundle\Schedule\ScheduleBuilder;
15
use Zenstruck\ScheduleBundle\Tests\Fixture\MockScheduleBuilder;
16
17
/**
18
 * @author Kevin Bond <[email protected]>
19
 */
20
final class ScheduleListCommandTest extends TestCase
21
{
22
    /**
23
     * @test
24
     */
25
    public function no_tasks_defined()
26
    {
27
        $runner = (new MockScheduleBuilder())->getRunner();
28
        $commandTester = new CommandTester(new ScheduleListCommand($runner, new ExtensionHandlerRegistry([])));
29
30
        $this->expectException(\RuntimeException::class);
31
        $this->expectExceptionMessage('No scheduled tasks configured.');
32
33
        $commandTester->execute([]);
34
    }
35
36
    /**
37
     * @test
38
     */
39
    public function lists_configured_tasks_and_issues()
40
    {
41
        $runner = (new MockScheduleBuilder())
42
            ->addBuilder(new class() implements ScheduleBuilder {
43
                public function buildSchedule(Schedule $schedule): void
44
                {
45
                    $schedule->emailOnFailure('[email protected]');
46
                    $schedule->addCommand('my:command')
47
                        ->mondays()
48
                        ->at('1:30')
49
                        ->emailOnFailure('[email protected]')
50
                        ->pingOnFailure('https://example.com/my-command-failed')
51
                    ;
52
                }
53
            })
54
            ->getRunner()
55
        ;
56
        $command = new ScheduleListCommand($runner, new ExtensionHandlerRegistry([]));
57
        $command->setHelperSet(new HelperSet([new FormatterHelper()]));
58
        $commandTester = new CommandTester($command);
59
60
        $commandTester->execute([]);
61
62
        $this->assertStringContainsString('[!] CommandTask   my:command   2            Every Monday at 1:30am (30 1 * * 1)', $commandTester->getDisplay());
63
        $this->assertStringContainsString('[WARNING] 3 task issues:', $commandTester->getDisplay());
64
        $this->assertStringContainsString('[ERROR] No task runner registered to handle "Zenstruck\ScheduleBundle\Schedule\Task\CommandTask".', $commandTester->getDisplay());
65
        $this->assertStringContainsString('[ERROR] To use the email extension you must configure a mailer (config path: "zenstruck_schedule.email_handler").', $commandTester->getDisplay());
66
        $this->assertStringContainsString('[ERROR] No extension handler registered for "Zenstruck\ScheduleBundle\Schedule\Extension\PingExtension: On Task', $commandTester->getDisplay());
67
        $this->assertStringContainsString('Failure, ping "https://example.com/my-command-failed"".', $commandTester->getDisplay());
68
        $this->assertStringContainsString('1 Schedule Extension:', $commandTester->getDisplay());
69
        $this->assertStringContainsString('On Schedule Failure, email output to "[email protected]"', $commandTester->getDisplay());
70
        $this->assertStringContainsString('[WARNING] 1 issue with schedule:', $commandTester->getDisplay());
71
    }
72
73
    /**
74
     * @test
75
     */
76
    public function renders_exception_stack_trace_if_verbose()
77
    {
78
        $runner = (new MockScheduleBuilder())
79
            ->addBuilder(new class() implements ScheduleBuilder {
80
                public function buildSchedule(Schedule $schedule): void
81
                {
82
                    $schedule->addCommand('my:command')
83
                        ->mondays()
84
                        ->at('1:30')
85
                    ;
86
                }
87
            })
88
            ->getRunner()
89
        ;
90
        $command = new ScheduleListCommand($runner, new ExtensionHandlerRegistry([]));
91
        $command->setHelperSet(new HelperSet([new FormatterHelper()]));
92
        $command->setApplication(new Application());
93
        $commandTester = new CommandTester($command);
94
95
        $commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
96
97
        $this->assertStringContainsString('[WARNING] 1 task issue:', $commandTester->getDisplay());
98
        $this->assertStringContainsString('In ScheduleRunner.php line', $commandTester->getDisplay());
99
        $this->assertStringContainsString('[LogicException]', $commandTester->getDisplay());
100
        $this->assertStringContainsString('No task runner registered to handle "Zenstruck\ScheduleBundle\Schedule\Task\CommandTask".', $commandTester->getDisplay());
101
        $this->assertStringContainsString('Exception trace:', $commandTester->getDisplay());
102
    }
103
104
    /**
105
     * @test
106
     */
107
    public function lists_configured_tasks_and_issues_in_detail()
108
    {
109
        $runner = (new MockScheduleBuilder())
110
            ->addBuilder(new class() implements ScheduleBuilder {
111
                public function buildSchedule(Schedule $schedule): void
112
                {
113
                    $schedule->emailOnFailure('[email protected]');
114
                    $schedule->addCommand('my:command')
115
                        ->arguments('arg1', '--option1')
116
                        ->mondays()
117
                        ->at('1:30')
118
                        ->emailOnFailure('[email protected]')
119
                        ->pingOnFailure('https://example.com/my-command-failed')
120
                    ;
121
                }
122
            })
123
            ->getRunner()
124
        ;
125
        $command = new ScheduleListCommand($runner, new ExtensionHandlerRegistry([]));
126
        $command->setHelperSet(new HelperSet([new FormatterHelper()]));
127
        $command->setApplication(new Application());
128
        $commandTester = new CommandTester($command);
129
130
        $commandTester->execute(['--detail' => null]);
131
132
        $this->assertStringContainsString('1 Scheduled Tasks Configured', $commandTester->getDisplay());
133
        $this->assertStringContainsString('(1/1) CommandTask: my:command', $commandTester->getDisplay());
134
        $this->assertStringContainsString('Frequency   Every Monday at 1:30am (30 1 * * 1)', $commandTester->getDisplay());
135
        $this->assertStringContainsString('Next Run    Mon,', $commandTester->getDisplay());
136
        $this->assertStringContainsString('Arguments: arg1 --option1', $commandTester->getDisplay());
137
        $this->assertStringContainsString('2 Task Extensions:', $commandTester->getDisplay());
138
        $this->assertStringContainsString('On Task Failure, email output to "[email protected]"', $commandTester->getDisplay());
139
        $this->assertStringContainsString('On Task Failure, ping "https://example.com/my-command-failed"', $commandTester->getDisplay());
140
        $this->assertStringContainsString('[WARNING] 3 issues with this task:', $commandTester->getDisplay());
141
        $this->assertStringContainsString('[ERROR] No task runner registered to handle "Zenstruck\ScheduleBundle\Schedule\Task\CommandTask".', $commandTester->getDisplay());
142
        $this->assertStringContainsString('[ERROR] To use the email extension you must configure a mailer (config path: "zenstruck_schedule.email_handler").', $commandTester->getDisplay());
143
        $this->assertStringContainsString('[ERROR] No extension handler registered for "Zenstruck\ScheduleBundle\Schedule\Extension\PingExtension: On Task', $commandTester->getDisplay());
144
        $this->assertStringContainsString('Failure, ping "https://example.com/my-command-failed"".', $commandTester->getDisplay());
145
        $this->assertStringContainsString('1 Schedule Extension:', $commandTester->getDisplay());
146
        $this->assertStringContainsString('On Schedule Failure, email output to "[email protected]"', $commandTester->getDisplay());
147
        $this->assertStringContainsString('[WARNING] 1 issue with schedule:', $commandTester->getDisplay());
148
    }
149
}
150