Completed
Pull Request — master (#59)
by Alessandro
05:30
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.243

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 10
cp 0.7
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 3.243
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
64 4
            ->setName('run')
65 4
            ->addOption('debug', null, InputOption::VALUE_NONE, 'Print verbose debug output');
66
67 4
        foreach ($this->phpunitOptions as $option) {
68 4
            $this->addOption(
69 4
                $option->getName(),
70 4
                $option->getShortName(),
71 4
                $option->hasValue() ? InputOption::VALUE_OPTIONAL : InputOption::VALUE_NONE,
72
                'Option carried over to every single PHPUnit process, see PHPUnit docs for usage'
73 4
            );
74 4
        }
75 4
    }
76
77
    /**
78
     * @param InputInterface $input
79
     * @param OutputInterface $output
80
     *
81
     * @return int|null
82
     *
83
     * @throws \Exception
84
     */
85 2
    protected function execute(InputInterface $input, OutputInterface $output)
86
    {
87 2
        $testsuite = null;
88
89 2
        if ($input->getOption('testsuite')) {
90
            $testsuite = $input->getOption('testsuite');
91
        }
92
93 2
        $config = $this->createConfig($input);
94
95 2
        $container = $this->configuration->buildContainer($input);
96
97 2
        $filter = $container->get('paraunit.filter.filter');
98 2
        $testArray = $filter->filterTestFiles($config, $testsuite);
99 2
        $runner = $container->get('paraunit.runner.runner');
100
101 2
        return $runner->run($testArray, $output, $config, $input->getOption('debug'));
102
    }
103
104
    /**
105
     * @param InputInterface $input
106
     * @return PHPUnitConfig
107
     * @throws \InvalidArgumentException
108
     */
109 2
    private function createConfig(InputInterface $input)
110
    {
111 2
        $config = new PHPUnitConfig($input->getOption('configuration'));
112
113 2
        foreach ($this->phpunitOptions as $option) {
114 2
            $cliOption = $input->getOption($option->getName());
115 2
            if ($cliOption) {
116
                $option->setValue($cliOption);
117
                $config->addPhpunitOption($option);
118
            }
119 2
        }
120
121 2
        return $config;
122
    }
123
}
124