123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Tokenizer\Analyzer;
- use PhpCsFixer\Tests\TestCase;
- use PhpCsFixer\Tokenizer\Analyzer\Analysis\DataProviderAnalysis;
- use PhpCsFixer\Tokenizer\Analyzer\DataProviderAnalyzer;
- use PhpCsFixer\Tokenizer\Tokens;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\Tokenizer\Analyzer\DataProviderAnalyzer
- */
- final class DataProviderAnalyzerTest extends TestCase
- {
- /**
- * @param list<DataProviderAnalysis> $expected
- *
- * @dataProvider provideGettingDataProvidersCases
- */
- public function testGettingDataProviders(array $expected, string $code, int $startIndex = 0, ?int $endIndex = null): void
- {
- $tokens = Tokens::fromCode($code);
- if (null === $endIndex) {
- $endIndex = $tokens->count() - 1;
- }
- $analyzer = new DataProviderAnalyzer();
- self::assertSame(serialize($expected), serialize($analyzer->getDataProviders($tokens, $startIndex, $endIndex)));
- }
- /**
- * @return iterable<array{list<DataProviderAnalysis>, string}>
- */
- public static function provideGettingDataProvidersCases(): iterable
- {
- yield 'single data provider' => [
- [new DataProviderAnalysis('provider', 28, [[11, 23]])],
- '<?php class FooTest extends TestCase {
- /**
- * @dataProvider provider
- */
- public function testFoo() {}
- public function provider() {}
- }',
- ];
- yield 'single data provider with different casing' => [
- [new DataProviderAnalysis('dataProvider', 28, [[11, 23]])],
- '<?php class FooTest extends TestCase {
- /**
- * @dataProvider dataPROVIDER
- */
- public function testFoo() {}
- public function dataProvider() {}
- }',
- ];
- yield 'single static data provider' => [
- [new DataProviderAnalysis('provider', 30, [[11, 23]])],
- '<?php class FooTest extends TestCase {
- /**
- * @dataProvider provider
- */
- public function testFoo() {}
- public static function provider() {}
- }',
- ];
- yield 'multiple data provider' => [
- [
- new DataProviderAnalysis('provider1', 28, [[11, 23]]),
- new DataProviderAnalysis('provider2', 39, [[11, 66]]),
- new DataProviderAnalysis('provider3', 50, [[11, 109]]),
- ],
- '<?php class FooTest extends TestCase {
- /**
- * @dataProvider provider1
- * @dataProvider provider2
- * @dataProvider provider3
- */
- public function testFoo() {}
- public function provider1() {}
- public function provider2() {}
- public function provider3() {}
- }',
- ];
- yield 'single data provider with multiple usage' => [
- [
- new DataProviderAnalysis('provider', 28, [[11, 23], [35, 23]]),
- ],
- '<?php class FooTest extends TestCase {
- /**
- * @dataProvider provider
- */
- public function testFoo() {}
- public function provider() {}
- /**
- * @dataProvider provider
- */
- public function testFoo2() {}
- }',
- ];
- foreach (['abstract', 'final', 'private', 'protected', 'static', '/* private */'] as $modifier) {
- yield \sprintf('test function with %s modifier', $modifier) => [
- [
- new DataProviderAnalysis('provider1', 54, [[37, 4]]),
- new DataProviderAnalysis('provider2', 65, [[11, 4]]),
- new DataProviderAnalysis('provider3', 76, [[24, 4]]),
- ],
- \sprintf('<?php class FooTest extends TestCase {
- /** @dataProvider provider2 */
- public function testFoo1() {}
- /** @dataProvider provider3 */
- %s function testFoo2() {}
- /** @dataProvider provider1 */
- public function testFoo3() {}
- public function provider1() {}
- public function provider2() {}
- public function provider3() {}
- }', $modifier),
- ];
- }
- yield 'not existing data provider used' => [
- [],
- '<?php class FooTest extends TestCase {
- /**
- * @dataProvider provider
- */
- public function testFoo() {}
- }',
- ];
- yield 'data provider being constant' => [
- [],
- '<?php class FooTest extends TestCase {
- private const provider = [];
- /**
- * @dataProvider provider
- */
- public function testFoo() {}
- }',
- ];
- yield 'ignore anonymous function' => [
- [
- new DataProviderAnalysis('provider2', 93, [[65, 27]]),
- ],
- '<?php class FooTest extends TestCase {
- public function testFoo0() {}
- /**
- * @dataProvider provider0
- */
- public function testFoo1()
- {
- /**
- * @dataProvider provider1
- */
- $f = function ($x, $y) { return $x + $y; };
- }
- /**
- * @dataProvider provider2
- */
- public function testFoo2() {}
- public function provider1() {}
- public function provider2() {}
- }',
- ];
- }
- /**
- * @param list<DataProviderAnalysis> $expected
- *
- * @requires PHP ^8.0
- *
- * @dataProvider provideGettingDataProviders80Cases
- */
- public function testGettingDataProviders80(array $expected, string $code, int $startIndex = 0, ?int $endIndex = null): void
- {
- $this->testGettingDataProviders($expected, $code, $startIndex, $endIndex);
- }
- /**
- * @return iterable<array{list<DataProviderAnalysis>, string}>
- */
- public static function provideGettingDataProviders80Cases(): iterable
- {
- yield 'with an attribute between PHPDoc and test method' => [
- [new DataProviderAnalysis('provideFooCases', 35, [[11, 11]])],
- <<<'PHP'
- <?php
- class FooTest extends TestCase {
- /**
- * @dataProvider provideFooCases
- */
- #[CustomAttribute]
- public function testFoo(): void {}
- public function provideFooCases(): iterable {}
- }
- PHP,
- ];
- }
- }
|