forked from infection/infection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoverageCheckerTest.php
611 lines (535 loc) · 19.9 KB
/
CoverageCheckerTest.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
<?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\TestFramework\Coverage;
use function extension_loaded;
use Infection\FileSystem\Locator\FileNotFound;
use Infection\TestFramework\Coverage\CoverageChecker;
use Infection\TestFramework\Coverage\CoverageNotFound;
use Infection\TestFramework\Coverage\JUnit\JUnitReportLocator;
use Infection\TestFramework\Coverage\XmlReport\IndexXmlCoverageLocator;
use Infection\TestFramework\PhpUnit\Adapter\PhpUnitAdapter;
use const PHP_SAPI;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use function Safe\sprintf;
use Symfony\Component\Filesystem\Path;
/**
* All these tests should be ran in separate processes, as otherwise they may rely
* on the internal state of XdebugHandler.
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*
* @group integration
* Requires some I/O operations
*/
final class CoverageCheckerTest extends TestCase
{
/**
* @var string
*/
private static $coveragePath;
/**
* @var string
*/
private static $jUnit;
public static function setUpBeforeClass(): void
{
self::$coveragePath = Path::canonicalize(__DIR__ . '/../../Fixtures/Files/phpunit/coverage');
self::$jUnit = Path::canonicalize(__DIR__ . '/../../Fixtures/Files/phpunit/junit.xml');
}
public function test_it_needs_coverage_to_be_provided_if_initial_tests_are_skipped_without_junit_report(): void
{
$checker = new CoverageChecker(
false,
true,
'',
'',
false,
$this->createFakeJUnitReportLocatorMock(),
'unknown',
$this->createFakeIndexLocatorMock()
);
$this->expectException(CoverageNotFound::class);
$this->expectExceptionMessage(
'The initial test suite run is being skipped. The XML reports need to be provided with '
. 'the "--coverage" option'
);
$checker->checkCoverageRequirements();
}
public function test_it_needs_coverage_to_be_provided_if_initial_tests_are_skipped_with_junit_report(): void
{
$checker = new CoverageChecker(
false,
true,
'',
'',
true,
$this->createJUnitReportLocatorMock(),
'unknown',
$this->createFakeIndexLocatorMock()
);
$this->expectException(CoverageNotFound::class);
$this->expectExceptionMessage(
'The initial test suite run is being skipped. The XML and JUnit coverage reports need '
. 'to be provided with the "--coverage" option'
);
$checker->checkCoverageRequirements();
}
public function test_it_needs_code_coverage_generator_enabled_if_coverage_is_not_provided(): void
{
$this->requireNoPcov();
$this->requireNoPhpDbg();
$this->requireNoXdebug();
$checker = new CoverageChecker(
false,
false,
'',
'',
false,
$this->createFakeJUnitReportLocatorMock(),
'unknown',
$this->createFakeIndexLocatorMock()
);
$this->expectException(CoverageNotFound::class);
$this->expectExceptionMessage(<<<TXT
Coverage needs to be generated but no code coverage generator (pcov, phpdbg or xdebug) has been detected. Please either:
- Enable pcov and run Infection again
- Use phpdbg, e.g. `phpdbg -qrr infection`
- Enable Xdebug (in case of using Xdebug 3 check that `xdebug.mode` or environment variable XDEBUG_MODE set to `coverage`) and run Infection again
- Use the "--coverage" option with path to the existing coverage report
- Enable the code generator tool for the initial test run only, e.g. with `--initial-tests-php-options -d zend_extension=xdebug.so`
TXT
);
$checker->checkCoverageRequirements();
}
public function test_it_passes_existence_check_if_xml_index_and_junit_files_are_found_with_junit_report(): void
{
$checker = new CoverageChecker(
false,
false,
'',
self::$coveragePath,
true,
$this->createJUnitReportLocatorMock(),
'unknown',
$this->createIndexLocatorMock(self::$coveragePath . '/index.xml')
);
$checker->checkCoverageExists();
$this->addToAssertionCount(1);
}
public function test_it_does_not_pass_existence_check_if_xml_index_is_missing_with_lambda_test_framework_adapter(): void
{
$checker = new CoverageChecker(
false,
false,
'',
'/nowhere',
true,
$this->createJUnitReportLocatorMock(),
'unknown',
$this->createInvalidIndexLocatorMock()
);
try {
$checker->checkCoverageExists();
$this->fail();
} catch (CoverageNotFound $exception) {
$this->assertSame(
'Could not find the "index.xml" file. Please ensure that the XML coverage '
. 'report has been properly generated at the right place.',
$exception->getMessage()
);
$this->assertSame(0, $exception->getCode());
$this->assertInstanceOf(FileNotFound::class, $exception->getPrevious());
}
}
public function test_it_does_not_pass_existence_check_if_xml_index_is_missing_with_phpunit(): void
{
$checker = new CoverageChecker(
false,
false,
'',
'/nowhere',
true,
$this->createJUnitReportLocatorMock(),
'phpunit',
$this->createInvalidIndexLocatorMock()
);
try {
$checker->checkCoverageExists();
$this->fail();
} catch (CoverageNotFound $exception) {
$this->assertSame(
'Could not find the "index.xml" file. Please ensure that the XML coverage '
. 'report has been properly generated at the right place. The PHPUnit option for the '
. 'path given is "--coverage-xml=/nowhere/coverage-xml"',
$exception->getMessage()
);
$this->assertSame(0, $exception->getCode());
$this->assertInstanceOf(FileNotFound::class, $exception->getPrevious());
}
}
public function test_it_does_not_pass_existence_check_if_xml_index_is_missing_with_codeception(): void
{
$checker = new CoverageChecker(
false,
false,
'',
'/nowhere',
true,
$this->createJUnitReportLocatorMock(),
'codeception',
$this->createInvalidIndexLocatorMock()
);
try {
$checker->checkCoverageExists();
$this->fail();
} catch (CoverageNotFound $exception) {
$this->assertSame(
'Could not find the "index.xml" file. Please ensure that the XML coverage '
. 'report has been properly generated at the right place. The Codeception option for the'
. ' path given is "--coverage-phpunit=/nowhere/coverage-xml"',
$exception->getMessage()
);
$this->assertSame(0, $exception->getCode());
$this->assertInstanceOf(FileNotFound::class, $exception->getPrevious());
}
}
public function test_it_passes_existence_check_if_xml_index_is_present_and_junit_file_is_missing_without_junit_report(): void
{
$checker = new CoverageChecker(
false,
false,
'',
self::$coveragePath,
false,
$this->createFakeJUnitReportLocatorMock(),
'unknown',
$this->createIndexLocatorMock(self::$coveragePath . '/index.xml')
);
$checker->checkCoverageExists();
$this->addToAssertionCount(1);
}
public function test_it_does_not_pass_existence_check_if_junit_file_is_missing_with_junit_report_with_lambda_test_framework_adapter(): void
{
$checker = new CoverageChecker(
false,
false,
'',
self::$coveragePath,
true,
$this->createInvalidJUnitReportLocatorMock(),
'unknown',
$this->createIndexLocatorMock(self::$coveragePath . '/index.xml')
);
try {
$checker->checkCoverageExists();
$this->fail();
} catch (CoverageNotFound $exception) {
$this->assertSame(
sprintf(
'Could not find the JUnit file report. Please ensure that the JUnit coverage '
. 'report has been properly generated at the right place.',
self::$coveragePath
),
$exception->getMessage()
);
$this->assertSame(0, $exception->getCode());
$this->assertInstanceOf(FileNotFound::class, $exception->getPrevious());
}
}
public function test_it_does_not_pass_existence_check_if_junit_file_is_missing_with_junit_report_with_phpunit_test_framework_adapter(): void
{
$phpUnitAdapterMock = $this->createMock(PhpUnitAdapter::class);
$phpUnitAdapterMock->method('hasJUnitReport')->willReturn(true);
$checker = new CoverageChecker(
false,
false,
'',
self::$coveragePath,
true,
$this->createInvalidJUnitReportLocatorMock(),
'phpunit',
$this->createIndexLocatorMock(self::$coveragePath . '/index.xml')
);
try {
$checker->checkCoverageExists();
$this->fail();
} catch (CoverageNotFound $exception) {
$this->assertSame(
sprintf(
'Could not find the JUnit file report. Please ensure that the JUnit coverage '
. 'report has been properly generated at the right place. The PHPUnit option for the '
. 'path given is "--log-junit=%s/junit.xml"',
self::$coveragePath
),
$exception->getMessage()
);
$this->assertSame(0, $exception->getCode());
$this->assertInstanceOf(FileNotFound::class, $exception->getPrevious());
}
}
public function test_it_does_not_pass_existence_check_if_junit_file_is_missing_with_junit_report_with_codeception_test_framework_adapter(): void
{
$checker = new CoverageChecker(
false,
false,
'',
self::$coveragePath,
true,
$this->createInvalidJUnitReportLocatorMock(),
'codeception',
$this->createIndexLocatorMock(self::$coveragePath . '/index.xml')
);
try {
$checker->checkCoverageExists();
$this->fail();
} catch (CoverageNotFound $exception) {
$this->assertSame(
sprintf(
'Could not find the JUnit file report. Please ensure that the JUnit coverage report has'
. ' been properly generated at the right place. The Codeception option for the path '
. 'given is "--xml"',
self::$coveragePath
),
$exception->getMessage()
);
$this->assertSame(0, $exception->getCode());
$this->assertInstanceOf(FileNotFound::class, $exception->getPrevious());
}
}
public function test_it_passes_existence_check_if_xml_index_and_junit_files_are_found_after_tests_run_with_junit_report(): void
{
$checker = new CoverageChecker(
false,
false,
'',
self::$coveragePath,
true,
$this->createJUnitReportLocatorMock(),
'unknown',
$this->createIndexLocatorMock(self::$coveragePath . '/index.xml')
);
$checker->checkCoverageHasBeenGenerated(
'bin/phpunit --coverage-xml=coverage/coverage-xml --log-junit=coverage=junit.xml',
'Ok!'
);
$this->addToAssertionCount(1);
}
public function test_it_passes_existence_check_if_xml_index_and_junit_files_are_found_after_tests_run_without_junit_report(): void
{
$checker = new CoverageChecker(
false,
false,
'',
self::$coveragePath,
true,
$this->createJUnitReportLocatorMock(),
'unknown',
$this->createIndexLocatorMock(self::$coveragePath . '/index.xml')
);
$checker->checkCoverageHasBeenGenerated(
'bin/phpunit --coverage-xml=coverage/coverage-xml --log-junit=coverage=junit.xml',
'Ok!'
);
$this->addToAssertionCount(1);
}
public function test_it_does_not_pass_existence_check_if_xml_index_is_missing_after_tests_run(): void
{
$checker = new CoverageChecker(
false,
false,
'',
'/nowhere',
true,
$this->createJUnitReportLocatorMock(),
'unknown',
$this->createInvalidIndexLocatorMock()
);
$this->expectException(CoverageNotFound::class);
$this->expectExceptionMessage(<<<'TXT'
The code coverage generated by the initial test run is invalid. Please report the issue on the
infection repository "https://github.com/infection/infection".
```
$ bin/phpunit --coverage-xml=coverage/coverage-xml --log-junit=coverage=junit.xml
Ok!
```
Issue(s):
- The file "index.xml" could not be found: No index file found
TXT
);
$checker->checkCoverageHasBeenGenerated(
'bin/phpunit --coverage-xml=coverage/coverage-xml --log-junit=coverage=junit.xml',
'Ok!'
);
}
public function test_it_does_not_pass_existence_check_if_junit_file_is_missing_after_tests_run_with_junit_report(): void
{
$coveragePath = Path::canonicalize(self::$coveragePath);
$checker = new CoverageChecker(
false,
false,
'',
$coveragePath,
true,
$this->createInvalidJUnitReportLocatorMock(),
'unknown',
$this->createIndexLocatorMock(self::$coveragePath . '/index.xml')
);
$this->expectException(CoverageNotFound::class);
$this->expectExceptionMessage(<<<'TXT'
The code coverage generated by the initial test run is invalid. Please report the issue on the
infection repository "https://github.com/infection/infection".
```
$ bin/phpunit --coverage-xml=coverage/coverage-xml --log-junit=coverage=junit.xml
Ok!
```
Issue(s):
- The JUnit file could not be found: No JUnit file found
TXT
);
$checker->checkCoverageHasBeenGenerated(
'bin/phpunit --coverage-xml=coverage/coverage-xml --log-junit=coverage=junit.xml',
'Ok!'
);
}
public function test_it_passes_existence_check_if_xml_index_is_found_and_junit_file_is_missing_after_tests_run_without_junit_report(): void
{
$checker = new CoverageChecker(
false,
false,
'',
self::$coveragePath,
false,
$this->createFakeJUnitReportLocatorMock(),
'unknown',
$this->createIndexLocatorMock(self::$coveragePath . '/index.xml')
);
$checker->checkCoverageHasBeenGenerated(
'bin/phpunit --coverage-xml=coverage/coverage-xml --log-junit=coverage=junit.xml',
'Ok!'
);
$this->addToAssertionCount(1);
}
private function requireNoPhpDbg(): void
{
if (PHP_SAPI === 'phpdbg') {
$this->markTestSkipped('Test requires phpdbg to be disabled to run.');
}
}
private function requireNoXdebug(): void
{
if (extension_loaded('xdebug')) {
$this->markTestSkipped('Test requires xdebug to be disabled to run.');
}
}
private function requireNoPcov(): void
{
if (extension_loaded('pcov')) {
$this->markTestSkipped('Test requires pcov to be disabled to run.');
}
}
/**
* @return IndexXmlCoverageLocator|MockObject
*/
private function createFakeIndexLocatorMock(): IndexXmlCoverageLocator
{
$indexLocatorMock = $this->createMock(IndexXmlCoverageLocator::class);
$indexLocatorMock
->expects($this->never())
->method($this->anything())
;
return $indexLocatorMock;
}
/**
* @return JUnitReportLocator|MockObject
*/
private function createFakeJUnitReportLocatorMock(): JUnitReportLocator
{
$jUnitLocatorMock = $this->createMock(JUnitReportLocator::class);
$jUnitLocatorMock
->expects($this->never())
->method($this->anything())
;
return $jUnitLocatorMock;
}
/**
* @return IndexXmlCoverageLocator|MockObject
*/
private function createInvalidIndexLocatorMock(): IndexXmlCoverageLocator
{
$indexLocatorMock = $this->createMock(IndexXmlCoverageLocator::class);
$indexLocatorMock
->method('locate')
->willThrowException(new FileNotFound('No index file found'))
;
return $indexLocatorMock;
}
/**
* @return IndexXmlCoverageLocator|MockObject
*/
private function createIndexLocatorMock(string $indexPath): IndexXmlCoverageLocator
{
$indexLocatorMock = $this->createMock(IndexXmlCoverageLocator::class);
$indexLocatorMock
->method('locate')
->willReturn($indexPath)
;
return $indexLocatorMock;
}
/**
* @return JUnitReportLocator|MockObject
*/
private function createInvalidJUnitReportLocatorMock(): JUnitReportLocator
{
$jUnitLocatorMock = $this->createMock(JUnitReportLocator::class);
$jUnitLocatorMock
->method('locate')
->willThrowException(new FileNotFound('No JUnit file found'))
;
return $jUnitLocatorMock;
}
/**
* @return JUnitReportLocator|MockObject
*/
private function createJUnitReportLocatorMock(): JUnitReportLocator
{
$jUnitLocatorMock = $this->createMock(JUnitReportLocator::class);
$jUnitLocatorMock
->method('locate')
->willReturn(self::$jUnit)
;
return $jUnitLocatorMock;
}
}