Skip to content

Commit 2617699

Browse files
authored
DataProviderNameFixer - add options "prefix" and "suffix" (#269)
1 parent 270540d commit 2617699

File tree

5 files changed

+151
-6
lines changed

5 files changed

+151
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CHANGELOG for PHP CS Fixer: custom fixers
22

3+
## v2.2.0 - [Unreleased]
4+
- Feature: DataProviderNameFixer - add options "prefix" and "suffix"
5+
36
## v2.1.0 - *2020-03-15*
47
- Add CommentedOutFunctionFixer
58
- Add NoDuplicatedArrayKeyFixer

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=master&event=push)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)
1010
[![Code coverage](https://img.shields.io/coveralls/github/kubawerlos/php-cs-fixer-custom-fixers/master.svg)](https://coveralls.io/github/kubawerlos/php-cs-fixer-custom-fixers?branch=master)
11-
![Tests](https://img.shields.io/badge/tests-2168-brightgreen.svg)
11+
![Tests](https://img.shields.io/badge/tests-2173-brightgreen.svg)
1212
[![Mutation testing badge](https://badge.stryker-mutator.io/github.com/kubawerlos/php-cs-fixer-custom-fixers/master)](https://stryker-mutator.github.io)
1313
[![Psalm type coverage](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers/coverage.svg)](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers)
1414

@@ -59,6 +59,9 @@ Configuration options:
5959
#### DataProviderNameFixer
6060
Name of data provider that is used only once must match name of test.
6161
*Risky: when relying on name of data provider function.*
62+
Configuration options:
63+
- `prefix` (`string`): prefix that replaces "test"; defaults to `'provide'`
64+
- `suffix` (`string`): suffix to be added at the end"; defaults to `'Cases'`
6265
```diff
6366
<?php
6467
class FooTest extends TestCase {

src/Fixer/DataProviderNameFixer.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace PhpCsFixerCustomFixers\Fixer;
66

7+
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
8+
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
9+
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
710
use PhpCsFixer\FixerDefinition\CodeSample;
811
use PhpCsFixer\FixerDefinition\FixerDefinition;
912
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
@@ -13,8 +16,14 @@
1316
use PhpCsFixer\Tokenizer\Tokens;
1417
use PhpCsFixerCustomFixers\Analyzer\DataProviderAnalyzer;
1518

16-
final class DataProviderNameFixer extends AbstractFixer
19+
final class DataProviderNameFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface
1720
{
21+
/** @var string */
22+
private $prefix = 'provide';
23+
24+
/** @var string */
25+
private $suffix = 'Cases';
26+
1827
/**
1928
* {@inheritdoc}
2029
*/
@@ -40,6 +49,34 @@ public function dataProvider() {}
4049
);
4150
}
4251

52+
public function getConfigurationDefinition(): FixerConfigurationResolver
53+
{
54+
return new FixerConfigurationResolver([
55+
(new FixerOptionBuilder('prefix', 'prefix that replaces "test"'))
56+
->setAllowedTypes(['string'])
57+
->setDefault($this->prefix)
58+
->getOption(),
59+
(new FixerOptionBuilder('suffix', 'suffix to be added at the end"'))
60+
->setAllowedTypes(['string'])
61+
->setDefault($this->suffix)
62+
->getOption(),
63+
]);
64+
}
65+
66+
public function configure(?array $configuration = null): void
67+
{
68+
/** @var string[] $configuration */
69+
$configuration = $configuration ?? [];
70+
71+
if (isset($configuration['prefix'])) {
72+
$this->prefix = $configuration['prefix'];
73+
}
74+
75+
if (isset($configuration['suffix'])) {
76+
$this->suffix = $configuration['suffix'];
77+
}
78+
}
79+
4380
public function getPriority(): int
4481
{
4582
return 0;
@@ -99,6 +136,6 @@ private function getProviderNameForTestName(string $name): string
99136
$name = \substr($name, 4);
100137
}
101138

102-
return 'provide' . \ucfirst($name) . 'Cases';
139+
return $this->prefix . \ucfirst($name) . $this->suffix;
103140
}
104141
}

tests/Fixer/AbstractFixerTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ final public function testPriority(): void
9494

9595
final protected function doTest(string $expected, ?string $input = null, ?array $configuration = null): void
9696
{
97-
if ($configuration !== null) {
97+
if ($this->fixer instanceof ConfigurableFixerInterface) {
9898
$this->fixer->configure($configuration);
9999
}
100100

tests/Fixer/DataProviderNameFixerTest.php

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,27 @@
44

55
namespace Tests\Fixer;
66

7+
use PhpCsFixer\FixerConfiguration\FixerOptionInterface;
8+
79
/**
810
* @internal
911
*
1012
* @covers \PhpCsFixerCustomFixers\Fixer\DataProviderNameFixer
1113
*/
1214
final class DataProviderNameFixerTest extends AbstractFixerTestCase
1315
{
16+
public function testConfiguration(): void
17+
{
18+
/** @var FixerOptionInterface[] $options */
19+
$options = $this->fixer->getConfigurationDefinition()->getOptions();
20+
self::assertArrayHasKey(0, $options);
21+
self::assertSame('prefix', $options[0]->getName());
22+
self::assertSame('provide', $options[0]->getDefault());
23+
self::assertArrayHasKey(1, $options);
24+
self::assertSame('suffix', $options[1]->getName());
25+
self::assertSame('Cases', $options[1]->getDefault());
26+
}
27+
1428
public function testIsRisky(): void
1529
{
1630
self::assertTrue($this->fixer->isRisky());
@@ -19,9 +33,9 @@ public function testIsRisky(): void
1933
/**
2034
* @dataProvider provideFixCases
2135
*/
22-
public function testFix(string $expected, ?string $input = null): void
36+
public function testFix(string $expected, ?string $input = null, ?array $configuration = null): void
2337
{
24-
$this->doTest($expected, $input);
38+
$this->doTest($expected, $input, $configuration);
2539
}
2640

2741
public static function provideFixCases(): iterable
@@ -277,5 +291,93 @@ public function fooDataProvider() {}
277291
}', $modifier),
278292
];
279293
}
294+
295+
yield 'custom prefix' => [
296+
'<?php
297+
class FooTest extends TestCase {
298+
/**
299+
* @dataProvider theBestPrefixFooCases
300+
*/
301+
public function testFoo() {}
302+
public function theBestPrefixFooCases() {}
303+
}',
304+
'<?php
305+
class FooTest extends TestCase {
306+
/**
307+
* @dataProvider dp
308+
*/
309+
public function testFoo() {}
310+
public function dp() {}
311+
}',
312+
['prefix' => 'theBestPrefix'],
313+
];
314+
315+
yield 'custom suffix' => [
316+
'<?php
317+
class FooTest extends TestCase {
318+
/**
319+
* @dataProvider provideFooTheBestSuffix
320+
*/
321+
public function testFoo() {}
322+
public function provideFooTheBestSuffix() {}
323+
}',
324+
'<?php
325+
class FooTest extends TestCase {
326+
/**
327+
* @dataProvider dp
328+
*/
329+
public function testFoo() {}
330+
public function dp() {}
331+
}',
332+
['suffix' => 'TheBestSuffix'],
333+
];
334+
335+
yield 'custom prefix and suffix' => [
336+
'<?php
337+
class FooTest extends TestCase {
338+
/**
339+
* @dataProvider theBestPrefixFooTheBestSuffix
340+
*/
341+
public function testFoo() {}
342+
public function theBestPrefixFooTheBestSuffix() {}
343+
}',
344+
'<?php
345+
class FooTest extends TestCase {
346+
/**
347+
* @dataProvider dp
348+
*/
349+
public function testFoo() {}
350+
public function dp() {}
351+
}',
352+
353+
[
354+
'prefix' => 'theBestPrefix',
355+
'suffix' => 'TheBestSuffix',
356+
],
357+
];
358+
359+
yield 'empty suffix' => [
360+
'<?php
361+
class FooTest extends TestCase {
362+
/**
363+
* @dataProvider dataProviderForFoo
364+
*/
365+
public function testFoo() {}
366+
public function dataProviderForFoo() {}
367+
}',
368+
'<?php
369+
class FooTest extends TestCase {
370+
/**
371+
* @dataProvider dp
372+
*/
373+
public function testFoo() {}
374+
public function dp() {}
375+
}',
376+
377+
[
378+
'prefix' => 'dataProviderFor',
379+
'suffix' => '',
380+
],
381+
];
280382
}
281383
}

0 commit comments

Comments
 (0)