ParallelCommand::configure()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 14
cts 14
cp 1
rs 9.6333
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Paraunit\Command;
6
7
use Paraunit\Configuration\ParallelConfiguration;
8
use Paraunit\Configuration\PHPUnitConfig;
9
use Paraunit\Configuration\PHPUnitOption;
10
use Paraunit\Runner\Runner;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * Class ParallelCommand.
19
 */
20
class ParallelCommand extends Command
21
{
22
    /** @var ParallelConfiguration */
23
    protected $configuration;
24
25
    /** @var PHPUnitOption[] */
26
    private $phpunitOptions;
27
28
    /**
29
     * ParallelCommand constructor.
30
     * @param ParallelConfiguration $configuration
31
     * @throws \Symfony\Component\Console\Exception\LogicException
32
     */
33 24
    public function __construct(ParallelConfiguration $configuration)
34
    {
35 24
        $this->phpunitOptions = [
36 24
            new PHPUnitOption('filter'),
37 24
            new PHPUnitOption('testsuite'),
38 24
            new PHPUnitOption('group'),
39 24
            new PHPUnitOption('exclude-group'),
40 24
            new PHPUnitOption('test-suffix'),
41
42 24
            new PHPUnitOption('dont-report-useless-tests', false),
43 24
            new PHPUnitOption('strict-coverage', false),
44 24
            new PHPUnitOption('strict-global-state', false),
45 24
            new PHPUnitOption('disallow-test-output', false),
46 24
            new PHPUnitOption('disallow-resource-usage', false),
47 24
            new PHPUnitOption('enforce-time-limit', false),
48 24
            new PHPUnitOption('disallow-todo-tests', false),
49
50 24
            new PHPUnitOption('process-isolation', false),
51 24
            new PHPUnitOption('globals-backup', false),
52 24
            new PHPUnitOption('static-backup', false),
53
54 24
            new PHPUnitOption('loader'),
55 24
            new PHPUnitOption('repeat'),
56 24
            new PHPUnitOption('printer'),
57
58 24
            new PHPUnitOption('bootstrap'),
59 24
            new PHPUnitOption('no-configuration'),
60 24
            new PHPUnitOption('no-coverage'),
61 24
            new PHPUnitOption('no-extensions'),
62 24
            new PHPUnitOption('include-path'),
63
        ];
64
65 24
        parent::__construct();
66 24
        $this->configuration = $configuration;
67
    }
68
69 24
    protected function configure()
70
    {
71 24
        $this->setName('run');
72 24
        $this->setDescription('Run all the requested tests in parallel');
73 24
        $this->addOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'The PHPUnit XML config filename or path');
74 24
        $this->addArgument('stringFilter', InputArgument::OPTIONAL, 'A case-insensitive string to filter tests filename');
75 24
        $this->addOption('parallel', null, InputOption::VALUE_REQUIRED, 'Number of concurrent processes to launch', 10);
76 24
        $this->addOption('debug', null, InputOption::VALUE_NONE, 'Print verbose debug output');
77 24
        $this->addOption('logo', null, InputOption::VALUE_NONE, 'Print the Shark logo at the top');
78
79 24
        foreach ($this->phpunitOptions as $option) {
80 24
            $this->addOption(
81 24
                $option->getName(),
82 24
                $option->getShortName(),
83 24
                $option->hasValue() ? InputOption::VALUE_OPTIONAL : InputOption::VALUE_NONE,
84 24
                'Option carried over to every single PHPUnit process, see PHPUnit docs for usage'
85
            );
86
        }
87
    }
88
89
    /**
90
     * @param InputInterface $input
91
     * @param OutputInterface $output
92
     *
93
     * @return int|null
94
     *
95
     * @throws \Exception
96
     */
97 22
    protected function execute(InputInterface $input, OutputInterface $output)
98
    {
99 22
        $container = $this->configuration->buildContainer($input, $output);
100
101
        /** @var PHPUnitConfig $config */
102 22
        $config = $container->get(PHPUnitConfig::class);
103 22
        $this->addPHPUnitOptions($config, $input);
104
105
        /** @var Runner $runner */
106 22
        $runner = $container->get(Runner::class);
107
108 22
        return $runner->run();
109
    }
110
111
    /**
112
     * @param PHPUnitConfig $config
113
     * @param InputInterface $input
114
     * @return PHPUnitConfig
115
     */
116 22
    private function addPHPUnitOptions(PHPUnitConfig $config, InputInterface $input): PHPUnitConfig
117
    {
118 22
        foreach ($this->phpunitOptions as $option) {
119 22
            $cliOption = $input->getOption($option->getName());
120 22
            if ($this->setOptionValue($option, $cliOption)) {
121 22
                $config->addPhpunitOption($option);
122
            }
123
        }
124
125 22
        return $config;
126
    }
127
128
    /**
129
     * @param PHPUnitOption $option
130
     * @param mixed $cliOption
131
     * @return bool
132
     */
133 22
    private function setOptionValue(PHPUnitOption $option, $cliOption): bool
134
    {
135 22
        if (! $cliOption) {
136 22
            return false;
137
        }
138 5
        if ($option->hasValue()) {
139 4
            $option->setValue($cliOption);
140
        }
141
142 5
        return true;
143
    }
144
}
145