Repository navigation
Expand file tree
/
Copy pathUnusedSkipResolverTest.php
More file actions
121 lines (92 loc) · 4.83 KB
/
Copy pathUnusedSkipResolverTest.php
File metadata and controls
121 lines (92 loc) · 4.83 KB
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
<?php
declare(strict_types=1);
namespace Rector\Tests\Reporting;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Reporting\UnusedSkipResolver;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Rector\Tests\Skipper\Skipper\Fixture\Element\FifthElement;
use Rector\Tests\Skipper\Skipper\Fixture\Element\ThreeMan;
use Rector\Tests\Skipper\Skipper\Source\AnotherClassToSkip;
use Rector\ValueObject\ProcessResult;
final class UnusedSkipResolverTest extends AbstractLazyTestCase
{
private const string UNUSED_RULE_MASK = '*/dead-rule/*';
private const string USED_RULE_MASK = '*/matched-rule/*';
private const string GLOBAL_MASK = '*/global-mask/*';
private UnusedSkipResolver $unusedSkipResolver;
protected function setUp(): void
{
parent::setUp();
$this->unusedSkipResolver = $this->make(UnusedSkipResolver::class);
SimpleParameterProvider::setParameter(Option::SKIP, [
// skip-everywhere rule skip - forgotten at boot, not trackable, excluded
ThreeMan::class,
// rule-scoped path skip, never matched - intentional, reported even as mask
FifthElement::class => [self::UNUSED_RULE_MASK],
// rule-scoped path skip, matched - excluded
AnotherClassToSkip::class => [self::USED_RULE_MASK],
// global mask path skip - hard to spot, false-positive prone, excluded
self::GLOBAL_MASK,
]);
}
protected function tearDown(): void
{
SimpleParameterProvider::setParameter(Option::SKIP, []);
SimpleParameterProvider::setParameter(Option::REPORT_UNUSED_SKIPS, false);
SimpleParameterProvider::setParameter(Option::IS_RUN_NARROWED, false);
}
public function testResolvesUnusedSkipsAsRuleAndPath(): void
{
SimpleParameterProvider::setParameter(Option::REPORT_UNUSED_SKIPS, true);
$processResult = new ProcessResult([], [], 0, [self::USED_RULE_MASK]);
$unusedSkips = $this->unusedSkipResolver->resolve($processResult);
// rule-scoped unused skip is reported as "rule => path"
$this->assertContains(FifthElement::class . ' => ' . self::UNUSED_RULE_MASK, $unusedSkips);
// matched rule-scoped skip, global mask and skip-everywhere rule are excluded
$this->assertNotContains(AnotherClassToSkip::class . ' => ' . self::USED_RULE_MASK, $unusedSkips);
$this->assertNotContains(self::GLOBAL_MASK, $unusedSkips);
$this->assertNotContains(ThreeMan::class, $unusedSkips);
}
public function testReportsUnusedSkipAsRelativePath(): void
{
SimpleParameterProvider::setParameter(Option::REPORT_UNUSED_SKIPS, true);
$absolutePath = getcwd() . '/src/Reporting/UnusedSkipResolver.php';
SimpleParameterProvider::setParameter(Option::SKIP, [
FifthElement::class => [$absolutePath],
]);
$unusedSkips = $this->unusedSkipResolver->resolve(new ProcessResult([], [], 0, []));
// the absolute path is shortened to a relative one, matching the "->withSkip()" syntax
$this->assertContains(FifthElement::class . ' => src/Reporting/UnusedSkipResolver.php', $unusedSkips);
}
public function testGroupsMultipleUnusedPathsUnderRule(): void
{
SimpleParameterProvider::setParameter(Option::REPORT_UNUSED_SKIPS, true);
$firstPath = getcwd() . '/src/Reporting/UnusedSkipResolver.php';
$secondPath = getcwd() . '/src/Reporting/MissConfigurationReporter.php';
SimpleParameterProvider::setParameter(Option::SKIP, [
FifthElement::class => [$firstPath, $secondPath],
]);
$unusedSkips = $this->unusedSkipResolver->resolve(new ProcessResult([], [], 0, []));
// multiple unused paths are grouped under their rule, not repeated per line
$this->assertContains(
FifthElement::class . ' => [ src/Reporting/UnusedSkipResolver.php' . "\n " . 'src/Reporting/MissConfigurationReporter.php ]',
$unusedSkips
);
}
public function testResolvesNothingWhenRunIsNarrowed(): void
{
SimpleParameterProvider::setParameter(Option::REPORT_UNUSED_SKIPS, true);
SimpleParameterProvider::setParameter(Option::IS_RUN_NARROWED, true);
// narrowed run (cli paths, "--only", "--only-suffix") only touches part of the codebase,
// so skips outside that scope are not false positives
$processResult = new ProcessResult([], [], 0, []);
$this->assertSame([], $this->unusedSkipResolver->resolve($processResult));
}
public function testResolvesNothingWhenDisabled(): void
{
SimpleParameterProvider::setParameter(Option::REPORT_UNUSED_SKIPS, false);
$processResult = new ProcessResult([], [], 0, []);
$this->assertSame([], $this->unusedSkipResolver->resolve($processResult));
}
}