-
-
Notifications
You must be signed in to change notification settings - Fork 154
/
InitialConfigBuilderTest.php
579 lines (438 loc) · 18.9 KB
/
InitialConfigBuilderTest.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
<?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 DOMDocument;
use DOMNodeList;
use DOMXPath;
use Infection\TestFramework\PhpUnit\Config\Builder\InitialConfigBuilder;
use Infection\TestFramework\PhpUnit\Config\InvalidPhpUnitConfiguration;
use Infection\TestFramework\PhpUnit\Config\Path\PathReplacer;
use Infection\TestFramework\PhpUnit\Config\XmlConfigurationManipulator;
use Infection\TestFramework\PhpUnit\Config\XmlConfigurationVersionProvider;
use Infection\Tests\FileSystem\FileSystemTestCase;
use function Infection\Tests\normalizePath as p;
use InvalidArgumentException;
use const PHP_EOL;
use function Safe\file_get_contents;
use function Safe\realpath;
use function Safe\simplexml_load_string;
use function Safe\sprintf;
use Symfony\Component\Filesystem\Filesystem;
/**
* @group integration
*/
final class InitialConfigBuilderTest extends FileSystemTestCase
{
private const FIXTURES = __DIR__ . '/../../../../Fixtures/Files/phpunit';
/**
* @var string
*/
private $projectPath;
/**
* @var InitialConfigBuilder
*/
private $builder;
protected function setUp(): void
{
parent::setUp();
$this->projectPath = p(realpath(self::FIXTURES . '/project-path'));
$this->builder = $this->createConfigBuilder();
}
public function test_it_builds_and_dump_the_xml_configuration(): void
{
$configurationPath = $this->builder->build('6.5');
$this->assertSame(
$this->tmp . '/phpunitConfiguration.initial.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.'
);
}
public function test_it_preserves_white_spaces_and_formatting(): void
{
if (PHP_EOL === "\r\n") {
$this->markTestSkipped('Test fixture uses Unix line endings');
}
$builder = $this->createConfigBuilder(
self::FIXTURES . '/format-whitespace/original-phpunit.xml'
);
$configurationPath = $builder->build('6.5');
$this->assertFileEquals(
self::FIXTURES . '/format-whitespace/expected-phpunit.xml',
$configurationPath
);
}
public function test_the_original_xml_config_must_be_a_valid_xml_file(): void
{
try {
$this->createConfigBuilder(
self::FIXTURES . '/invalid/empty-phpunit.xml'
);
$this->fail('Expected an exception to be thrown.');
} catch (InvalidArgumentException $exception) {
$this->assertSame(
'The original XML config content cannot be an empty string',
$exception->getMessage()
);
}
}
public function test_the_original_xml_config_must_be_a_valid_phpunit_config_file(): void
{
$builder = $this->createConfigBuilder(
self::FIXTURES . '/invalid/invalid-phpunit.xml'
);
try {
$builder->build('x');
$this->fail('Expected an exception to be thrown.');
} catch (InvalidPhpUnitConfiguration $exception) {
$this->assertSame(
sprintf(
'The file "%s/phpunitConfiguration.initial.infection.xml" is not a valid PHPUnit configuration file',
$this->tmp
),
$exception->getMessage()
);
}
}
public function test_it_replaces_relative_path_to_absolute_path(): void
{
$xml = file_get_contents($this->builder->build('6.5'));
$directories = $this->queryXpath($xml, '/phpunit/testsuites/testsuite/directory');
$this->assertInstanceOf(DOMNodeList::class, $directories);
$this->assertSame(1, $directories->length);
$this->assertSame($this->projectPath . '/*Bundle', p($directories[0]->nodeValue));
}
public function test_it_sets_stops_on_failure(): void
{
$xml = file_get_contents($this->builder->build('6.5'));
$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('6.5'));
$colors = $this->queryXpath($xml, '/phpunit/@colors')[0]->nodeValue;
$this->assertSame('false', $colors);
}
public function test_it_disables_caching(): void
{
$xml = file_get_contents($this->builder->build('6.5'));
$cacheResult = $this->queryXpath($xml, '/phpunit/@cacheResult')[0]->nodeValue;
$this->assertSame('false', $cacheResult);
}
public function test_it_deactivates_stderr_redirection(): void
{
$xml = file_get_contents($this->builder->build('6.5'));
$stdErr = $this->queryXpath($xml, '/phpunit/@stderr')[0]->nodeValue;
$this->assertSame('false', $stdErr);
}
public function test_it_replaces_bootstrap_file(): void
{
$xml = file_get_contents($this->builder->build('6.5'));
$bootstrap = p($this->queryXpath($xml, '/phpunit/@bootstrap')[0]->nodeValue);
$this->assertSame($this->projectPath . '/app/autoload2.php', $bootstrap);
}
public function test_it_removes_original_loggers(): void
{
$xml = file_get_contents($this->builder->build('6.5'));
$logEntries = $this->queryXpath($xml, '/phpunit/logging/log[@type="coverage-html"]');
$this->assertSame(0, $logEntries->length);
}
public function test_it_does_not_add_coverage_loggers_ever_for_legacy_configuration(): void
{
$xml = file_get_contents($this->builder->build('6.5'));
$logEntries = $this->queryXpath($xml, '/phpunit/logging/log');
$this->assertInstanceOf(DOMNodeList::class, $logEntries);
$this->assertSame(0, $logEntries->length);
}
public function test_it_does_not_add_coverage_loggers_ever_for_latest_configuration(): void
{
$builder = $this->createConfigBuilderForPHPUnit93();
$xml = file_get_contents($builder->build('9.4'));
$logEntries = $this->queryXpath($xml, '/phpunit/logging');
$this->assertInstanceOf(DOMNodeList::class, $logEntries);
$this->assertSame(0, $logEntries->length);
}
public function test_it_creates_coverage_filter_whitelist_node_if_does_not_exist(): void
{
$phpunitXmlPath = self::FIXTURES . '/phpunit_without_coverage_whitelist.xml';
$xml = file_get_contents($this->createConfigBuilder($phpunitXmlPath)->build('6.5'));
$whitelistedDirectories = $this->queryXpath($xml, '/phpunit/filter/whitelist/directory');
$this->assertInstanceOf(DOMNodeList::class, $whitelistedDirectories);
$this->assertSame(2, $whitelistedDirectories->length);
}
public function test_it_creates_coverage_filter_whitelist_node_if_does_not_exist_and_version_situation_is_uncertain(): void
{
$phpunitXmlPath = self::FIXTURES . '/phpunit_without_coverage_whitelist.xml';
$xml = file_get_contents($this->createConfigBuilder($phpunitXmlPath)->build('9.3'));
$whitelistedDirectories = $this->queryXpath($xml, '/phpunit/filter/whitelist/directory');
$this->assertInstanceOf(DOMNodeList::class, $whitelistedDirectories);
$this->assertSame(2, $whitelistedDirectories->length);
}
public function test_it_replaces_coverage_filter_include_node_if_exists_but_filtered_source_files_provided(): void
{
$phpunitXmlPath = self::FIXTURES . '/phpunit_with_coverage_include_directories.xml';
$xml = file_get_contents($this->createConfigBuilder($phpunitXmlPath, ['src/File1.php'])->build('9.3'));
$coverageIncludeFiles = $this->queryXpath($xml, '/phpunit/coverage/include/file');
$this->assertInstanceOf(DOMNodeList::class, $coverageIncludeFiles);
$this->assertSame(1, $coverageIncludeFiles->length);
}
public function test_it_creates_coverage_include_node_if_does_not_exist_for_future_version_of_phpunit(): void
{
$phpunitXmlPath = self::FIXTURES . '/phpunit_without_coverage_whitelist.xml';
$xml = file_get_contents($this->createConfigBuilder($phpunitXmlPath)->build('10.0'));
$includedDirectories = $this->queryXpath($xml, '/phpunit/coverage/include/directory');
$this->assertInstanceOf(DOMNodeList::class, $includedDirectories);
$this->assertSame(2, $includedDirectories->length);
}
public function test_it_does_not_create_legacy_coverage_filter_whitelist_node_for_future_version_of_phpunit(): void
{
$phpunitXmlPath = self::FIXTURES . '/phpunit_without_coverage_whitelist.xml';
$xml = file_get_contents($this->createConfigBuilder($phpunitXmlPath)->build('10.0'));
$whitelistedDirectories = $this->queryXpath($xml, '/phpunit/filter/whitelist/directory');
$this->assertInstanceOf(DOMNodeList::class, $whitelistedDirectories);
$this->assertSame(0, $whitelistedDirectories->length);
}
public function test_it_does_not_create_coverage_filter_whitelist_node_if_already_exist(): void
{
$xml = file_get_contents($this->builder->build('6.5'));
$whitelistedDirectories = $this->queryXpath($xml, '/phpunit/filter/whitelist/directory');
$this->assertInstanceOf(DOMNodeList::class, $whitelistedDirectories);
$this->assertSame(1, $whitelistedDirectories->length);
}
public function test_it_does_not_create_coverage_include_node_if_already_exist(): void
{
$builder = $this->createConfigBuilderForPHPUnit93();
$xml = file_get_contents($builder->build('9.4'));
$includedDirectories = $this->queryXpath($xml, '/phpunit/coverage/include');
$this->assertInstanceOf(DOMNodeList::class, $includedDirectories);
$this->assertSame(1, $includedDirectories->length);
}
public function test_it_removes_printer_class(): void
{
$xml = file_get_contents($this->builder->build('6.5'));
$printerClass = $this->queryXpath($xml, '/phpunit/@printerClass');
$this->assertInstanceOf(DOMNodeList::class, $printerClass);
$this->assertSame(0, $printerClass->length);
}
/**
* @dataProvider executionOrderProvider
*/
public function test_it_adds_execution_order_for_proper_phpunit_versions(
string $version,
string $attributeName,
int $expectedNodeCount
): void {
$xml = file_get_contents($this->builder->build($version));
$nodes = $this->queryXpath($xml, sprintf('/phpunit/@%s', $attributeName));
$this->assertInstanceOf(DOMNodeList::class, $nodes);
$this->assertSame($expectedNodeCount, $nodes->length);
}
public function test_it_does_not_update_order_if_it_is_already_set(): void
{
$phpunitXmlPath = self::FIXTURES . '/phpunit_with_order_set.xml';
$builder = $this->createConfigBuilder($phpunitXmlPath);
$xml = file_get_contents($builder->build('7.2'));
$executionOrder = $this->queryXpath($xml, sprintf('/phpunit/@%s', 'executionOrder'));
$this->assertInstanceOf(DOMNodeList::class, $executionOrder);
$this->assertSame('reverse', $executionOrder[0]->value);
$resolveDependencies = $this->queryXpath($xml, sprintf('/phpunit/@%s', 'resolveDependencies'));
$this->assertInstanceOf(DOMNodeList::class, $resolveDependencies);
$this->assertSame(0, $resolveDependencies->length);
}
/**
* @dataProvider failOnProvider
*/
public function test_it_adds_fail_on_risky_and_warning_for_proper_phpunit_versions(
string $version,
string $attributeName,
int $expectedNodeCount
): void {
$xml = file_get_contents($this->builder->build($version));
$nodes = $this->queryXpath($xml, sprintf('/phpunit/@%s', $attributeName));
$this->assertInstanceOf(DOMNodeList::class, $nodes);
$this->assertSame($expectedNodeCount, $nodes->length);
}
public function test_it_does_not_update_fail_on_risky_attributes_if_it_is_already_set(): void
{
$phpunitXmlPath = self::FIXTURES . '/phpunit_with_fail_on_risky_set.xml';
$builder = $this->createConfigBuilder($phpunitXmlPath);
$xml = file_get_contents($builder->build('5.2'));
$failOnRisky = $this->queryXpath($xml, sprintf('/phpunit/@%s', 'failOnRisky'));
$this->assertInstanceOf(DOMNodeList::class, $failOnRisky);
$this->assertSame('false', $failOnRisky[0]->value);
}
public function test_it_does_not_update_fail_on_warning_attributes_if_it_is_already_set(): void
{
$phpunitXmlPath = self::FIXTURES . '/phpunit_with_fail_on_warning_set.xml';
$builder = $this->createConfigBuilder($phpunitXmlPath);
$xml = file_get_contents($builder->build('5.2'));
$failOnRisky = $this->queryXpath($xml, sprintf('/phpunit/@%s', 'failOnWarning'));
$this->assertInstanceOf(DOMNodeList::class, $failOnRisky);
$this->assertSame('false', $failOnRisky[0]->value);
}
public function test_it_creates_a_configuration(): void
{
$builder = $this->createConfigBuilder(
self::FIXTURES . '/phpunit.xml'
);
$configurationPath = $builder->build('6.5');
$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="$projectPath/app/autoload2.php" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" syntaxCheck="false" defaultTestSuite="unit" failOnRisky="true" failOnWarning="true" stopOnFailure="true" cacheResult="false" stderr="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory>$projectPath/*Bundle</directory>
</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($configurationPath)
);
}
public function executionOrderProvider(): iterable
{
yield 'PHPUnit 7.1.99 runs without random test order' => [
'7.1.99',
'executionOrder',
0,
];
yield 'PHPUnit 7.2 runs with random test order' => [
'7.2',
'executionOrder',
1,
];
yield 'PHPUnit 7.3.1 runs with random test order' => [
'7.3.1',
'executionOrder',
1,
];
yield 'PHPUnit 7.1.99 runs without dependency resolver' => [
'7.1.99',
'resolveDependencies',
0,
];
yield 'PHPUnit 7.2 runs with dependency resolver' => [
'7.2',
'resolveDependencies',
1,
];
yield 'PHPUnit 7.3.1 runs dependency resolver' => [
'7.3.1',
'resolveDependencies',
1,
];
}
public function failOnProvider(): iterable
{
yield 'PHPUnit 5.1.99 runs without failOnRisky' => [
'5.1.99',
'failOnRisky',
0,
];
yield 'PHPUnit 5.2 runs with failOnRisky' => [
'5.2',
'failOnRisky',
1,
];
yield 'PHPUnit 5.3.1 runs with failOnRisky' => [
'5.3.1',
'failOnRisky',
1,
];
yield 'PHPUnit 5.1.99 runs without resolveDependencies' => [
'5.1.99',
'failOnWarning',
0,
];
yield 'PHPUnit 5.2 runs with resolveDependencies' => [
'5.2',
'failOnWarning',
1,
];
yield 'PHPUnit 5.3.1 runs resolveDependencies' => [
'5.3.1',
'failOnWarning',
1,
];
}
private function queryXpath(string $xml, string $query)
{
$dom = new DOMDocument();
$dom->loadXML($xml);
return (new DOMXPath($dom))->query($query);
}
private function createConfigBuilderForPHPUnit93(): InitialConfigBuilder
{
return $this->createConfigBuilder(self::FIXTURES . '/phpunit_93.xml');
}
private function createConfigBuilder(
?string $originalPhpUnitXmlConfigPath = null,
array $filteredSourceFilesToMutate = []
): InitialConfigBuilder {
$phpunitXmlPath = $originalPhpUnitXmlConfigPath ?: self::FIXTURES . '/phpunit.xml';
$srcDirs = ['src', 'app'];
$replacer = new PathReplacer(new Filesystem(), $this->projectPath);
return new InitialConfigBuilder(
$this->tmp,
file_get_contents($phpunitXmlPath),
new XmlConfigurationManipulator($replacer, ''),
new XmlConfigurationVersionProvider(),
$srcDirs,
$filteredSourceFilesToMutate
);
}
}