generated from ergebnis/php-cs-fixer-config-template
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
AbstractRuleSetTestCase.php
251 lines (209 loc) 路 6.14 KB
/
AbstractRuleSetTestCase.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
<?php
declare(strict_types=1);
/**
* Copyright (c) 2019 Andreas M枚ller
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/ergebnis/php-cs-fixer-config
*/
namespace Ergebnis\PhpCsFixer\Config\Test\Unit\RuleSet;
use Ergebnis\PhpCsFixer\Config;
use PhpCsFixer\Fixer;
use PhpCsFixer\FixerFactory;
use PhpCsFixer\RuleSet;
use PHPUnit\Framework;
/**
* @internal
*/
abstract class AbstractRuleSetTestCase extends Framework\TestCase
{
/**
* @var string
*/
protected $name;
/**
* @var array
*/
protected $rules;
/**
* @var int
*/
protected $targetPhpVersion;
final public function testDefaults(): void
{
$ruleSet = self::createRuleSet();
self::assertSame($this->name, $ruleSet->name());
self::assertEquals($this->rules, $ruleSet->rules());
self::assertEquals($this->targetPhpVersion, $ruleSet->targetPhpVersion());
}
final public function testAllConfiguredRulesAreBuiltIn(): void
{
$fixersNotBuiltIn = \array_diff(
$this->configuredFixers(),
$this->builtInFixers()
);
\sort($fixersNotBuiltIn);
self::assertEmpty($fixersNotBuiltIn, \sprintf(
'Failed to assert that fixers for the rules "%s" are built in',
\implode('", "', $fixersNotBuiltIn)
));
}
final public function testAllBuiltInRulesAreConfigured(): void
{
$fixersWithoutConfiguration = \array_diff(
$this->builtInFixers(),
$this->configuredFixers()
);
\sort($fixersWithoutConfiguration);
self::assertEmpty($fixersWithoutConfiguration, \sprintf(
'Failed to assert that built-in fixers for the rules "%s" are configured',
\implode('", "', $fixersWithoutConfiguration)
));
}
final public function testHeaderCommentFixerIsDisabledByDefault(): void
{
$rules = self::createRuleSet()->rules();
self::assertArrayHasKey('header_comment', $rules);
self::assertFalse($rules['header_comment']);
}
/**
* @dataProvider providerValidHeader
*
* @param string $header
*/
final public function testHeaderCommentFixerIsEnabledIfHeaderIsProvided($header): void
{
$rules = self::createRuleSet($header)->rules();
self::assertArrayHasKey('header_comment', $rules);
$expected = [
'comment_type' => 'PHPDoc',
'header' => \trim($header),
'location' => 'after_declare_strict',
'separate' => 'both',
];
self::assertSame($expected, $rules['header_comment']);
}
/**
* @return \Generator
*/
final public function providerValidHeader()
{
$values = [
'string-empty' => '',
'string-not-empty' => 'foo',
'string-with-line-feed-only' => "\n",
'string-with-spaces-only' => ' ',
'string-with-tab-only' => "\t",
];
foreach ($values as $key => $value) {
yield $key => [
$value,
];
}
}
/**
* @dataProvider providerRuleNames
*
* @param array $ruleNames
* @param string $source
*/
final public function testRulesAreSortedByName($source, $ruleNames): void
{
$sorted = $ruleNames;
\sort($sorted);
self::assertEquals($sorted, $ruleNames, \sprintf(
'Failed to assert that the rules are sorted by name in %s',
$source
));
}
/**
* @return \Generator
*/
final public function providerRuleNames()
{
$values = [
'rule set' => self::createRuleSet()->rules(),
'test' => $this->rules,
];
foreach ($values as $source => $rules) {
yield [
$source,
\array_keys($rules),
];
}
}
/**
* @throws \RuntimeException
*
* @return string
*/
final protected static function className(): string
{
$className = \preg_replace(
'/Test$/',
'',
\str_replace(
'\Test\Unit',
'',
static::class
)
);
if (!\is_string($className)) {
throw new \RuntimeException(\sprintf(
'Failed resolving class name from test class name "%s".',
static::class
));
}
return $className;
}
/**
* @param string $header
*
* @throws \RuntimeException
*
* @return Config\RuleSet
*/
final protected static function createRuleSet($header = null): Config\RuleSet
{
$className = self::className();
$reflection = new \ReflectionClass($className);
$ruleSet = $reflection->newInstance($header);
if (!$ruleSet instanceof Config\RuleSet) {
throw new \RuntimeException(\sprintf(
'Class %s" does not implement interface "%s".',
$className,
Config\RuleSet::class
));
}
return $ruleSet;
}
/**
* @return string[]
*/
private function builtInFixers(): array
{
static $builtInFixers;
if (null === $builtInFixers) {
$fixerFactory = FixerFactory::create();
$fixerFactory->registerBuiltInFixers();
$builtInFixers = \array_map(static function (Fixer\FixerInterface $fixer): string {
return $fixer->getName();
}, $fixerFactory->getFixers());
}
return $builtInFixers;
}
private function configuredFixers(): array
{
/**
* RuleSet::create() removes disabled fixers, to let's just enable them to make sure they are not removed.
*
* @see https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/2361
*/
$rules = \array_map(static function (): bool {
return true;
}, self::createRuleSet()->rules());
return \array_keys(RuleSet::create($rules)->getRules());
}
}