Completed
Pull Request — master (#94)
by Alessandro
04:33
created

ParallelConfiguration::buildContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Paraunit\Configuration;
5
6
use Paraunit\Printer\OutputFactory;
7
use Symfony\Component\Config\FileLocator;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Definition;
12
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
13
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
16
/**
17
 * Class Paraunit
18
 * @package Paraunit\Configuration
19
 */
20
class ParallelConfiguration
21
{
22
    const TAG_EVENT_SUBSCRIBER = 'paraunit.event_subscriber';
23
24
    /**
25
     * @param InputInterface $input
26
     * @param OutputInterface $output
27
     * @return ContainerBuilder
28
     * @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
29
     * @throws \Exception
30
     */
31 56
    public function buildContainer(InputInterface $input, OutputInterface $output): ContainerBuilder
32
    {
33 56
        $containerBuilder = new ContainerBuilder();
34
35 56
        $this->injectOutput($containerBuilder, $output);
36 56
        $this->loadYamlConfiguration($containerBuilder);
37 56
        $this->loadCommandLineOptions($containerBuilder, $input);
38 56
        $this->tagEventSubscribers($containerBuilder);
39
40 56
        $containerBuilder->compile();
41
42 56
        $this->loadPostCompileSettings($containerBuilder, $input);
43
44 56
        return $containerBuilder;
45
    }
46
47
    /**
48
     * @param ContainerBuilder $containerBuilder
49
     * @return YamlFileLoader
50
     * @throws \Exception
51
     */
52 56
    protected function loadYamlConfiguration(ContainerBuilder $containerBuilder): YamlFileLoader
53
    {
54 56
        $loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__ . '/../Resources/config/'));
55 56
        $loader->load('configuration.yml');
56 56
        $loader->load('file.yml');
57 56
        $loader->load('parser.yml');
58 56
        $loader->load('printer.yml');
59 56
        $loader->load('process.yml');
60 56
        $loader->load('runner.yml');
61 56
        $loader->load('services.yml');
62 56
        $loader->load('test_result.yml');
63 56
        $loader->load('test_result_container.yml');
64 56
        $loader->load('test_result_format.yml');
65
66 56
        return $loader;
67
    }
68
69 56
    protected function tagEventSubscribers(ContainerBuilder $container)
70
    {
71 56
        $container->addCompilerPass(new RegisterListenersPass('event_dispatcher', null, self::TAG_EVENT_SUBSCRIBER));
72
73 56
        foreach ($container->getDefinitions() as $definition) {
74 56
            if (is_subclass_of($definition->getClass(), EventSubscriberInterface::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Symfony\Component\Event...scriberInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
75 56
                $definition->addTag(self::TAG_EVENT_SUBSCRIBER);
76
            }
77
        }
78
    }
79
80 56
    protected function loadCommandLineOptions(ContainerBuilder $containerBuilder, InputInterface $input)
81
    {
82 56
        $containerBuilder->setParameter('paraunit.max_process_count', $input->getOption('parallel'));
83 56
        $containerBuilder->setParameter('paraunit.phpunit_config_filename', $input->getOption('configuration') ?? '.');
84 56
        $containerBuilder->setParameter('paraunit.testsuite', $input->getOption('testsuite'));
85 56
        $containerBuilder->setParameter('paraunit.string_filter', $input->getArgument('stringFilter'));
86
    }
87
88
    protected function loadPostCompileSettings(ContainerBuilder $container, InputInterface $input)
89
    {
90
    }
91
92 56
    private function injectOutput(ContainerBuilder $containerBuilder, OutputInterface $output)
93
    {
94 56
        OutputFactory::setOutput($output);
95 56
        $factoryClass = OutputFactory::class;
96 56
        $factoryMethod = 'getOutput';
97
98 56
        $definition = new Definition(OutputInterface::class);
99 56
        $definition->setFactory([$factoryClass, $factoryMethod]);
100
101 56
        $containerBuilder->setDefinition('output', $definition);
102
    }
103
}
104