Completed
Push — master ( fdad86...4add92 )
by Alessandro
08:39
created

ParallelCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 110
ccs 0
cts 25
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 34 1
A configure() 0 15 3
A execute() 0 14 2
A createConfig() 0 14 3
1
<?php
2
3
namespace Paraunit\Command;
4
5
use Paraunit\Configuration\PHPUnitConfig;
6
use Paraunit\Configuration\PHPUnitOption;
7
use Paraunit\Filter\Filter;
8
use Paraunit\Runner\Runner;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * Class ParallelCommand.
16
 */
17
class ParallelCommand extends Command
18
{
19
    /** @var Filter */
20
    protected $filter;
21
22
    /** @var Runner */
23
    protected $runner;
24
25
    /** @var  PHPUnitOption[] */
26
    private $phpunitOptions;
27
28
    /**
29
     * @param Filter $filter
30
     * @param Runner $runner
31
     * @param string $name
32
     */
33
    public function __construct(Filter $filter, Runner $runner, $name = 'Paraunit')
34
    {
35
        $this->phpunitOptions = array(
36
            new PHPUnitOption('filter'),
37
            new PHPUnitOption('testsuite'),
38
            new PHPUnitOption('group'),
39
            new PHPUnitOption('exclude-group'),
40
            new PHPUnitOption('test-suffix'),
41
42
            new PHPUnitOption('report-useless-tests', false),
43
            new PHPUnitOption('strict-global-state', false),
44
            new PHPUnitOption('disallow-test-output', false),
45
            new PHPUnitOption('enforce-time-limit', false),
46
            new PHPUnitOption('disallow-todo-tests', false),
47
48
            new PHPUnitOption('process-isolation', false),
49
            new PHPUnitOption('no-globals-backup', false),
50
            new PHPUnitOption('static-backup', false),
51
52
            new PHPUnitOption('loader'),
53
            new PHPUnitOption('repeat'),
54
            new PHPUnitOption('printer'),
55
56
            new PHPUnitOption('bootstrap'),
57
            new PHPUnitOption('configuration', true, 'c'),
58
            new PHPUnitOption('no-configuration'),
59
            new PHPUnitOption('include-path'),
60
        );
61
62
        parent::__construct($name);
63
64
        $this->filter = $filter;
65
        $this->runner = $runner;
66
    }
67
68
    protected function configure()
69
    {
70
        $this
71
            ->setName('run')
72
            ->addOption('debug', null, InputOption::VALUE_NONE, 'Print verbose debug output');
73
74
        foreach ($this->phpunitOptions as $option) {
75
            $this->addOption(
76
                $option->getName(),
77
                $option->getShortName(),
78
                $option->hasValue() ? InputOption::VALUE_OPTIONAL : InputOption::VALUE_NONE,
79
                'Option carried over to every single PHPUnit process, see PHPUnit docs for usage'
80
            );
81
        }
82
    }
83
84
    /**
85
     * @param InputInterface  $input
86
     * @param OutputInterface $output
87
     *
88
     * @return int|null
89
     *
90
     * @throws \Exception
91
     */
92
    protected function execute(InputInterface $input, OutputInterface $output)
93
    {
94
        $testsuite = null;
95
96
        if ($input->getOption('testsuite')) {
97
            $testsuite = $input->getOption('testsuite');
98
        }
99
100
        $config = $this->createConfig($input);
101
102
        $testArray = $this->filter->filterTestFiles($config, $testsuite);
103
104
        return $this->runner->run($testArray, $output, $config, $input->getOption('debug'));
105
    }
106
107
    /**
108
     * @param InputInterface $input
109
     * @return PHPUnitConfig
110
     * @throws \InvalidArgumentException
111
     */
112
    private function createConfig(InputInterface $input)
113
    {
114
        $config = new PHPUnitConfig($input->getOption('configuration'));
115
116
        foreach ($this->phpunitOptions as $option) {
117
            $cliOption = $input->getOption($option->getName());
118
            if ($cliOption) {
119
                $option->setValue($cliOption);
120
                $config->addPhpunitOption($option);
121
            }
122
        }
123
124
        return $config;
125
    }
126
}
127