-
Notifications
You must be signed in to change notification settings - Fork 19
/
AbstractFixerTestCase.php
104 lines (76 loc) · 2.94 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
<?php
declare(strict_types = 1);
namespace Tests\Fixer;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\DefinedFixerInterface;
use PhpCsFixer\Tests\Test\Assert\AssertTokensTrait;
use PhpCsFixer\Tokenizer\Tokens;
use PHPUnit\Framework\TestCase;
/**
* @internal
*/
abstract class AbstractFixerTestCase extends TestCase
{
use AssertTokensTrait;
/** @var DefinedFixerInterface */
protected $fixer;
final protected function setUp(): void
{
$reflectionClass = new \ReflectionClass(static::class);
$className = 'PhpCsFixerCustomFixers\\Fixer\\' . \substr($reflectionClass->getShortName(), 0, -4);
$this->fixer = new $className();
}
final public function testFixerDefinitionSummaryStartWithCorrectCase(): void
{
static::assertRegExp('/^[A-Z`].*\.$/', $this->fixer->getDefinition()->getSummary());
}
final public function testFixerDefinitionRiskyDescriptionStartWithLowercase(): void
{
if (!$this->fixer->isRisky()) {
$this->addToAssertionCount(1);
return;
}
static::assertRegExp('/^[a-z]/', $this->fixer->getDefinition()->getRiskyDescription());
}
final public function testFixerDefinitionRiskyDescriptionDoesNotEndWithDot(): void
{
if (!$this->fixer->isRisky()) {
$this->addToAssertionCount(1);
return;
}
static::assertStringEndsNotWith('.', $this->fixer->getDefinition()->getRiskyDescription());
}
final public function testFixerDefinitionHasExactlyOneCodeSample(): void
{
static::assertCount(1, $this->fixer->getDefinition()->getCodeSamples());
}
final public function testCodeSampleEndsWithNewLine(): void
{
$codeSample = $this->fixer->getDefinition()->getCodeSamples()[0];
static::assertRegExp('/\n$/', $codeSample->getCode());
}
final public function testCodeSampleIsChangedDuringFixing(): void
{
$codeSample = $this->fixer->getDefinition()->getCodeSamples()[0];
if ($this->fixer instanceof ConfigurableFixerInterface) {
$this->fixer->configure($codeSample->getConfiguration());
}
$tokens = Tokens::fromCode($codeSample->getCode());
$this->fixer->fix($this->createMock(\SplFileInfo::class), $tokens);
static::assertNotSame($codeSample->getCode(), $tokens->generateCode());
}
final protected function doTest(string $expected, ?string $input = null): void
{
if ($input === null) {
$input = $expected;
}
$tokens = Tokens::fromCode($input);
if ($input !== $expected) {
static::assertTrue($this->fixer->isCandidate($tokens));
}
$this->fixer->fix($this->createMock(\SplFileInfo::class), $tokens);
$tokens->clearEmptyTokens();
static::assertSame($expected, $tokens->generateCode());
static::assertTokens(Tokens::fromCode($expected), $tokens);
}
}