-
-
Notifications
You must be signed in to change notification settings - Fork 154
/
MutationConfigBuilderTest.php
720 lines (617 loc) · 22.1 KB
/
MutationConfigBuilderTest.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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
<?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\PhpUnit\Config\Builder;
use function array_map;
use DOMDocument;
use DOMNode;
use DOMNodeList;
use DOMXPath;
use function escapeshellarg;
use function exec;
use Infection\AbstractTestFramework\Coverage\TestLocation;
use Infection\StreamWrapper\IncludeInterceptor;
use Infection\TestFramework\Coverage\JUnit\JUnitTestCaseSorter;
use Infection\TestFramework\PhpUnit\Config\Builder\MutationConfigBuilder;
use Infection\TestFramework\PhpUnit\Config\Path\PathReplacer;
use Infection\TestFramework\PhpUnit\Config\XmlConfigurationManipulator;
use Infection\Tests\FileSystem\FileSystemTestCase;
use function Infection\Tests\normalizePath as p;
use function iterator_to_array;
use function Safe\file_get_contents;
use function Safe\realpath;
use function Safe\sprintf;
use function simplexml_load_string;
use Symfony\Component\Filesystem\Filesystem;
/**
* @group integration
*/
final class MutationConfigBuilderTest extends FileSystemTestCase
{
public const HASH = 'a1b2c3';
private const FIXTURES = __DIR__ . '/../../../../Fixtures/Files/phpunit';
private const ORIGINAL_FILE_PATH = '/original/file/path';
private const MUTATED_FILE_PATH = '/mutated/file/path';
/**
* @var string
*/
private $projectPath;
/**
* @var MutationConfigBuilder
*/
private $builder;
protected function setUp(): void
{
parent::setUp();
$this->projectPath = p(realpath(self::FIXTURES . '/project-path'));
$this->builder = $this->createConfigBuilder(self::FIXTURES . '/phpunit.xml');
}
public function test_it_builds_and_dump_the_xml_configuration(): void
{
$configurationPath = $this->builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'7.1'
);
$this->assertSame(
$this->tmp . '/phpunitConfiguration.a1b2c3.infection.xml',
$configurationPath
);
$this->assertFileExists($configurationPath);
$xml = file_get_contents($configurationPath);
$this->assertNotFalse(
@simplexml_load_string($xml),
'Expected dumped configuration content to be a valid XML file.'
);
$this->assertFileExists($this->tmp . '/interceptor.autoload.a1b2c3.infection.php');
}
public function test_it_preserves_white_spaces_and_formatting(): void
{
$configurationPath = $this->builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'7.1'
);
$tmp = $this->tmp;
$projectPath = $this->projectPath;
$this->assertSame(
<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright © 2017 Maks Rafalko
~
~ License: https://opensource.org/licenses/BSD-3-Clause New BSD License
-->
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="$tmp/interceptor.autoload.a1b2c3.infection.php" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" syntaxCheck="false" stopOnFailure="true" stderr="false">
<testsuites>
<testsuite name="Infection testsuite with filtered tests"/>
</testsuites>
<filter>
<whitelist>
<directory>$projectPath/src/</directory>
<!--<exclude>-->
<!--<directory>src/*Bundle/Resources</directory>-->
<!--<directory>src/*/*Bundle/Resources</directory>-->
<!--<directory>src/*/Bundle/*Bundle/Resources</directory>-->
<!--</exclude>-->
</whitelist>
</filter>
</phpunit>
XML
,
file_get_contents($configurationPath)
);
}
public function test_it_can_build_the_config_for_multiple_mutations(): void
{
$tmp = $this->tmp;
$projectPath = $this->projectPath;
$interceptorPath = IncludeInterceptor::LOCATION;
$this->assertSame(
<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright © 2017 Maks Rafalko
~
~ License: https://opensource.org/licenses/BSD-3-Clause New BSD License
-->
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="$tmp/interceptor.autoload.hash1.infection.php" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" syntaxCheck="false" stopOnFailure="true" stderr="false">
<testsuites>
<testsuite name="Infection testsuite with filtered tests">
<file>/path/to/FooTest.php</file>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>$projectPath/src/</directory>
<!--<exclude>-->
<!--<directory>src/*Bundle/Resources</directory>-->
<!--<directory>src/*/*Bundle/Resources</directory>-->
<!--<directory>src/*/Bundle/*Bundle/Resources</directory>-->
<!--</exclude>-->
</whitelist>
</filter>
</phpunit>
XML
,
file_get_contents(
$this->builder->build(
[
new TestLocation(
'FooTest::test_foo',
'/path/to/FooTest.php',
1.
),
],
self::MUTATED_FILE_PATH,
'hash1',
self::ORIGINAL_FILE_PATH,
'7.1'
)
)
);
$phpCode = file_get_contents($this->tmp . '/interceptor.autoload.hash1.infection.php');
$this->assertSame(
<<<PHP
<?php
if (function_exists('proc_nice')) {
proc_nice(1);
}
require_once '$interceptorPath';
use Infection\StreamWrapper\IncludeInterceptor;
IncludeInterceptor::intercept('/original/file/path', '/mutated/file/path');
IncludeInterceptor::enable();
require_once '$projectPath/app/autoload2.php';
PHP
,
$phpCode
);
$this->assertPHPSyntaxIsValid($phpCode);
$this->assertSame(
<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright © 2017 Maks Rafalko
~
~ License: https://opensource.org/licenses/BSD-3-Clause New BSD License
-->
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="$tmp/interceptor.autoload.hash2.infection.php" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" syntaxCheck="false" stopOnFailure="true" stderr="false">
<testsuites>
<testsuite name="Infection testsuite with filtered tests">
<file>/path/to/BarTest.php</file>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>$projectPath/src/</directory>
<!--<exclude>-->
<!--<directory>src/*Bundle/Resources</directory>-->
<!--<directory>src/*/*Bundle/Resources</directory>-->
<!--<directory>src/*/Bundle/*Bundle/Resources</directory>-->
<!--</exclude>-->
</whitelist>
</filter>
</phpunit>
XML
,
file_get_contents(
$this->builder->build(
[
new TestLocation(
'BarTest::test_bar_1',
'/path/to/BarTest.php',
1.
),
],
self::MUTATED_FILE_PATH,
'hash2',
self::ORIGINAL_FILE_PATH,
'7.1'
)
)
);
$phpCode = file_get_contents($this->tmp . '/interceptor.autoload.hash2.infection.php');
$this->assertSame(
<<<PHP
<?php
if (function_exists('proc_nice')) {
proc_nice(1);
}
require_once '$interceptorPath';
use Infection\StreamWrapper\IncludeInterceptor;
IncludeInterceptor::intercept('/original/file/path', '/mutated/file/path');
IncludeInterceptor::enable();
require_once '$projectPath/app/autoload2.php';
PHP
,
$phpCode
);
$this->assertPHPSyntaxIsValid($phpCode);
}
public function test_it_builds_path_to_mutation_config_file(): void
{
$this->assertSame(
$this->tmp . '/phpunitConfiguration.a1b2c3.infection.xml',
$this->builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'7.1'
)
);
}
public function test_it_sets_custom_autoloader(): void
{
$xml = file_get_contents(
$this->builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'7.1'
)
);
$resultAutoLoaderFilePath = $this->queryXpath($xml, '/phpunit/@bootstrap')[0]->nodeValue;
$expectedCustomAutoloadFilePath = sprintf(
'%s/interceptor.autoload.%s.infection.php',
$this->tmp,
self::HASH
);
$this->assertSame($expectedCustomAutoloadFilePath, $resultAutoLoaderFilePath);
$this->assertStringContainsString(
'app/autoload2.php',
file_get_contents($expectedCustomAutoloadFilePath)
);
}
public function test_it_sets_custom_autoloader_when_attribute_is_absent(): void
{
$builder = $this->createConfigBuilder(self::FIXTURES . '/phpunit_without_bootstrap.xml');
$xml = file_get_contents(
$builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'7.1'
)
);
$resultAutoLoaderFilePath = $this->queryXpath($xml, '/phpunit/@bootstrap')[0]->nodeValue;
$expectedCustomAutoloadFilePath = sprintf(
'%s/interceptor.autoload.%s.infection.php',
$this->tmp,
self::HASH
);
$this->assertSame($expectedCustomAutoloadFilePath, $resultAutoLoaderFilePath);
$this->assertStringContainsString(
'vendor/autoload.php',
file_get_contents($expectedCustomAutoloadFilePath)
);
}
public function test_it_sets_stops_on_failure(): void
{
$xml = file_get_contents(
$this->builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'7.1'
)
);
$stopOnFailure = $this->queryXpath($xml, '/phpunit/@stopOnFailure')[0]->nodeValue;
$this->assertSame('true', $stopOnFailure);
}
public function test_it_deactivates_the_colors(): void
{
$xml = file_get_contents(
$this->builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'7.1'
)
);
$colors = $this->queryXpath($xml, '/phpunit/@colors')[0]->nodeValue;
$this->assertSame('false', $colors);
}
public function test_it_handles_root_test_suite(): void
{
$builder = $this->createConfigBuilder(self::FIXTURES . '/phpunit_root_test_suite.xml');
$configurationPath = $builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'7.1'
);
$testSuite = $this->queryXpath(
file_get_contents($configurationPath),
'/phpunit/testsuite'
);
$this->assertInstanceOf(DOMNodeList::class, $testSuite);
$this->assertSame(1, $testSuite->length);
}
public function test_it_removes_original_loggers(): void
{
$xml = file_get_contents(
$this->builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'7.1'
)
);
$logEntries = $this->queryXpath($xml, '/phpunit/logging/log[@type="coverage-html"]');
$this->assertInstanceOf(DOMNodeList::class, $logEntries);
$this->assertSame(0, $logEntries->length);
}
public function test_it_removes_printer_class(): void
{
$xml = file_get_contents(
$this->builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'7.1'
)
);
$printerClass = $this->queryXpath($xml, '/phpunit/@printerClass');
$this->assertInstanceOf(DOMNodeList::class, $printerClass);
$this->assertSame(0, $printerClass->length);
}
public function test_it_does_not_set_default_execution_order_for_phpunit_7_1(): void
{
$builder = $this->createConfigBuilder(self::FIXTURES . '/phpunit_without_coverage_whitelist.xml');
$xml = file_get_contents(
$builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'7.1'
)
);
$executionOrder = $this->queryXpath($xml, '/phpunit/@executionOrder');
$this->assertSame(0, $executionOrder->length);
}
public function test_it_sets_default_execution_order_when_attribute_is_absent_for_phpunit_7_2(): void
{
$builder = $this->createConfigBuilder(self::FIXTURES . '/phpunit_without_coverage_whitelist.xml');
$xml = file_get_contents(
$builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'7.2'
)
);
$executionOrder = $this->queryXpath($xml, '/phpunit/@executionOrder')[0]->nodeValue;
$this->assertSame('default', $executionOrder);
}
public function test_it_sets_default_execution_order_when_attribute_is_present_for_phpunit_7_2(): void
{
$builder = $this->createConfigBuilder(self::FIXTURES . '/phpunit_with_order_set.xml');
$xml = file_get_contents(
$builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'7.2'
)
);
$executionOrder = $this->queryXpath($xml, '/phpunit/@executionOrder')[0]->nodeValue;
$this->assertSame('default', $executionOrder);
}
public function test_it_sets_defects_execution_order_and_cache_result_when_attribute_is_present_for_phpunit_7_3(): void
{
$builder = $this->createConfigBuilder(self::FIXTURES . '/phpunit_with_order_set.xml');
$xml = file_get_contents(
$builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'7.3'
)
);
$executionOrder = $this->queryXpath($xml, '/phpunit/@executionOrder')[0]->nodeValue;
$this->assertSame('defects', $executionOrder);
$executionOrder = $this->queryXpath($xml, '/phpunit/@cacheResult')[0]->nodeValue;
$this->assertSame('true', $executionOrder);
$executionOrder = $this->queryXpath($xml, '/phpunit/@cacheResultFile')[0]->nodeValue;
$this->assertSame(sprintf('.phpunit.result.cache.%s', self::HASH), $executionOrder);
}
/**
* @dataProvider locationsProvider
*
* @param TestLocation[] $tests
* @param string[] $expectedFiles
*/
public function test_it_sets_sorted_list_of_test_files(
array $tests,
array $expectedFiles
): void {
$xml = file_get_contents(
$this->builder->build(
$tests,
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'7.1'
)
);
$files = array_map(
static function (DOMNode $file): string {
return $file->nodeValue;
},
iterator_to_array(
$this->queryXpath($xml, '/phpunit/testsuites/testsuite/file'),
false
)
);
$this->assertSame($expectedFiles, $files);
}
public function test_it_removes_default_test_suite(): void
{
$xml = file_get_contents(
$this->builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'7.1'
)
);
$defaultTestSuite = $this->queryXpath($xml, '/phpunit/@defaultTestSuite');
$this->assertInstanceOf(DOMNodeList::class, $defaultTestSuite);
$this->assertCount(0, $defaultTestSuite);
}
public function test_interceptor_is_included(): void
{
$builder = $this->createConfigBuilder(self::FIXTURES . '/phpunit_without_bootstrap.xml');
$builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'7.1'
);
$expectedCustomAutoloadFilePath = sprintf(
'%s/interceptor.autoload.%s.infection.php',
$this->tmp,
self::HASH
);
$this->assertFileExists($expectedCustomAutoloadFilePath);
$this->assertStringContainsString(
'IncludeInterceptor.php',
file_get_contents($expectedCustomAutoloadFilePath)
);
}
public function locationsProvider(): iterable
{
yield [
[
new TestLocation(
'SimpleHabits\\Domain\\Model\\Goal\\GoalTest::it_calculates_percentage with data set #5',
'/path/to/siteSimpleHabits/Domain/Model/Goal/GoalTest.php',
0.861780
),
new TestLocation(
'SimpleHabits\\Domain\\Model\\Goal\\GoalTest::it_calculates_percentage with data set #6',
'/path/to/siteSimpleHabits/Domain/Model/Goal/GoalTest.php',
0.861780
),
new TestLocation(
'SimpleHabits\\Domain\\Model\\Goal\\GoalStepTest::it_correctly_returns_id',
'/path/to/siteSimpleHabits/Domain/Model/Goal/GoalStepTest.php',
0.035935
),
new TestLocation(
'SimpleHabits\\Domain\\Model\\Goal\\GoalStepTest::it_correctly_returns_recorded_at_date',
'/path/to/siteSimpleHabits/Domain/Model/Goal/GoalStepTest.php',
0.035935
),
],
[
'/path/to/siteSimpleHabits/Domain/Model/Goal/GoalStepTest.php',
'/path/to/siteSimpleHabits/Domain/Model/Goal/GoalTest.php',
],
];
yield [
[
new TestLocation(
'Path\\To\\A::test_a',
'/path/to/A.php',
0.586178
),
new TestLocation(
'Path\\To\\B::test_b',
'/path/to/B.php',
0.186178
),
new TestLocation(
'Path\\To\\C::test_c',
'/path/to/C.php',
0.016178
),
],
[
'/path/to/C.php',
'/path/to/B.php',
'/path/to/A.php',
],
];
}
private function queryXpath(string $xml, string $query)
{
$dom = new DOMDocument();
$dom->loadXML($xml);
return (new DOMXPath($dom))->query($query);
}
private function createConfigBuilder(
?string $originalPhpUnitXmlConfigPath = null
): MutationConfigBuilder {
$phpunitXmlPath = $originalPhpUnitXmlConfigPath ?: self::FIXTURES . '/phpunit.xml';
$replacer = new PathReplacer(new Filesystem(), $this->projectPath);
return new MutationConfigBuilder(
$this->tmp,
file_get_contents($phpunitXmlPath),
new XmlConfigurationManipulator($replacer, ''),
'project/dir',
new JUnitTestCaseSorter()
);
}
private function assertPHPSyntaxIsValid(string $phpCode): void
{
exec(
sprintf('echo %s | php -l', escapeshellarg($phpCode)),
$output,
$returnCode
);
$this->assertSame(
0,
$returnCode,
'Builder produced invalid code'
);
}
}