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

RunnerTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 138
Duplicated Lines 40.58 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 56
loc 138
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testRunEmptyTestSuite() 0 22 1
A testRunWithSomeGreenTests() 0 28 1
A testOnProcessParsingCompletedWithFailedProcess() 28 28 1
A testOnProcessToBeRetried() 28 28 1
A mockEventDispatcher() 0 16 1
A mockProcessFactory() 0 8 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\Runner;
6
7
use Paraunit\Filter\Filter;
8
use Paraunit\Lifecycle\EngineEvent;
9
use Paraunit\Lifecycle\ProcessEvent;
10
use Paraunit\Process\AbstractParaunitProcess;
11
use Paraunit\Process\ProcessFactoryInterface;
12
use Paraunit\Runner\Pipeline;
13
use Paraunit\Runner\PipelineCollection;
14
use Paraunit\Runner\Runner;
15
use Prophecy\Argument;
16
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17
use Tests\BaseUnitTestCase;
18
use Tests\Stub\StubbedParaunitProcess;
19
20
class RunnerTest extends BaseUnitTestCase
21
{
22
    public function testRunEmptyTestSuite()
23
    {
24
        $filter = $this->prophesize(Filter::class);
25
        $filter->filterTestFiles()
26
            ->willReturn([]);
27
        $pipelineCollection = $this->prophesize(PipelineCollection::class);
28
        $pipelineCollection->triggerProcessTermination()
29
            ->shouldBeCalled();
30
        $pipelineCollection->hasEmptySlots()
31
            ->willReturn(true);
32
        $pipelineCollection->isEmpty()
33
            ->willReturn(true);
34
35
        $runner = new Runner(
36
            $this->mockEventDispatcher(),
37
            $this->mockProcessFactory(),
38
            $filter->reveal(),
39
            $pipelineCollection->reveal()
40
        );
41
42
        $this->assertSame(0, $runner->run());
43
    }
44
45
    public function testRunWithSomeGreenTests()
46
    {
47
        $filter = $this->prophesize(Filter::class);
48
        $filter->filterTestFiles()
49
            ->willReturn([
50
                'Test1.php',
51
                'Test2.php',
52
            ]);
53
        $pipelineCollection = $this->prophesize(PipelineCollection::class);
54
        $pipelineCollection->triggerProcessTermination()
55
            ->shouldBeCalled();
56
        $pipelineCollection->hasEmptySlots()
57
            ->willReturn(true);
58
        $pipelineCollection->isEmpty()
59
            ->willReturn(true);
60
        $pipelineCollection->push(Argument::cetera())
61
            ->shouldBeCalledTimes(2)
62
            ->willReturn($this->prophesize(Pipeline::class)->reveal());
63
64
        $runner = new Runner(
65
            $this->mockEventDispatcher(),
66
            $this->mockProcessFactory(),
67
            $filter->reveal(),
68
            $pipelineCollection->reveal()
69
        );
70
71
        $this->assertSame(0, $runner->run());
72
    }
73
74 View Code Duplication
    public function testOnProcessParsingCompletedWithFailedProcess()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
75
    {
76
        $process = new StubbedParaunitProcess();
77
        $process->setIsToBeRetried(false);
78
        $process->setExitCode(1);
79
80
        $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
81
        $eventDispatcher->dispatch(Argument::cetera())
82
            ->shouldNotBeCalled();
83
84
        $filter = $this->prophesize(Filter::class);
85
        $filter->filterTestFiles()
86
            ->willReturn([]);
87
        $pipelineCollection = $this->prophesize(PipelineCollection::class);
88
        $pipelineCollection->push($process)
89
            ->shouldNotBeCalled();
90
91
        $runner = new Runner(
92
            $eventDispatcher->reveal(),
93
            $this->mockProcessFactory(),
94
            $filter->reveal(),
95
            $pipelineCollection->reveal()
96
        );
97
98
        $runner->onProcessParsingCompleted(new ProcessEvent($process));
99
100
        $this->assertAttributeNotSame(0, 'exitCode', $runner);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\Assert::assertAttributeNotSame() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/issues/3338

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
101
    }
102
103 View Code Duplication
    public function testOnProcessToBeRetried()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
104
    {
105
        $process = new StubbedParaunitProcess();
106
        $process->setIsToBeRetried(true);
107
        $process->setExitCode(1);
108
109
        $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
110
        $eventDispatcher->dispatch(Argument::cetera())
111
            ->shouldNotBeCalled();
112
113
        $filter = $this->prophesize(Filter::class);
114
        $filter->filterTestFiles()
115
            ->willReturn([]);
116
        $pipelineCollection = $this->prophesize(PipelineCollection::class);
117
        $pipelineCollection->push($process)
118
            ->shouldNotBeCalled();
119
120
        $runner = new Runner(
121
            $eventDispatcher->reveal(),
122
            $this->mockProcessFactory(),
123
            $filter->reveal(),
124
            $pipelineCollection->reveal()
125
        );
126
127
        $runner->onProcessToBeRetried(new ProcessEvent($process));
128
129
        $this->assertAttributeSame(0, 'exitCode', $runner);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\Assert::assertAttributeSame() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/issues/3338

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
130
    }
131
132
    private function mockEventDispatcher(): EventDispatcherInterface
133
    {
134
        $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
135
        $eventDispatcher->dispatch(EngineEvent::BEFORE_START, Argument::cetera())
136
            ->shouldBeCalledTimes(1)
137
            ->will(function () use ($eventDispatcher) {
138
                $eventDispatcher->dispatch(EngineEvent::START, Argument::cetera())
139
                    ->shouldBeCalledTimes(1)
140
                    ->will(function () use ($eventDispatcher) {
141
                        $eventDispatcher->dispatch(EngineEvent::END, Argument::cetera())
142
                            ->shouldBeCalledTimes(1);
143
                    });
144
            });
145
146
        return $eventDispatcher->reveal();
147
    }
148
149
    private function mockProcessFactory(): ProcessFactoryInterface
150
    {
151
        $processFactory = $this->prophesize(ProcessFactoryInterface::class);
152
        $processFactory->create(Argument::containingString('.php'))
153
            ->willReturn($this->prophesize(AbstractParaunitProcess::class)->reveal());
154
155
        return $processFactory->reveal();
156
    }
157
}
158