Skip to content

Commit 4a8ff9d

Browse files
authored
Remove AbstractFixerTestCase::$fixer (#933)
1 parent 5c7c9f6 commit 4a8ff9d

File tree

3 files changed

+54
-49
lines changed

3 files changed

+54
-49
lines changed

tests/Fixer/AbstractFixerTestCase.php

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -45,68 +45,51 @@ abstract class AbstractFixerTestCase extends TestCase
4545
'testTokenIsUseful',
4646
];
4747

48-
protected FixerInterface $fixer;
49-
50-
final protected function setUp(): void
51-
{
52-
$reflectionClass = new \ReflectionClass(static::class);
53-
54-
$className = 'PhpCsFixerCustomFixers\\Fixer\\' . \substr($reflectionClass->getShortName(), 0, -4);
55-
56-
$fixer = new $className();
57-
\assert($fixer instanceof FixerInterface);
58-
59-
$this->fixer = $fixer;
60-
if ($this->fixer instanceof WhitespacesAwareFixerInterface) {
61-
$this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig());
62-
}
63-
}
64-
6548
final public function testFixerDefinitionSummaryStartWithCorrectCase(): void
6649
{
67-
self::assertMatchesRegularExpression('/^[A-Z].*\.$/', $this->fixer->getDefinition()->getSummary());
50+
self::assertMatchesRegularExpression('/^[A-Z].*\.$/', self::getFixer()->getDefinition()->getSummary());
6851
}
6952

