Test Failed
Pull Request — master (#132)
by Alessandro
03:15
created

ParallelConfigurationTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 4.71 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 4
loc 85
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B testBuildContainer() 4 55 3
A testBuildContainerWithDebug() 0 21 1
A getService() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tests\Unit\Configuration;
6
7
use Paraunit\Configuration\ParallelConfiguration;
8
use Paraunit\Configuration\PHPDbgBinFile;
9
use Paraunit\Configuration\PHPUnitConfig;
10
use Paraunit\Coverage\CoverageFetcher;
11
use Paraunit\File\Cleaner;
12
use Paraunit\Parser\JSON\LogParser;
13
use Paraunit\Printer\CoveragePrinter;
14
use Paraunit\Printer\DebugPrinter;
15
use Paraunit\Printer\ProcessPrinter;
16
use Paraunit\Process\ProcessFactoryInterface;
17
use Paraunit\Runner\Runner;
18
use Paraunit\TestResult\TestResultFactory;
19
use Prophecy\Argument;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\DependencyInjection\ContainerBuilder;
23
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
24
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
25
use Tests\BaseUnitTestCase;
26
27
/**
28
 * Class ParaunitTest
29
 * @package Tests\Unit\Configuration
30
 */
31
class ParallelConfigurationTest extends BaseUnitTestCase
32
{
33
    public function testBuildContainer()
34
    {
35
        $paraunit = new ParallelConfiguration(true);
36
        $input = $this->prophesize(InputInterface::class);
37
        $output = $this->prophesize(OutputInterface::class);
38
        $input->getArgument('stringFilter')
39
            ->willReturn('text');
40
        $input->getOption('parallel')
41
            ->willReturn(10);
42
        $input->getOption('testsuite')
43
            ->willReturn('testsuite');
44
        $input->getOption('configuration')
45
            ->willReturn($this->getConfigForStubs());
46
        $input->getOption(Argument::cetera())
47
            ->willReturn(null);
48
49
        $container = $paraunit->buildContainer($input->reveal(), $output->reveal());
50
51
        $this->assertInstanceOf(ContainerBuilder::class, $container);
52
53
        $requiredParameters = [
54
            'paraunit.max_process_count' => 10,
55
            'paraunit.testsuite' => 'testsuite',
56
            'paraunit.string_filter' => 'text',
57
            'paraunit.phpunit_config_filename' => $this->getConfigForStubs(),
58
        ];
59
60 View Code Duplication
        foreach ($requiredParameters as $parameterName => $expectedValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
            $this->assertTrue($container->hasParameter($parameterName), 'Parameter missing: ' . $parameterName);
62
            $this->assertEquals($expectedValue, $container->getParameter($parameterName));
63
        }
64
65
        $requiredDefinitions = [
66
            OutputInterface::class,
67
            Cleaner::class,
68
            LogParser::class,
69
            ProcessPrinter::class,
70
            ProcessFactoryInterface::class,
71
            Runner::class,
72
            EventDispatcherInterface::class,
73
            TestResultFactory::class,
74
            'paraunit.test_result.pass_container',
75
            'paraunit.test_result.pass_format',
76
            PHPUnitConfig::class,
77
        ];
78
79
        $servicesIds = $container->getServiceIds();
80
        $this->assertNotContains(PHPDbgBinFile::class, $servicesIds);
81
        $this->assertNotContains(CoverageFetcher::class, $servicesIds);
82
        $this->assertNotContains(CoveragePrinter::class, $servicesIds);
83
84
        foreach ($requiredDefinitions as $definitionName) {
85
            $this->getService($container, $definitionName); // test instantiation, to prevent misconfigurations
86
        }
87
    }
88
89
    public function testBuildContainerWithDebug()
90
    {
91
        $paraunit = new ParallelConfiguration(true);
92
        $input = $this->prophesize(InputInterface::class);
93
        $output = $this->prophesize(OutputInterface::class);
94
        $input->getArgument('stringFilter')
95
            ->willReturn('text');
96
        $input->getOption('debug')
97
            ->willReturn(true);
98
        $input->getOption(Argument::cetera())
99
            ->willReturn(null);
100
101
        $container = $paraunit->buildContainer($input->reveal(), $output->reveal());
102
103
        $this->assertInstanceOf(ContainerBuilder::class, $container);
104
105
        // test instantiation, to prevent misconfigurations
106
        $service = $this->getService($container, DebugPrinter::class);
107
        $this->assertInstanceOf(DebugPrinter::class, $service);
108
        $this->assertInstanceOf(EventSubscriberInterface::class, $service);
109
    }
110
111
    private function getService(ContainerBuilder $container, string $serviceName)
112
    {
113
        return $container->get(sprintf(ParallelConfiguration::PUBLIC_ALIAS_FORMAT, $serviceName));
114
    }
115
}
116