Completed
Push — master ( 400051...4d5855 )
by Alessandro
07:06
created

ParallelCommand::createConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 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 8
    public function __construct(ParallelConfiguration $configuration)
29
    {
30 8
        $this->phpunitOptions = array(
31 8
            new PHPUnitOption('filter'),
32 8
            new PHPUnitOption('testsuite'),
33 8
            new PHPUnitOption('group'),
34 8
            new PHPUnitOption('exclude-group'),
35 8
            new PHPUnitOption('test-suffix'),
36
37 8
            new PHPUnitOption('report-useless-tests', false),
38 8
            new PHPUnitOption('strict-global-state', false),
39 8
            new PHPUnitOption('disallow-test-output', false),
40 8
            new PHPUnitOption('enforce-time-limit', false),
41 8
            new PHPUnitOption('disallow-todo-tests', false),
42
43 8
            new PHPUnitOption('process-isolation', false),
44 8
            new PHPUnitOption('no-globals-backup', false),
45 8
            new PHPUnitOption('static-backup', false),
46
47 8
            new PHPUnitOption('loader'),
48 8
            new PHPUnitOption('repeat'),
49 8
            new PHPUnitOption('printer'),
50
51 8
            new PHPUnitOption('bootstrap'),
52 8
            new PHPUnitOption('configuration', true, 'c'),
53 8
            new PHPUnitOption('no-configuration'),
54 8
            new PHPUnitOption('include-path'),
55
        );
56
57 8
        parent::__construct();
58 8
        $this->configuration = $configuration;
59 8
    }
60
61 8
    protected function configure()
62
    {
63 8
        $this->setName('run');
64 8
        $this->setDescription('Run all the requested tests in parallel');
65 8
        $this->addOption('parallel', null, InputOption::VALUE_REQUIRED, 'Number of concurrent processes to launch', 10);
66 8
        $this->addOption('debug', null, InputOption::VALUE_NONE, 'Print verbose debug output');
67
68 8
        foreach ($this->phpunitOptions as $option) {
69 8
            $this->addOption(
70 8
                $option->getName(),
71 8
                $option->getShortName(),
72 8
                $option->hasValue() ? InputOption::VALUE_OPTIONAL : InputOption::VALUE_NONE,
73 8
                'Option carried over to every single PHPUnit process, see PHPUnit docs for usage'
74
            );
75
        }
76 8
    }
77
78
    /**
79
     * @param InputInterface $input
80
     * @param OutputInterface $output
81
     *
82
     * @return int|null
83
     *
84
     * @throws \Exception
85
     */
86 6
    protected function execute(InputInterface $input, OutputInterface $output)
87
    {
88 6
        $testsuite = null;
89
90 6
        if ($input->getOption('testsuite')) {
91
            $testsuite = $input->getOption('testsuite');
92
        }
93
94 6
        $config = $this->createConfig($input);
95
96 6
        $container = $this->configuration->buildContainer($input);
97
98 6
        $filter = $container->get('paraunit.filter.filter');
99 6
        $testArray = $filter->filterTestFiles($config, $testsuite);
100 6
        $runner = $container->get('paraunit.runner.runner');
101
102 6
        return $runner->run($testArray, $output, $config, $input->getOption('debug'));
103
    }
104
105
    /**
106
     * @param InputInterface $input
107
     * @return PHPUnitConfig
108
     * @throws \InvalidArgumentException
109
     */
110 6
    private function createConfig(InputInterface $input)
111
    {
112 6
        $config = new PHPUnitConfig($input->getOption('configuration'));
113
114 6
        foreach ($this->phpunitOptions as $option) {
115 6
            $cliOption = $input->getOption($option->getName());
116 6
            if ($cliOption) {
117
                $option->setValue($cliOption);
118 6
                $config->addPhpunitOption($option);
119
            }
120
        }
121
122 6
        return $config;
123
    }
124
}
125