1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Tests\Unit\Configuration; |
6
|
|
|
|
7
|
|
|
use Paraunit\Configuration\CoverageConfiguration; |
8
|
|
|
use Paraunit\Configuration\PHPDbgBinFile; |
9
|
|
|
use Paraunit\Configuration\PHPUnitConfig; |
10
|
|
|
use Paraunit\Coverage\CoverageFetcher; |
11
|
|
|
use Paraunit\Coverage\CoverageMerger; |
12
|
|
|
use Paraunit\Coverage\CoverageResult; |
13
|
|
|
use Paraunit\Coverage\Processor\AbstractText; |
14
|
|
|
use Paraunit\Coverage\Processor\Clover; |
15
|
|
|
use Paraunit\Coverage\Processor\Crap4j; |
16
|
|
|
use Paraunit\Coverage\Processor\Html; |
17
|
|
|
use Paraunit\Coverage\Processor\Php; |
18
|
|
|
use Paraunit\Coverage\Processor\Text; |
19
|
|
|
use Paraunit\Coverage\Processor\TextSummary; |
20
|
|
|
use Paraunit\Coverage\Processor\Xml; |
21
|
|
|
use Paraunit\Parser\JSON\LogParser; |
22
|
|
|
use Paraunit\Printer\CoveragePrinter; |
23
|
|
|
use Paraunit\Printer\DebugPrinter; |
24
|
|
|
use Paraunit\Printer\ProcessPrinter; |
25
|
|
|
use Paraunit\Process\ProcessFactoryInterface; |
26
|
|
|
use Paraunit\Runner\Runner; |
27
|
|
|
use Paraunit\TestResult\TestResultFactory; |
28
|
|
|
use Prophecy\Argument; |
29
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
30
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
31
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
32
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
33
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
34
|
|
|
use Tests\BaseUnitTestCase; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Class ParaunitCoverageTest |
38
|
|
|
* @package Tests\Unit\Configuration |
39
|
|
|
*/ |
40
|
|
|
class CoverageConfigurationTest extends BaseUnitTestCase |
41
|
|
|
{ |
42
|
|
|
public function testBuildContainer() |
43
|
|
|
{ |
44
|
|
|
$paraunit = new CoverageConfiguration(true); |
45
|
|
|
$input = $this->prophesize(InputInterface::class); |
46
|
|
|
$output = $this->prophesize(OutputInterface::class); |
47
|
|
|
$input->getArgument('stringFilter') |
48
|
|
|
->willReturn('text'); |
49
|
|
|
$input->getOption('parallel') |
50
|
|
|
->willReturn(10); |
51
|
|
|
$input->getOption('testsuite') |
52
|
|
|
->willReturn('testsuite'); |
53
|
|
|
$input->getOption('configuration') |
54
|
|
|
->willReturn($this->getConfigForStubs()); |
55
|
|
|
$input->getOption(Argument::cetera()) |
56
|
|
|
->willReturn(null); |
57
|
|
|
$input->hasParameterOption(Argument::cetera()) |
58
|
|
|
->willReturn(false); |
59
|
|
|
|
60
|
|
|
$container = $paraunit->buildContainer($input->reveal(), $output->reveal()); |
61
|
|
|
|
62
|
|
|
$this->assertInstanceOf(ContainerBuilder::class, $container); |
63
|
|
|
|
64
|
|
|
$requiredParameters = [ |
65
|
|
|
'paraunit.max_process_count' => 10, |
66
|
|
|
'paraunit.testsuite' => 'testsuite', |
67
|
|
|
'paraunit.string_filter' => 'text', |
68
|
|
|
'paraunit.phpunit_config_filename' => $this->getConfigForStubs(), |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
View Code Duplication |
foreach ($requiredParameters as $parameterName => $expectedValue) { |
|
|
|
|
72
|
|
|
$this->assertTrue($container->hasParameter($parameterName), 'Parameter missing: ' . $parameterName); |
73
|
|
|
$this->assertEquals($expectedValue, $container->getParameter($parameterName)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$requiredDefinitions = [ |
77
|
|
|
OutputInterface::class, |
78
|
|
|
LogParser::class, |
79
|
|
|
ProcessPrinter::class, |
80
|
|
|
ProcessFactoryInterface::class, |
81
|
|
|
Runner::class, |
82
|
|
|
EventDispatcherInterface::class, |
83
|
|
|
TestResultFactory::class, |
84
|
|
|
'paraunit.test_result.pass_container', |
85
|
|
|
'paraunit.test_result.pass_format', |
86
|
|
|
|
87
|
|
|
CoverageFetcher::class, |
88
|
|
|
CoverageMerger::class, |
89
|
|
|
CoverageResult::class, |
90
|
|
|
PHPDbgBinFile::class, |
91
|
|
|
CoveragePrinter::class, |
92
|
|
|
PHPUnitConfig::class, |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
foreach ($requiredDefinitions as $definitionName) { |
96
|
|
|
// test instantiation, to prevent misconfigurations |
97
|
|
|
$this->getService($container, $definitionName); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testBuildContainerWithDebug() |
102
|
|
|
{ |
103
|
|
|
$paraunit = new CoverageConfiguration(true); |
104
|
|
|
$input = $this->prophesize(InputInterface::class); |
105
|
|
|
$output = $this->prophesize(OutputInterface::class); |
106
|
|
|
$input->getArgument('stringFilter') |
107
|
|
|
->willReturn('text'); |
108
|
|
|
$input->getOption('debug') |
109
|
|
|
->willReturn(true); |
110
|
|
|
$input->getOption(Argument::cetera()) |
111
|
|
|
->willReturn(null); |
112
|
|
|
$input->hasParameterOption(Argument::cetera()) |
113
|
|
|
->willReturn(false); |
114
|
|
|
|
115
|
|
|
$container = $paraunit->buildContainer($input->reveal(), $output->reveal()); |
116
|
|
|
|
117
|
|
|
$this->assertInstanceOf(ContainerBuilder::class, $container); |
118
|
|
|
|
119
|
|
|
$service = $this->getService($container, DebugPrinter::class); // test instantiation, to prevent misconfigurations |
120
|
|
|
$this->assertInstanceOf(DebugPrinter::class, $service); |
121
|
|
|
$this->assertInstanceOf(EventSubscriberInterface::class, $service); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @dataProvider cliOptionsProvider |
126
|
|
|
*/ |
127
|
|
|
public function testBuildContainerWithCoverageSettings(string $inputOption, string $processorClass) |
128
|
|
|
{ |
129
|
|
|
$paraunit = new CoverageConfiguration(true); |
130
|
|
|
$input = $this->prophesize(InputInterface::class); |
131
|
|
|
$output = $this->prophesize(OutputInterface::class); |
132
|
|
|
$options = [ |
133
|
|
|
'testsuite', |
134
|
|
|
'configuration', |
135
|
|
|
'clover', |
136
|
|
|
'xml', |
137
|
|
|
'html', |
138
|
|
|
'text', |
139
|
|
|
'text-summary', |
140
|
|
|
'crap4j', |
141
|
|
|
'php', |
142
|
|
|
'ansi', |
143
|
|
|
'logo', |
144
|
|
|
]; |
145
|
|
|
|
146
|
|
|
foreach ($options as $optionName) { |
147
|
|
|
$input->getOption($optionName) |
148
|
|
|
->willReturn($optionName === $inputOption ? 'someValue' : null); |
149
|
|
|
$input->hasParameterOption('--' . $optionName) |
150
|
|
|
->willReturn($optionName === $inputOption); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$input->getArgument('stringFilter') |
154
|
|
|
->willReturn(); |
155
|
|
|
$input->getOption('parallel') |
156
|
|
|
->shouldBeCalled() |
157
|
|
|
->willReturn(10); |
158
|
|
|
$input->getOption('debug') |
159
|
|
|
->willReturn(null); |
160
|
|
|
|
161
|
|
|
$container = $paraunit->buildContainer($input->reveal(), $output->reveal()); |
162
|
|
|
|
163
|
|
|
$this->assertInstanceOf(ContainerBuilder::class, $container); |
164
|
|
|
|
165
|
|
|
$coverageResult = $this->getService($container, CoverageResult::class); |
166
|
|
|
$reflection = new \ReflectionObject($coverageResult); |
167
|
|
|
$property = $reflection->getProperty('coverageProcessors'); |
168
|
|
|
$property->setAccessible(true); |
169
|
|
|
$processors = $property->getValue($coverageResult); |
170
|
|
|
|
171
|
|
|
$this->assertCount(1, $processors, 'Wrong count of coverage processors'); |
172
|
|
|
$this->assertInstanceOf($processorClass, $processors[0]); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function cliOptionsProvider(): array |
176
|
|
|
{ |
177
|
|
|
return [ |
178
|
|
|
['clover', Clover::class], |
179
|
|
|
['xml', Xml::class], |
180
|
|
|
['html', Html::class], |
181
|
|
|
['text', Text::class], |
182
|
|
|
['text-summary', TextSummary::class], |
183
|
|
|
['crap4j', Crap4j::class], |
184
|
|
|
['php', Php::class], |
185
|
|
|
]; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testBuildContainerWithColoredTextToConsoleCoverage() |
189
|
|
|
{ |
190
|
|
|
$paraunit = new CoverageConfiguration(true); |
191
|
|
|
$input = $this->prophesize(InputInterface::class); |
192
|
|
|
$output = $this->prophesize(OutputInterface::class); |
193
|
|
|
$options = [ |
194
|
|
|
'testsuite', |
195
|
|
|
'configuration', |
196
|
|
|
'clover', |
197
|
|
|
'xml', |
198
|
|
|
'html', |
199
|
|
|
'text', |
200
|
|
|
'crap4j', |
201
|
|
|
'php', |
202
|
|
|
]; |
203
|
|
|
|
204
|
|
|
foreach ($options as $optionName) { |
205
|
|
|
$input->getOption($optionName) |
206
|
|
|
->willReturn(null); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$input->getArgument('stringFilter') |
210
|
|
|
->willReturn(); |
211
|
|
|
$input->getOption('parallel') |
212
|
|
|
->shouldBeCalled() |
213
|
|
|
->willReturn(10); |
214
|
|
|
$input->getOption('text-summary') |
215
|
|
|
->shouldBeCalled() |
216
|
|
|
->willReturn(true); |
217
|
|
|
$input->getOption('ansi') |
218
|
|
|
->shouldBeCalled() |
219
|
|
|
->willReturn(true); |
220
|
|
|
$input->getOption('debug') |
221
|
|
|
->willReturn(null); |
222
|
|
|
$input->getOption('logo') |
223
|
|
|
->willReturn(false); |
224
|
|
|
$input->hasParameterOption('--text-summary') |
225
|
|
|
->willReturn(true); |
226
|
|
|
$input->hasParameterOption(Argument::cetera()) |
227
|
|
|
->willReturn(false); |
228
|
|
|
|
229
|
|
|
$container = $paraunit->buildContainer($input->reveal(), $output->reveal()); |
230
|
|
|
|
231
|
|
|
$this->assertInstanceOf(ContainerBuilder::class, $container); |
232
|
|
|
|
233
|
|
|
$coverageResult = $this->getService($container, CoverageResult::class); |
234
|
|
|
$reflection = new \ReflectionObject($coverageResult); |
235
|
|
|
$property = $reflection->getProperty('coverageProcessors'); |
236
|
|
|
$property->setAccessible(true); |
237
|
|
|
$processors = $property->getValue($coverageResult); |
238
|
|
|
|
239
|
|
|
$this->assertCount(1, $processors, 'Wrong count of coverage processors'); |
240
|
|
|
$processor = $processors[0]; |
241
|
|
|
$this->assertInstanceOf(TextSummary::class, $processor); |
242
|
|
|
|
243
|
|
|
$reflection = new \ReflectionClass(AbstractText::class); |
244
|
|
|
$property = $reflection->getProperty('showColors'); |
245
|
|
|
$property->setAccessible(true); |
246
|
|
|
$this->assertTrue($property->getValue($processor)); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
private function getService(ContainerBuilder $container, string $serviceName) |
250
|
|
|
{ |
251
|
|
|
return $container->get(sprintf(CoverageConfiguration::PUBLIC_ALIAS_FORMAT, $serviceName)); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
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.