Completed
Pull Request — master (#57)
by Alessandro
10:45
created

ParallelCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 34 1
A configure() 0 17 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('configuration', 'c', InputOption::VALUE_REQUIRED, 'The PHPUnit XML config file', PHPUnitConfig::DEFAULT_FILE_NAME)
73
//            ->addOption('testsuite', null, InputOption::VALUE_REQUIRED, 'Choice a specific testsuite from your XML config file')
74
            ->addOption('debug', null, InputOption::VALUE_NONE, 'Print verbose debug output');
75
76
        foreach ($this->phpunitOptions as $option) {
77
            $this->addOption(
78
                $option->getName(),
79
                $option->getShortName(),
80
                $option->hasValue() ? InputOption::VALUE_OPTIONAL : InputOption::VALUE_NONE,
81
                'Option carried over to every single PHPUnit process, see PHPUnit docs for usage'
82
            );
83
        }
84
    }
85
86
    /**
87
     * @param InputInterface  $input
88
     * @param OutputInterface $output
89
     *
90
     * @return int|null
91
     *
92
     * @throws \Exception
93
     */
94
    protected function execute(InputInterface $input, OutputInterface $output)
95
    {
96
        $testsuite = null;
97
98
        if ($input->getOption('testsuite')) {
99
            $testsuite = $input->getOption('testsuite');
100
        }
101
102
        $config = $this->createConfig($input);
103
104
        $testArray = $this->filter->filterTestFiles($config, $testsuite);
105
106
        return $this->runner->run($testArray, $output, $config, $input->getOption('debug'));
107
    }
108
109
    /**
110
     * @param InputInterface $input
111
     * @return PHPUnitConfig
112
     * @throws \InvalidArgumentException
113
     */
114
    private function createConfig(InputInterface $input)
115
    {
116
        $config = new PHPUnitConfig($input->getOption('configuration'));
117
118
        foreach ($this->phpunitOptions as $option) {
119
            $cliOption = $input->getOption($option->getName());
120
            if ($cliOption) {
121
                $option->setValue($cliOption);
122
                $config->addPhpunitOption($option);
123
            }
124
        }
125
126
        return $config;
127
    }
128
}
129