Passed
Push — master ( 18c542...46352d )
by Kevin
02:34
created

CommandTaskRunnerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 33
c 0
b 0
f 0
dl 0
loc 81
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A creates_exception_result() 0 13 1
A creates_successful_result() 0 10 1
A hp$0 ➔ execute() 0 9 3
createCommand() 0 22 ?
A supports_command_task() 0 3 1
A hp$0 ➔ createCommand() 0 22 3
A hp$0 ➔ configure() 0 5 1
A creates_failure_result() 0 11 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Tests\Schedule\Task\Runner;
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\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask;
11
use Zenstruck\ScheduleBundle\Schedule\Task\Runner\CommandTaskRunner;
12
13
/**
14
 * @author Kevin Bond <[email protected]>
15
 */
16
final class CommandTaskRunnerTest extends TestCase
17
{
18
    /**
19
     * @test
20
     */
21
    public function creates_successful_result()
22
    {
23
        $application = new Application();
24
        $application->add($this->createCommand());
25
        $runner = new CommandTaskRunner($application);
26
27
        $result = $runner(new CommandTask('my:command'));
28
29
        $this->assertTrue($result->isSuccessful());
30
        $this->assertSame('some output...', $result->getOutput());
31
    }
32
33
    /**
34
     * @test
35
     */
36
    public function creates_exception_result()
37
    {
38
        $application = new Application();
39
        $application->add($this->createCommand());
40
        $runner = new CommandTaskRunner($application);
41
42
        $result = $runner(new CommandTask('my:command --exception'));
43
44
        $this->assertTrue($result->isFailure());
45
        $this->assertTrue($result->isException());
46
        $this->assertInstanceOf(\RuntimeException::class, $result->getException());
47
        $this->assertSame('RuntimeException: exception message', $result->getDescription());
48
        $this->assertSame('some output...', $result->getOutput());
49
    }
50
51
    /**
52
     * @test
53
     */
54
    public function creates_failure_result()
55
    {
56
        $application = new Application();
57
        $application->add($this->createCommand());
58
        $runner = new CommandTaskRunner($application);
59
60
        $result = $runner(new CommandTask('my:command --fail'));
61
62
        $this->assertTrue($result->isFailure());
63
        $this->assertSame('Exit 1: General error', $result->getDescription());
64
        $this->assertSame('some output...', $result->getOutput());
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function supports_command_task()
71
    {
72
        $this->assertTrue((new CommandTaskRunner(new Application()))->supports(new CommandTask('my:command')));
73
    }
74
75
    private function createCommand(): Command
76
    {
77
        return new class() extends Command {
78
            protected static $defaultName = 'my:command';
79
80
            protected function configure()
81
            {
82
                $this
83
                    ->addOption('fail')
84
                    ->addOption('exception')
85
                ;
86
            }
87
88
            protected function execute(InputInterface $input, OutputInterface $output)
89
            {
90
                $output->write('some output...');
91
92
                if ($input->getOption('exception')) {
93
                    throw new \RuntimeException('exception message');
94
                }
95
96
                return $input->getOption('fail') ? 1 : 0;
97
            }
98
        };
99
    }
100
}
101