Passed
Push — master ( b67a30...b7883b )
by Kevin
09:53 queued 08:10
created

ScheduleListCommandTest.php$2 ➔ normalizeOutput()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
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
        $output = $this->normalizeOutput($commandTester);
62
63
        $this->assertStringContainsString('[!] CommandTask my:command 2 Every Monday at 1:30am (30 1 * * 1)', $output);
64
        $this->assertStringContainsString('[WARNING] 3 task issues:', $output);
65
        $this->assertStringContainsString('[ERROR] No task runner registered to handle "Zenstruck\ScheduleBundle\Schedule\Task\CommandTask".', $output);
66
        $this->assertStringContainsString('[ERROR] To use the email extension you must configure a mailer (config path: "zenstruck_schedule.email_handler").', $output);
67
        $this->assertStringContainsString('[ERROR] No extension handler registered for "Zenstruck\ScheduleBundle\Schedule\Extension\PingExtension: On Task', $output);
68
        $this->assertStringContainsString('Failure, ping "https://example.com/my-command-failed"".', $output);
69
        $this->assertStringContainsString('1 Schedule Extension:', $output);
70
        $this->assertStringContainsString('On Schedule Failure, email output to "[email protected]"', $output);
71
        $this->assertStringContainsString('[WARNING] 1 issue with schedule:', $output);
72
    }
73
74
    /**
75
     * @test
76
     */
77
    public function renders_exception_stack_trace_if_verbose()
78
    {
79
        $runner = (new MockScheduleBuilder())
80
            ->addBuilder(new class() implements ScheduleBuilder {
81
                public function buildSchedule(Schedule $schedule): void
82
                {
83
                    $schedule->addCommand('my:command')
84
                        ->mondays()
85
                        ->at('1:30')
86
                    ;
87
                }
88
            })
89
            ->getRunner()
90
        ;
91
        $command = new ScheduleListCommand($runner, new ExtensionHandlerRegistry([]));
92
        $command->setHelperSet(new HelperSet([new FormatterHelper()]));
93
        $command->setApplication(new Application());
94
        $commandTester = new CommandTester($command);
95
96
        $commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
97
        $output = $this->normalizeOutput($commandTester);
98
99
        $this->assertStringContainsString('[WARNING] 1 task issue:', $output);
100
        $this->assertStringContainsString('In ScheduleRunner.php line', $output);
101
        $this->assertStringContainsString('[LogicException]', $output);
102
        $this->assertStringContainsString('No task runner registered to handle', $output);
103
        $this->assertStringContainsString('Exception trace:', $output);
104
    }
105
106
    /**
107
     * @test
108
     */
109
    public function lists_configured_tasks_and_issues_in_detail()
110
    {
111
        $runner = (new MockScheduleBuilder())
112
            ->addBuilder(new class() implements ScheduleBuilder {
113
                public function buildSchedule(Schedule $schedule): void
114
                {
115
                    $schedule->emailOnFailure('[email protected]');
116
                    $schedule->addCommand('my:command')
117
                        ->arguments('arg1', '--option1')
118
                        ->mondays()
119
                        ->at('1:30')
120
                        ->emailOnFailure('[email protected]')
121
                        ->pingOnFailure('https://example.com/my-command-failed')
122
                    ;
123
                }
124
            })
125
            ->getRunner()
126
        ;
127
        $command = new ScheduleListCommand($runner, new ExtensionHandlerRegistry([]));
128
        $command->setHelperSet(new HelperSet([new FormatterHelper()]));
129
        $command->setApplication(new Application());
130
        $commandTester = new CommandTester($command);
131
132
        $commandTester->execute(['--detail' => null]);
133
        $output = $this->normalizeOutput($commandTester);
134
135
        $this->assertStringContainsString('1 Scheduled Tasks Configured', $output);
136
        $this->assertStringContainsString('(1/1) CommandTask: my:command', $output);
137
        $this->assertStringContainsString('Every Monday at 1:30am (30 1 * * 1)', $output);
138
        $this->assertStringContainsString('Mon,', $output);
139
        $this->assertStringContainsString('Arguments: arg1 --option1', $output);
140
        $this->assertStringContainsString('2 Task Extensions:', $output);
141
        $this->assertStringContainsString('On Task Failure, email output to "[email protected]"', $output);
142
        $this->assertStringContainsString('On Task Failure, ping "https://example.com/my-command-failed"', $output);
143
        $this->assertStringContainsString('[WARNING] 3 issues with this task:', $output);
144
        $this->assertStringContainsString('[ERROR] No task runner registered to handle "Zenstruck\ScheduleBundle\Schedule\Task\CommandTask".', $output);
145
        $this->assertStringContainsString('[ERROR] To use the email extension you must configure a mailer (config path: "zenstruck_schedule.email_handler").', $output);
146
        $this->assertStringContainsString('[ERROR] No extension handler registered for "Zenstruck\ScheduleBundle\Schedule\Extension\PingExtension: On Task', $output);
147
        $this->assertStringContainsString('Failure, ping "https://example.com/my-command-failed"".', $output);
148
        $this->assertStringContainsString('1 Schedule Extension:', $output);
149
        $this->assertStringContainsString('On Schedule Failure, email output to "[email protected]"', $output);
150
        $this->assertStringContainsString('[WARNING] 1 issue with schedule:', $output);
151
    }
152
153
    private function normalizeOutput(CommandTester $tester): string
154
    {
155
        return \preg_replace('/\s+/', ' ', \str_replace("\n", '', $tester->getDisplay(true)));
156
    }
157
}
158