Skip to content

Commit 1adfc79

Browse files
authored
Do not use mocks (#934)
1 parent 4a8ff9d commit 1adfc79

File tree

6 files changed

+69
-14
lines changed

6 files changed

+69
-14
lines changed

.dev-tools/phpcs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<exclude name='Generic.Files.LineLength' />
1616
<exclude name='PSR12.Files.FileHeader.SpacingAfterBlock' />
1717
<exclude name='PSR12.Files.OpenTag.NotAlone' />
18+
<exclude name='Squiz.WhiteSpace.ScopeClosingBrace.ContentBefore' />
1819
</rule>
1920

2021
</ruleset>

tests/AutoReview/SrcCodeTest.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
1515
use PhpCsFixer\Fixer\FixerInterface;
16+
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
1617
use PhpCsFixer\FixerNameValidator;
1718
use PhpCsFixer\Tokenizer\Token;
1819
use PhpCsFixer\Tokenizer\Tokens;
@@ -82,9 +83,7 @@ public static function provideFixerCases(): iterable
8283

8384
public function testFixerSupportsAllFilesByDefault(): void
8485
{
85-
$fixer = $this->getMockForAbstractClass(AbstractFixer::class);
86-
87-
self::assertTrue($fixer->supports($this->createMock(\SplFileInfo::class)));
86+
self::assertTrue($this->createAbstractFixerDouble()->supports($this->createSplFileInfoDouble()));
8887
}
8988

9089
/**
@@ -148,4 +147,39 @@ public static function provideThereIsNoPregFunctionUsedDirectlyCases(): iterable
148147
yield $className => [$className];
149148
}
150149
}
150+
151+
private function createAbstractFixerDouble(): AbstractFixer
152+
{
153+
return new class () extends AbstractFixer {
154+
public function isCandidate(Tokens $tokens): bool
155+
{
156+
throw new \BadMethodCallException('Not implemented.');
157+
}
158+
159+
public function isRisky(): bool
160+
{
161+
throw new \BadMethodCallException('Not implemented.');
162+
}
163+
164+
public function fix(\SplFileInfo $file, Tokens $tokens): void
165+
{
166+
throw new \BadMethodCallException('Not implemented.');
167+
}
168+
169+
public function getDefinition(): FixerDefinitionInterface
170+
{
171+
throw new \BadMethodCallException('Not implemented.');
172+
}
173+
174+
public function getPriority(): int
175+
{
176+
throw new \BadMethodCallException('Not implemented.');
177+
}
178+
};
179+
}
180+
181+
private function createSplFileInfoDouble(): \SplFileInfo
182+
{
183+
return new class ('') extends \SplFileInfo {};
184+
}
151185
}

tests/Fixer/AbstractFixerTestCase.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ final public function testCodeSampleIsChangedDuringFixing(): void
107107
Tokens::clearCache();
108108
$tokens = Tokens::fromCode($codeSample->getCode());
109109

110-
self::getFixer()->fix($this->createMock(\SplFileInfo::class), $tokens);
110+
self::getFixer()->fix($this->createSplFileInfoDouble(), $tokens);
111111

112112
self::assertNotSame($codeSample->getCode(), $tokens->generateCode());
113113
}
@@ -180,7 +180,7 @@ final protected function doTest(string $expected, ?string $input = null, ?array
180180

181181
self::assertTrue($fixer->isCandidate($inputTokens));
182182

183-
$fixer->fix($this->createMock(\SplFileInfo::class), $inputTokens);
183+
$fixer->fix($this->createSplFileInfoDouble(), $inputTokens);
184184
$inputTokens->clearEmptyTokens();
185185

186186
self::assertSame(
@@ -196,7 +196,7 @@ final protected function doTest(string $expected, ?string $input = null, ?array
196196
self::assertSameTokens($expectedTokens, $inputTokens);
197197
}
198198

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

201201
self::assertSame($expected, $expectedTokens->generateCode());
202202

@@ -242,4 +242,9 @@ final protected function assertSuccessorName(string $successorName): void
242242
self::assertInstanceOf(DeprecatedFixerInterface::class, $fixer);
243243
self::assertSame([$successorName], $fixer->getSuccessorsNames());
244244
}
245+
246+
private function createSplFileInfoDouble(): \SplFileInfo
247+
{
248+
return new class ('') extends \SplFileInfo {};
249+
}
245250
}

tests/Fixer/NumericLiteralSeparatorFixerTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function testReversingCodeSample(): void
4949
Tokens::clearCache();
5050
$tokens = Tokens::fromCode($codeSample->getCode());
5151

52-
$fixer->fix($this->createMock(\SplFileInfo::class), $tokens);
52+
$fixer->fix($this->createSplFileInfoDouble(), $tokens);
5353
self::assertNull($this->lintSource($tokens->generateCode()));
5454

5555
$this->doTest(
@@ -192,4 +192,9 @@ public static function provideFixCases(): iterable
192192
],
193193
];
194194
}
195+
196+
private function createSplFileInfoDouble(): \SplFileInfo
197+
{
198+
return new class ('') extends \SplFileInfo {};
199+
}
195200
}

tests/Fixer/SingleSpaceAfterStatementFixerTest.php

Lines changed: 8 additions & 3 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-
self::getFixer()->fix($this->createMock(\SplFileInfo::class), $tokens);
287+
self::getFixer()->fix($this->createSplFileInfoDouble(), $tokens);
288288

289289
self::assertDoesNotMatchRegularExpression('/[^\n ] {2,}/', $tokens->generateCode());
290290
}
@@ -297,7 +297,7 @@ public function testTokenIsUseful(int $token): void
297297
$fixer = self::getFixer();
298298

299299
$expectedTokens = Tokens::fromCode(self::EXAMPLE_WITH_ALL_TOKENS);
300-
$fixer->fix($this->createMock(\SplFileInfo::class), $expectedTokens);
300+
$fixer->fix($this->createSplFileInfoDouble(), $expectedTokens);
301301

302302
$reflectionObject = new \ReflectionObject($fixer);
303303
$property = $reflectionObject->getProperty('tokens');
@@ -309,7 +309,7 @@ public function testTokenIsUseful(int $token): void
309309
$property->setValue($fixer, \array_diff($tokens, [$token]));
310310

311311
$tokens = Tokens::fromCode(self::EXAMPLE_WITH_ALL_TOKENS);
312-
$fixer->fix($this->createMock(\SplFileInfo::class), $tokens);
312+
$fixer->fix($this->createSplFileInfoDouble(), $tokens);
313313

314314
self::assertNotSame(
315315
$expectedTokens->generateCode(),
@@ -335,4 +335,9 @@ public static function provideTokenIsUsefulCases(): iterable
335335
yield [$token];
336336
}
337337
}
338+
339+
private function createSplFileInfoDouble(): \SplFileInfo
340+
{
341+
return new class ('') extends \SplFileInfo {};
342+
}
338343
}

tests/PriorityTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public function testInOrder(FixerInterface $firstFixer, FixerInterface $secondFi
4848
Tokens::clearCache();
4949
$tokens = Tokens::fromCode($input);
5050

51-
$firstFixer->fix($this->createMock(\SplFileInfo::class), $tokens);
51+
$firstFixer->fix($this->createSplFileInfoDouble(), $tokens);
5252
$tokens->clearEmptyTokens();
5353

54-
$secondFixer->fix($this->createMock(\SplFileInfo::class), $tokens);
54+
$secondFixer->fix($this->createSplFileInfoDouble(), $tokens);
5555
$tokens->clearEmptyTokens();
5656

5757
self::assertSame($expected, $tokens->generateCode());
@@ -68,10 +68,10 @@ public function testInRevertedOrder(FixerInterface $firstFixer, FixerInterface $
6868
Tokens::clearCache();
6969
$tokens = Tokens::fromCode($input);
7070

71-
$secondFixer->fix($this->createMock(\SplFileInfo::class), $tokens);
71+
$secondFixer->fix($this->createSplFileInfoDouble(), $tokens);
7272
$tokens->clearEmptyTokens();
7373

74-
$firstFixer->fix($this->createMock(\SplFileInfo::class), $tokens);
74+
$firstFixer->fix($this->createSplFileInfoDouble(), $tokens);
7575
$tokens->clearEmptyTokens();
7676

7777
self::assertNotSame($expected, $tokens->generateCode());
@@ -130,4 +130,9 @@ private static function getFixer(string $name, array $config): FixerInterface
130130

131131
throw new \Exception(\sprintf('Fixer "%s" not found in config: "%s".', $name, \json_encode($config)));
132132
}
133+
134+
private function createSplFileInfoDouble(): \SplFileInfo
135+
{
136+
return new class ('') extends \SplFileInfo {};
137+
}
133138
}

0 commit comments

Comments
 (0)