Skip to content

Commit dc17012

Browse files
authored
Test data providers for case duplications (#711)
1 parent 42b7cac commit dc17012

File tree

7 files changed

+60
-46
lines changed

7 files changed

+60
-46
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
44
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
55
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
6-
![Tests](https://img.shields.io/badge/tests-3253-brightgreen.svg)
6+
![Tests](https://img.shields.io/badge/tests-3312-brightgreen.svg)
77
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
88

99
[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=main&event=push)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)

tests/AutoReview/TestsCodeTest.php

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\Component\Finder\Finder;
1717
use Symfony\Component\Finder\SplFileInfo;
18+
use Tests\Fixer\AbstractFixerTestCase;
1819

1920
/**
2021
* @internal
@@ -84,13 +85,9 @@ public function testDataProvidersKeys(string $className): void
8485

8586
foreach ($dataProviders as $dataProvider) {
8687
$dataSet = $dataProvider->invoke(null);
87-
\assert(\is_array($dataSet) || $dataSet instanceof \Generator);
88+
\assert($dataSet instanceof \Iterator);
8889

89-
if ($dataSet instanceof \Generator) {
90-
$dataSet = \iterator_to_array($dataSet);
91-
}
92-
93-
foreach (\array_keys($dataSet) as $key) {
90+
foreach (\array_keys(\iterator_to_array($dataSet)) as $key) {
9491
if (!\is_string($key)) {
9592
self::markTestIncomplete(\sprintf(
9693
'Data provider "%s" in class "%s" has non-string keys.',
@@ -106,6 +103,48 @@ public function testDataProvidersKeys(string $className): void
106103
}
107104
}
108105

106+
/**
107+
* @dataProvider provideTestClassCases
108+
*/
109+
public function testDataProvidersValues(string $className): void
110+
{
111+
if (!\is_subclass_of($className, AbstractFixerTestCase::class)) {
112+
$this->expectNotToPerformAssertions();
113+
114+
return;
115+
}
116+
117+
$dataProviders = $this->getDataProviders($className);
118+
119+
foreach ($dataProviders as $dataProvider) {
120+
$dataSet = $dataProvider->invoke(null);
121+
\assert($dataSet instanceof \Iterator);
122+
$dataSet = \iterator_to_array($dataSet);
123+
124+
$doNotChangeCases = [];
125+
foreach ($dataSet as $value) {
126+
if (\array_key_exists(1, $value) && $value[1] !== null) {
127+
continue;
128+
}
129+
$doNotChangeCases[] = $value[0];
130+
}
131+
foreach ($dataSet as $value) {
132+
if (!\array_key_exists(1, $value) || $value[1] === null) {
133+
continue;
134+
}
135+
self::assertFalse(
136+
\in_array($value[0], $doNotChangeCases, true),
137+
\sprintf(
138+
"Expected value:\n%s\nis already tested if it is not changing, it does not need separate test case (%s::%s).",
139+
$value[0],
140+
$className,
141+
$dataProvider->getName()
142+
)
143+
);
144+
}
145+
}
146+
}
147+
109148
/**
110149
* @return array<\ReflectionMethod>
111150
*/
@@ -159,6 +198,8 @@ public static function provideTestClassCases(): iterable
159198
$className .= '\\' . $file->getBasename('.php');
160199
$tests[$className] = [$className];
161200
}
201+
202+
$tests = new \ArrayIterator($tests);
162203
}
163204

164205
return $tests;

tests/Fixer/CommentSurroundedBySpacesFixerTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ public static function provideFixCases(): iterable
3939
yield ['<?php $a; //'];
4040
yield ['<?php $a; ////'];
4141
yield ['<?php $a; /**/'];
42-
yield ['<?php $a; // foo'];
43-
yield ['<?php $a; # foo'];
44-
yield ['<?php $a; /* foo */'];
45-
yield ['<?php $a; /** foo */'];
4642
yield ['<?php $a; /** foo */'];
4743
yield ["<?php AA; /**\tfoo\t*/"];
4844

tests/Fixer/DataProviderNameFixerTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,6 @@ public function testFix(string $expected, ?string $input = null, ?array $configu
5252
*/
5353
public static function provideFixCases(): iterable
5454
{
55-
yield 'data provider correctly named' => [
56-
'<?php
57-
class FooTest extends TestCase {
58-
/**
59-
* @dataProvider provideFooCases
60-
*/
61-
public function testFoo() {}
62-
public function provideFooCases() {}
63-
}',
64-
];
65-
6655
yield 'data provider named with different casing' => [
6756
'<?php
6857
class FooTest extends TestCase {

tests/Fixer/PhpdocParamTypeFixerTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ public function testFix(string $expected, ?string $input = null): void
3636
*/
3737
public static function provideFixCases(): iterable
3838
{
39-
yield [
40-
'<?php
41-
/**
42-
* @param mixed $foo
43-
*/
44-
',
45-
];
46-
4739
yield [
4840
'<?php
4941
/**

tests/Fixer/SingleSpaceBeforeStatementFixerTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public static function provideFixCases(): iterable
3939
yield ['<?php $isNotFoo = !require "foo.php";'];
4040
yield ['<?php foo(new stdClass());'];
4141
yield ['<?php $content = @include "foo.php";'];
42-
yield ['<?php $items = [new Item(), new Item()];'];
4342
yield ['<?php class Foo {public function bar() {}}'];
4443
yield ['<?php foo(
4544
new Item()

tests/FixersTest.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use PhpCsFixerCustomFixers\Fixers;
1616
use PHPUnit\Framework\TestCase;
1717
use Symfony\Component\Finder\Finder;
18-
use Symfony\Component\Finder\SplFileInfo;
1918

2019
/**
2120
* @internal
@@ -47,22 +46,20 @@ public function testFixerIsInCollection(FixerInterface $fixer): void
4746
*/
4847
public static function provideFixerIsInCollectionCases(): iterable
4948
{
50-
return \array_map(
51-
static function (SplFileInfo $fileInfo): array {
52-
$className = 'PhpCsFixerCustomFixers\\Fixer\\' . $fileInfo->getBasename('.php');
49+
$finder = Finder::create()
50+
->files()
51+
->in(__DIR__ . '/../src/Fixer/')
52+
->notName('AbstractFixer.php')
53+
->notName('DeprecatingFixerInterface.php');
5354

54-
$fixer = new $className();
55-
\assert($fixer instanceof FixerInterface);
55+
foreach ($finder as $file) {
56+
$className = 'PhpCsFixerCustomFixers\\Fixer\\' . $file->getBasename('.php');
5657

57-
return [$fixer];
58-
},
59-
\iterator_to_array(Finder::create()
60-
->files()
61-
->in(__DIR__ . '/../src/Fixer/')
62-
->notName('AbstractFixer.php')
63-
->notName('DeprecatingFixerInterface.php')
64-
->getIterator())
65-
);
58+
$fixer = new $className();
59+
\assert($fixer instanceof FixerInterface);
60+
61+
yield $className => [$fixer];
62+
}
6663
}
6764

6865
/**

0 commit comments

Comments
 (0)