-
-
Notifications
You must be signed in to change notification settings - Fork 155
/
StrykerHtmlReportBuilderTest.php
495 lines (473 loc) · 21.4 KB
/
StrykerHtmlReportBuilderTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
declare(strict_types=1);
namespace Infection\Tests\Logger\Html;
use function array_map;
use function implode;
use Infection\AbstractTestFramework\Coverage\TestLocation;
use Infection\Logger\Html\StrykerHtmlReportBuilder;
use Infection\Metrics\Collector;
use Infection\Metrics\MetricsCalculator;
use Infection\Metrics\ResultsCollector;
use Infection\Mutant\DetectionStatus;
use Infection\Mutant\MutantExecutionResult;
use Infection\Mutator\FunctionSignature\PublicVisibility;
use Infection\Mutator\Removal\ArrayItemRemoval;
use Infection\Mutator\Removal\MethodCallRemoval;
use Infection\Tests\Mutator\MutatorName;
use function Infection\Tests\normalize_trailing_spaces;
use JsonSchema\Validator;
use function Later\now;
use const PHP_EOL;
use PHPUnit\Framework\TestCase;
use function Safe\base64_decode;
use function Safe\file_get_contents;
use function Safe\json_decode;
use function Safe\json_encode;
use function Safe\realpath;
use function Safe\sprintf;
/**
* @group integration
*/
final class StrykerHtmlReportBuilderTest extends TestCase
{
private const SCHEMA_FILE = 'file://' . __DIR__ . '/../../../../resources/mutation-testing-report-schema.json';
/**
* @dataProvider metricsProvider
*/
public function test_it_logs_correctly_with_mutations(
MetricsCalculator $metricsCalculator,
ResultsCollector $resultsCollector,
array $expectedReport
): void {
$report = (new StrykerHtmlReportBuilder($metricsCalculator, $resultsCollector))->build();
$this->assertSame($expectedReport, json_decode(json_encode($report), true));
$this->assertJsonDocumentMatchesSchema($report);
}
public function metricsProvider()
{
yield 'no mutations' => [
new MetricsCalculator(2),
new ResultsCollector(),
[
'schemaVersion' => '1',
'thresholds' => [
'high' => 90,
'low' => 50,
],
'files' => [],
'testFiles' => [],
'framework' => [
'name' => 'Infection',
'branding' => [
'homepageUrl' => 'https://infection.github.io/',
'imageUrl' => 'https://infection.github.io/images/logo.png',
],
],
],
];
$realPathForHtmlReport = realpath(__DIR__ . '/../../Fixtures/ForHtmlReport.php');
$realPathForHtmlReport2 = realpath(__DIR__ . '/../../Fixtures/ForHtmlReport2.php');
yield 'different mutations' => [
$this->createFullHtmlReportMetricsCalculator(),
$this->createFullHtmlReportResultsCollector(),
[
'schemaVersion' => '1',
'thresholds' => [
'high' => 90,
'low' => 50,
],
'files' => [
'ForHtmlReport.php' => [
'language' => 'php',
'source' => file_get_contents($realPathForHtmlReport),
'mutants' => [
[
'id' => '32f68ca331c9262cc97322271d88d06d',
'mutatorName' => 'PublicVisibility',
'replacement' => 'protected function add(int $a, int $b) : int',
'description' => 'Replaces the `public` method visibility keyword with `protected`.',
'location' => ['start' => ['line' => 13, 'column' => 5], 'end' => ['line' => 13, 'column' => 45]],
'status' => 'Killed',
'statusReason' => 'PHPUnit output. Tests: 1, Assertions: 3',
'coveredBy' => ['06a6c58caae5aa33e9b787f064618f5e'],
'killedBy' => [],
'testsCompleted' => 1,
],
[
'id' => 'fd66aff56e903645c21271264b062b4f',
'mutatorName' => 'MethodCallRemoval',
'replacement' => '',
'description' => 'Removes the method call.',
'location' => ['start' => ['line' => 15, 'column' => 9], 'end' => ['line' => 15, 'column' => 27]],
'status' => 'Survived',
'statusReason' => 'PHPUnit output. Tests: 1, Assertions: 3. Failure: 1) TestClass::test_method1 Failed',
'coveredBy' => ['06a6c58caae5aa33e9b787f064618f5e'],
'killedBy' => ['06a6c58caae5aa33e9b787f064618f5e'],
'testsCompleted' => 1,
],
[
'id' => '746519c01522ddc7da799a9b7927e4c2',
'mutatorName' => 'MethodCallRemoval',
'replacement' => '',
'description' => 'Removes the method call.',
'location' => ['start' => ['line' => 17, 'column' => 9], 'end' => ['line' => 19, 'column' => 11]],
'status' => 'Survived',
'statusReason' => 'PHPUnit output. Tests: 1, Assertions: 3. Failure: 1) TestClass::test_method1 with data set #1',
'coveredBy' => ['2b67abde50b026f4057311ea32409632'],
'killedBy' => ['2b67abde50b026f4057311ea32409632'],
'testsCompleted' => 1,
],
[
'id' => '633b144fb6d55bbc60430df68a952388',
'mutatorName' => 'ArrayItemRemoval',
'replacement' => '$this->innerArray(array_keys([\'b\' => \'2\']));',
'description' => "Removes an element of an array literal. For example:\n\n```php\n\$x = [0, 1, 2];\n```\n\nWill be mutated to:\n\n```php\n\$x = [1, 2];\n```\n\nAnd:\n\n```php\n\$x = [0, 2];\n```\n\nAnd:\n\n```php\n\$x = [0, 1];\n```\n\nWhich elements it removes or how many elements it will attempt to remove will depend on its\nconfiguration.\n",
'location' => ['start' => ['line' => 28, 'column' => 9], 'end' => ['line' => 28, 'column' => 65]],
'status' => 'Survived',
'statusReason' => 'PHPUnit output. Tests: 3, Assertions: 3',
'coveredBy' => ['06a6c58caae5aa33e9b787f064618f5e', '949bee6dd4ac608462995babbe81ee12', '2733f8c97b5ba92b1aacb77d46837b0e'],
'killedBy' => [],
'testsCompleted' => 3,
],
],
],
'ForHtmlReport2.php' => [
'language' => 'php',
'source' => file_get_contents($realPathForHtmlReport2),
'mutants' => [
[
'id' => '12f68ca331c9262cc97322271d88d06d',
'mutatorName' => 'PublicVisibility',
'replacement' => 'protected function add(int $a, int $b) : int',
'description' => 'Replaces the `public` method visibility keyword with `protected`.',
'location' => ['start' => ['line' => 13, 'column' => 5], 'end' => ['line' => 13, 'column' => 6]],
'status' => 'Killed',
'statusReason' => 'Output without ability to detect the number of executed tests',
'coveredBy' => ['06a6c58caae5aa33e9b787f064618f5e'],
'killedBy' => [],
'testsCompleted' => 0,
],
[
'id' => '22f68ca331c9262cc97322271d88d06d',
'mutatorName' => 'PublicVisibility',
'replacement' => 'protected function add(int $a, int $b) : int',
'description' => 'Replaces the `public` method visibility keyword with `protected`.',
'location' => ['start' => ['line' => 13, 'column' => 5], 'end' => ['line' => 13, 'column' => 6]],
'status' => 'Killed',
'statusReason' => 'Output without ability to detect the number of executed testsi?',
'coveredBy' => ['06a6c58caae5aa33e9b787f064618f5e'],
'killedBy' => [],
'testsCompleted' => 0,
],
],
],
],
'testFiles' => [
'/infection/path/to/TestClass.php' => [
'tests' => [
[
'id' => '06a6c58caae5aa33e9b787f064618f5e',
'name' => 'TestClass::test_method1',
],
[
'id' => '2b67abde50b026f4057311ea32409632',
'name' => 'TestClass::test_method1 with data set #1',
],
],
],
'/infection/path/to/TestClass2.php' => [
'tests' => [
[
'id' => '949bee6dd4ac608462995babbe81ee12',
'name' => 'TestClass2::test_method2',
],
[
'id' => '2733f8c97b5ba92b1aacb77d46837b0e',
'name' => 'TestClass2::test_method3',
],
],
],
],
'framework' => [
'name' => 'Infection',
'branding' => [
'homepageUrl' => 'https://infection.github.io/',
'imageUrl' => 'https://infection.github.io/images/logo.png',
],
],
],
];
}
private function createFullHtmlReportMetricsCalculator(): MetricsCalculator
{
$collector = new MetricsCalculator(2);
$this->initHtmlReportCollector($collector);
return $collector;
}
private function createFullHtmlReportResultsCollector(): ResultsCollector
{
$collector = new ResultsCollector();
$this->initHtmlReportCollector($collector);
return $collector;
}
private function assertJsonDocumentMatchesSchema($report): void
{
$resultReport = json_decode(json_encode($report));
$validator = new Validator();
$validator->validate($resultReport, (object) ['$ref' => self::SCHEMA_FILE]);
$normalizedErrors = array_map(
static function (array $error): string {
return sprintf('[%s] %s%s', $error['property'], $error['message'], PHP_EOL);
},
$validator->getErrors()
);
$this->assertTrue(
$validator->isValid(),
sprintf(
'Expected the given JSON to be valid but is violating the following rules of'
. ' the schema: %s- %s',
PHP_EOL,
implode('- ', $normalizedErrors)
)
);
}
private function initHtmlReportCollector(Collector $collector): void
{
$collector->collect(
// this tests diffs on the method signature line
$this->createMutantExecutionResult(
DetectionStatus::KILLED,
<<<'DIFF'
--- Original
+++ New
@@ @@
use function array_fill_keys;
final class ForHtmlReport
{
- public function add(int $a, int $b) : int
+ protected function add(int $a, int $b) : int
{
$this->inner('3');
$this->inner('3');
DIFF,
'32f68ca331c9262cc97322271d88d06d',
PublicVisibility::class,
realpath(__DIR__ . '/../../Fixtures/ForHtmlReport.php'),
13,
35,
124,
547,
[
new TestLocation('TestClass::test_method1', '/infection/path/to/TestClass.php', 0.123),
// check that duplicate values are moved in the report
new TestLocation('TestClass::test_method1', '/infection/path/to/TestClass.php', 0.123),
]
),
// this tests diff on the one-line method call removal
$this->createMutantExecutionResult(
DetectionStatus::ESCAPED,
<<<'DIFF'
--- Original
+++ New
@@ @@
{
public function add(int $a, int $b) : int
{
- $this->inner('3');
+
$this->inner('3');
switch (true) {
case 1 !== 1:
DIFF,
'fd66aff56e903645c21271264b062b4f',
MethodCallRemoval::class,
realpath(__DIR__ . '/../../Fixtures/ForHtmlReport.php'),
15,
15,
179,
196,
[
new TestLocation('TestClass::test_method1', '/infection/path/to/TestClass.php', 0.123),
],
'PHPUnit output. Tests: 1, Assertions: 3. Failure: 1) TestClass::test_method1 Failed'
),
// this tests diff on the multi-line (in original source code) method call removal
$this->createMutantExecutionResult(
DetectionStatus::ESCAPED,
<<<'DIFF'
--- Original
+++ New
@@ @@
public function add(int $a, int $b) : int
{
$this->inner('3');
- $this->inner('3');
+
switch (true) {
case 0 !== 1:
break;
DIFF,
'746519c01522ddc7da799a9b7927e4c2',
MethodCallRemoval::class,
realpath(__DIR__ . '/../../Fixtures/ForHtmlReport.php'),
17,
19,
207,
246,
[
new TestLocation('TestClass::test_method1 with data set #1', '/infection/path/to/TestClass.php', 0.123),
],
'PHPUnit output. Tests: 1, Assertions: 3. Failure: 1) TestClass::test_method1 with data set #1'
),
// this tests diff on the one-line diff with array item removal
$this->createMutantExecutionResult(
DetectionStatus::ESCAPED,
<<<'DIFF'
--- Original
+++ New
@@ @@
default:
break;
}
- $this->innerArray(array_keys(['a' => '1', 'b' => '2']));
+ $this->innerArray(array_keys(['b' => '2']));
if ($this instanceof ForHtmlReport) {
// ...
}
DIFF,
'633b144fb6d55bbc60430df68a952388',
ArrayItemRemoval::class,
realpath(__DIR__ . '/../../Fixtures/ForHtmlReport.php'),
28,
28,
414,
437,
[
new TestLocation('TestClass::test_method1', '/infection/path/to/TestClass.php', 0.123),
new TestLocation('TestClass2::test_method2', '/infection/path/to/TestClass2.php', 0.456),
new TestLocation('TestClass2::test_method3', '/infection/path/to/TestClass2.php', 0.789),
],
'PHPUnit output. Tests: 3, Assertions: 3'
),
// add one test for the second file
$this->createMutantExecutionResult(
DetectionStatus::KILLED,
<<<'DIFF'
--- Original
+++ New
@@ @@
use function array_fill_keys;
final class ForHtmlReport2
{
- public function add(int $a, int $b) : int
+ protected function add(int $a, int $b) : int
{
return 0;
DIFF,
'12f68ca331c9262cc97322271d88d06d',
PublicVisibility::class,
realpath(__DIR__ . '/../../Fixtures/ForHtmlReport2.php'),
13,
35,
124,
547,
[
new TestLocation('TestClass::test_method1', '/infection/path/to/TestClass.php', 0.123),
],
'Output without ability to detect the number of executed tests'
),
//
// with non UTF-8 character
$this->createMutantExecutionResult(
DetectionStatus::KILLED,
<<<'DIFF'
--- Original
+++ New
@@ @@
use function array_fill_keys;
final class ForHtmlReport2
{
- public function add(int $a, int $b) : int
+ protected function add(int $a, int $b) : int
{
return 0;
DIFF,
'22f68ca331c9262cc97322271d88d06d',
PublicVisibility::class,
realpath(__DIR__ . '/../../Fixtures/ForHtmlReport2.php'),
13,
35,
124,
547,
[
new TestLocation('TestClass::test_method1', '/infection/path/to/TestClass.php', 0.123),
],
'Output without ability to detect the number of executed tests' . base64_decode('abc', true) // produces non UTF-8 character
),
);
}
/**
* @param array<int, TestLocation> $testLocations
*/
private function createMutantExecutionResult(
string $detectionStatus,
string $diff,
string $mutantHash,
string $mutatorClassName,
string $originalFileRealPath,
int $originalStartingLine,
int $originalEndingLine,
int $originalStartFilePosition,
int $originalEndFilePosition,
array $testLocations,
?string $processOutput = 'PHPUnit output. Tests: 1, Assertions: 3'
): MutantExecutionResult {
return new MutantExecutionResult(
'bin/phpunit --configuration infection-tmp-phpunit.xml --filter "tests/Acme/FooTest.php"',
$processOutput,
$detectionStatus,
now(normalize_trailing_spaces($diff)),
$mutantHash,
MutatorName::getName($mutatorClassName),
$originalFileRealPath,
$originalStartingLine,
$originalEndingLine,
$originalStartFilePosition,
$originalEndFilePosition,
now('<?php $a = 1;'),
now('<?php $a = 2;'),
$testLocations
);
}
}