7053
final public function testFixerDefinitionRiskyDescriptionStartWithLowercase(): void
7154
{
72-
if (!$this->fixer->isRisky()) {
55+
if (!self::getFixer()->isRisky()) {
7356
$this->expectNotToPerformAssertions();
7457

7558
return;
7659
}
7760

78-
$riskyDescription = $this->fixer->getDefinition()->getRiskyDescription();
61+
$riskyDescription = self::getFixer()->getDefinition()->getRiskyDescription();
7962
\assert(\is_string($riskyDescription));
8063

8164
self::assertMatchesRegularExpression('/^[a-z]/', $riskyDescription);
8265
}
8366

8467
final public function testFixerDefinitionRiskyDescriptionDoesNotEndWithDot(): void
8568
{
86-
if (!$this->fixer->isRisky()) {
69+
if (!self::getFixer()->isRisky()) {
8770
$this->expectNotToPerformAssertions();
8871

8972
return;
9073
}
9174

92-
$riskyDescription = $this->fixer->getDefinition()->getRiskyDescription();
75+
$riskyDescription = self::getFixer()->getDefinition()->getRiskyDescription();
9376
\assert(\is_string($riskyDescription));
9477

9578
self::assertStringEndsNotWith('.', $riskyDescription);
9679
}
9780

9881
final public function testFixerDefinitionHasExactlyOneCodeSample(): void
9982
{
100-
if ($this->fixer instanceof DeprecatedFixerInterface) {
101-
self::assertGreaterThanOrEqual(1, \count($this->fixer->getDefinition()->getCodeSamples()));
83+
if (self::getFixer() instanceof DeprecatedFixerInterface) {
84+
self::assertGreaterThanOrEqual(1, \count(self::getFixer()->getDefinition()->getCodeSamples()));
10285
} else {
103-
self::assertCount(1, $this->fixer->getDefinition()->getCodeSamples());
86+
self::assertCount(1, self::getFixer()->getDefinition()->getCodeSamples());
10487
}
10588
}
10689

10790
final public function testCodeSampleEndsWithNewLine(): void
10891
{
109-
$codeSample = $this->fixer->getDefinition()->getCodeSamples()[0];
92+
$codeSample = self::getFixer()->getDefinition()->getCodeSamples()[0];
11093

11194
self::assertMatchesRegularExpression('/\n$/', $codeSample->getCode());
11295
}
@@ -116,22 +99,22 @@ final public function testCodeSampleEndsWithNewLine(): void
11699
*/
117100
final public function testCodeSampleIsChangedDuringFixing(): void
118101
{
119-
$codeSample = $this->fixer->getDefinition()->getCodeSamples()[0];
120-
if ($this->fixer instanceof ConfigurableFixerInterface) {
121-
$this->fixer->configure($codeSample->getConfiguration() ?? []);
102+
$codeSample = self::getFixer()->getDefinition()->getCodeSamples()[0];
103+
if (self::getFixer() instanceof ConfigurableFixerInterface) {
104+
self::getFixer()->configure($codeSample->getConfiguration() ?? []);
122105
}
123106

124107
Tokens::clearCache();
125108
$tokens = Tokens::fromCode($codeSample->getCode());
126109

127-
$this->fixer->fix($this->createMock(\SplFileInfo::class), $tokens);
110+
self::getFixer()->fix($this->createMock(\SplFileInfo::class), $tokens);
128111

129112
self::assertNotSame($codeSample->getCode(), $tokens->generateCode());
130113
}
131114

132115
final public function testPriority(): void
133116
{
134-
self::assertLessThan((new EncodingFixer())->getPriority(), $this->fixer->getPriority());
117+
self::assertLessThan((new EncodingFixer())->getPriority(), self::getFixer()->getPriority());
135118
}
136119

137120
final public function testMethodNames(): void
@@ -157,13 +140,29 @@ final public function testMethodNames(): void
157140
}
158141
}
159142

143+
final protected static function getFixer(): FixerInterface
144+
{
145+
$className = \str_replace('Tests', 'PhpCsFixerCustomFixers', \substr(static::class, 0, -4));
146+
147+
$fixer = new $className();
148+
\assert($fixer instanceof FixerInterface);
149+
150+
if ($fixer instanceof WhitespacesAwareFixerInterface) {
151+
$fixer->setWhitespacesConfig(new WhitespacesFixerConfig());
152+
}
153+
154+
return $fixer;
155+
}
156+
160157
/**
161158
* @param null|array<string, mixed> $configuration
162159
*/
163160
final protected function doTest(string $expected, ?string $input = null, ?array $configuration = null): void
164161
{
165-
if ($this->fixer instanceof ConfigurableFixerInterface) {
166-
$this->fixer->configure($configuration ?? []);
162+
$fixer = self::getFixer();
163+
164+
if ($fixer instanceof ConfigurableFixerInterface) {
165+
$fixer->configure($configuration ?? []);
167166
}
168167

169168
if ($expected === $input) {
@@ -179,9 +178,9 @@ final protected function doTest(string $expected, ?string $input = null, ?array
179178
Tokens::clearCache();
180179
$inputTokens = Tokens::fromCode($input);
181180

182-
self::assertTrue($this->fixer->isCandidate($inputTokens));
181+
self::assertTrue($fixer->isCandidate($inputTokens));
183182

184-
$this->fixer->fix($this->createMock(\SplFileInfo::class), $inputTokens);
183+
$fixer->fix($this->createMock(\SplFileInfo::class), $inputTokens);
185184
$inputTokens->clearEmptyTokens();
186185

187186
self::assertSame(
@@ -197,7 +196,7 @@ final protected function doTest(string $expected, ?string $input = null, ?array
197196
self::assertSameTokens($expectedTokens, $inputTokens);
198197
}
199198

200-
$this->fixer->fix($this->createMock(\SplFileInfo::class), $expectedTokens);
199+
$fixer->fix($this->createMock(\SplFileInfo::class), $expectedTokens);
201200

202201
self::assertSame($expected, $expectedTokens->generateCode());
203202

@@ -209,9 +208,10 @@ final protected function doTest(string $expected, ?string $input = null, ?array
209208
*/
210209
final protected function getConfigurationOptions(): array
211210
{
212-
self::assertInstanceOf(ConfigurableFixerInterface::class, $this->fixer);
211+
$fixer = self::getFixer();
212+
self::assertInstanceOf(ConfigurableFixerInterface::class, $fixer);
213213

214-
return $this->fixer->getConfigurationDefinition()->getOptions();
214+
return $fixer->getConfigurationDefinition()->getOptions();
215215
}
216216

217217
final protected function lintSource(string $source): ?string
@@ -233,12 +233,13 @@ final protected function lintSource(string $source): ?string
233233

234234
final protected function assertRiskiness(bool $isRisky): void
235235
{
236-
self::assertSame($isRisky, $this->fixer->isRisky());
236+
self::assertSame($isRisky, self::getFixer()->isRisky());
237237
}
238238

239239
final protected function assertSuccessorName(string $successorName): void
240240
{
241-
self::assertInstanceOf(DeprecatedFixerInterface::class, $this->fixer);
242-
self::assertSame([$successorName], $this->fixer->getSuccessorsNames());
241+
$fixer = self::getFixer();
242+
self::assertInstanceOf(DeprecatedFixerInterface::class, $fixer);
243+
self::assertSame([$successorName], $fixer->getSuccessorsNames());
243244
}
244245
}

tests/Fixer/NumericLiteralSeparatorFixerTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ public function testConfiguration(): void
4242

4343
public function testReversingCodeSample(): void
4444
{
45-
$codeSample = $this->fixer->getDefinition()->getCodeSamples()[0];
45+
$fixer = self::getFixer();
46+
47+
$codeSample = $fixer->getDefinition()->getCodeSamples()[0];
4648

4749
Tokens::clearCache();
4850
$tokens = Tokens::fromCode($codeSample->getCode());
4951

50-
$this->fixer->fix($this->createMock(\SplFileInfo::class), $tokens);
52+
$fixer->fix($this->createMock(\SplFileInfo::class), $tokens);
5153
self::assertNull($this->lintSource($tokens->generateCode()));
5254

5355
$this->doTest(

tests/Fixer/SingleSpaceAfterStatementFixerTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class Foo {}
284284
public function testExampleWithAllTokensHasAllSpacesFixed(): void
285285
{
286286
$tokens = Tokens::fromCode(self::EXAMPLE_WITH_ALL_TOKENS);
287-
$this->fixer->fix($this->createMock(\SplFileInfo::class), $tokens);
287+
self::getFixer()->fix($this->createMock(\SplFileInfo::class), $tokens);
288288

289289
self::assertDoesNotMatchRegularExpression('/[^\n ] {2,}/', $tokens->generateCode());
290290
}
@@ -294,20 +294,22 @@ public function testExampleWithAllTokensHasAllSpacesFixed(): void
294294
*/
295295
public function testTokenIsUseful(int $token): void
296296
{
297+
$fixer = self::getFixer();
298+
297299
$expectedTokens = Tokens::fromCode(self::EXAMPLE_WITH_ALL_TOKENS);
298-
$this->fixer->fix($this->createMock(\SplFileInfo::class), $expectedTokens);
300+
$fixer->fix($this->createMock(\SplFileInfo::class), $expectedTokens);
299301

300-
$reflectionObject = new \ReflectionObject($this->fixer);
302+
$reflectionObject = new \ReflectionObject($fixer);
301303
$property = $reflectionObject->getProperty('tokens');
302304
$property->setAccessible(true);
303305

304306
/** @var array<int> $tokens */
305-
$tokens = $property->getValue($this->fixer);
307+
$tokens = $property->getValue($fixer);
306308

307-
$property->setValue($this->fixer, \array_diff($tokens, [$token]));
309+
$property->setValue($fixer, \array_diff($tokens, [$token]));
308310

309311
$tokens = Tokens::fromCode(self::EXAMPLE_WITH_ALL_TOKENS);
310-
$this->fixer->fix($this->createMock(\SplFileInfo::class), $tokens);
312+
$fixer->fix($this->createMock(\SplFileInfo::class), $tokens);
311313

312314
self::assertNotSame(
313315
$expectedTokens->generateCode(),

0 commit comments

Comments
 (0)