Completed
Pull Request — master (#62)
by Alessandro
05:54
created

ParallelCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 91.38%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 1
cbo 6
dl 0
loc 109
ccs 53
cts 58
cp 0.9138
rs 10

4 Methods

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