-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
AbstractFixerTestCase.php
263 lines (220 loc) · 8.22 KB
/
AbstractFixerTestCase.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
<?php
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Test;
use GeckoPackages\PHPUnit\Constraints\SameStringsConstraint;
use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\FixerFactory;
use PhpCsFixer\Linter\Linter;
use PhpCsFixer\Linter\LinterInterface;
use PhpCsFixer\RuleSet;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\Utils;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*/
abstract class AbstractFixerTestCase extends TestCase
{
/**
* @var LinterInterface
*/
protected $linter;
/**
* @var null|FixerInterface
*/
protected $fixer;
/**
* @var null|string
*/
private $fixerClassName;
protected function setUp()
{
$this->linter = $this->getLinter();
$this->fixer = $this->createFixer();
}
/**
* @return FixerInterface
*/
protected function createFixer()
{
$fixerClassName = $this->getFixerClassName();
return new $fixerClassName();
}
/**
* Create fixer factory with all needed fixers registered.
*
* @return FixerFactory
*/
protected function createFixerFactory()
{
return FixerFactory::create()->registerBuiltInFixers();
}
/**
* @return string
*/
protected function getFixerName()
{
$reflection = new \ReflectionClass($this);
$name = preg_replace('/FixerTest$/', '', $reflection->getShortName());
return Utils::camelCaseToUnderscore($name);
}
/**
* @param string $filename
*
* @return \SplFileInfo
*/
protected function getTestFile($filename = __FILE__)
{
static $files = [];
if (!isset($files[$filename])) {
$files[$filename] = new \SplFileInfo($filename);
}
return $files[$filename];
}
/**
* Tests if a fixer fixes a given string to match the expected result.
*
* It is used both if you want to test if something is fixed or if it is not touched by the fixer.
* It also makes sure that the expected output does not change when run through the fixer. That means that you
* do not need two test cases like [$expected] and [$expected, $input] (where $expected is the same in both cases)
* as the latter covers both of them.
* This method throws an exception if $expected and $input are equal to prevent test cases that accidentally do
* not test anything.
*
* @param string $expected The expected fixer output
* @param string|null $input The fixer input, or null if it should intentionally be equal to the output
* @param \SplFileInfo|null $file The file to fix, or null if unneeded
*/
protected function doTest($expected, $input = null, \SplFileInfo $file = null)
{
if ($expected === $input) {
throw new \InvalidArgumentException('Input parameter must not be equal to expected parameter.');
}
$file = $file ?: $this->getTestFile();
$fileIsSupported = $this->fixer->supports($file);
if (null !== $input) {
$this->assertNull($this->lintSource($input));
Tokens::clearCache();
$tokens = Tokens::fromCode($input);
if ($fileIsSupported) {
$this->assertTrue($this->fixer->isCandidate($tokens), 'Fixer must be a candidate for input code.');
$this->assertFalse($tokens->isChanged(), 'Fixer must not touch Tokens on candidate check.');
$fixResult = $this->fixer->fix($file, $tokens);
$this->assertNull($fixResult, '->fix method must return null.');
}
$this->assertThat(
$tokens->generateCode(),
new SameStringsConstraint($expected),
'Code build on input code must match expected code.'
);
$this->assertTrue($tokens->isChanged(), 'Tokens collection built on input code must be marked as changed after fixing.');
$tokens->clearEmptyTokens();
$this->assertSame(
count($tokens),
count(array_unique(array_map(function (Token $token) {
return spl_object_hash($token);
}, $tokens->toArray()))),
'Token items inside Tokens collection must be unique.'
);
Tokens::clearCache();
$expectedTokens = Tokens::fromCode($expected);
$this->assertTokens($expectedTokens, $tokens);
}
$this->assertNull($this->lintSource($expected));
Tokens::clearCache();
$tokens = Tokens::fromCode($expected);
if ($fileIsSupported) {
$fixResult = $this->fixer->fix($file, $tokens);
$this->assertNull($fixResult, '->fix method must return null.');
}
$this->assertThat(
$tokens->generateCode(),
new SameStringsConstraint($expected),
'Code build on expected code must not change.'
);
$this->assertFalse($tokens->isChanged(), 'Tokens collection built on expected code must not be marked as changed after fixing.');
}
/**
* @param string $source
*
* @return string|null
*/
protected function lintSource($source)
{
try {
$this->linter->lintSource($source)->check();
} catch (\Exception $e) {
return $e->getMessage()."\n\nSource:\n$source";
}
}
private function assertTokens(Tokens $expectedTokens, Tokens $inputTokens)
{
foreach ($expectedTokens as $index => $expectedToken) {
$inputToken = $inputTokens[$index];
$option = ['JSON_PRETTY_PRINT'];
$this->assertTrue(
$expectedToken->equals($inputToken),
sprintf("The token at index %d must be:\n%s,\ngot:\n%s.", $index, $expectedToken->toJson($option), $inputToken->toJson($option))
);
}
$this->assertSame($expectedTokens->count(), $inputTokens->count(), 'The collection must have the same length than the expected one.');
$foundTokenKinds = array_keys(AccessibleObject::create($expectedTokens)->foundTokenKinds);
foreach ($foundTokenKinds as $tokenKind) {
$this->assertTrue(
$inputTokens->isTokenKindFound($tokenKind),
sprintf('The token kind %s must be found in fixed tokens collection.', $tokenKind)
);
}
}
/**
* @return LinterInterface
*/
private function getLinter()
{
static $linter = null;
if (null === $linter) {
if (getenv('SKIP_LINT_TEST_CASES')) {
$linterProphecy = $this->prophesize(\PhpCsFixer\Linter\LinterInterface::class);
$linterProphecy
->lintSource(Argument::type('string'))
->willReturn($this->prophesize(\PhpCsFixer\Linter\LintingResultInterface::class)->reveal());
$linter = $linterProphecy->reveal();
} else {
$linter = new Linter();
}
}
return $linter;
}
/**
* @return string
*/
private function getFixerClassName()
{
if (null !== $this->fixerClassName) {
return $this->fixerClassName;
}
try {
$fixers = $this->createFixerFactory()
->useRuleSet(new RuleSet([$this->getFixerName() => true]))
->getFixers()
;
} catch (\UnexpectedValueException $e) {
throw new \UnexpectedValueException('Cannot determine fixer class, perhaps you forget to override `getFixerName` or `createFixerFactory` method?', 0, $e);
}
if (1 !== count($fixers)) {
throw new \UnexpectedValueException(sprintf('Determine fixer class should result in one fixer, got "%d". Perhaps you configured the fixer to "false" ?', count($fixers)));
}
$this->fixerClassName = get_class($fixers[0]);
return $this->fixerClassName;
}
}