PhpUnitDataProviderNameFixerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\PhpUnit;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderNameFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderNameFixer>
  20. *
  21. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderNameFixer
  22. */
  23. final class PhpUnitDataProviderNameFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @param _AutogeneratedInputConfiguration $configuration
  27. *
  28. * @dataProvider provideFixCases
  29. */
  30. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  31. {
  32. $this->fixer->configure($configuration);
  33. $this->doTest($expected, $input);
  34. }
  35. /**
  36. * @return iterable<array{0: string, 1?: string, 2?: array<string, string>}>
  37. */
  38. public static function provideFixCases(): iterable
  39. {
  40. yield 'data provider named with different casing' => [
  41. '<?php
  42. class FooTest extends TestCase {
  43. /**
  44. * @dataProvider provideFooCases
  45. */
  46. public function testFoo() {}
  47. public function provideFooCases() {}
  48. }',
  49. '<?php
  50. class FooTest extends TestCase {
  51. /**
  52. * @dataProvider provideFooCases
  53. */
  54. public function testFoo() {}
  55. public function PROVIDEFOOCASES() {}
  56. }',
  57. ];
  58. yield 'fixing simple scenario with test class prefixed' => [
  59. '<?php
  60. class FooTest extends TestCase {
  61. /**
  62. * @dataProvider provideFooCases
  63. */
  64. public function testFoo() {}
  65. public function provideFooCases() {}
  66. }',
  67. '<?php
  68. class FooTest extends TestCase {
  69. /**
  70. * @dataProvider fooDataProvider
  71. */
  72. public function testFoo() {}
  73. public function fooDataProvider() {}
  74. }',
  75. ];
  76. yield 'fixing simple scenario with test class annotated' => [
  77. '<?php
  78. class FooTest extends TestCase {
  79. /**
  80. * @test
  81. * @dataProvider provideFooCases
  82. */
  83. public function foo() {}
  84. public function provideFooCases() {}
  85. }',
  86. '<?php
  87. class FooTest extends TestCase {
  88. /**
  89. * @test
  90. * @dataProvider fooDataProvider
  91. */
  92. public function foo() {}
  93. public function fooDataProvider() {}
  94. }',
  95. ];
  96. yield 'data provider not found' => [
  97. '<?php
  98. class FooTest extends TestCase {
  99. /**
  100. * @dataProvider notExistingFunction
  101. */
  102. public function testFoo() {}
  103. }',
  104. ];
  105. yield 'data provider used multiple times' => [
  106. '<?php
  107. class FooTest extends TestCase {
  108. /**
  109. * @dataProvider reusedDataProvider
  110. */
  111. public function testFoo() {}
  112. /**
  113. * @dataProvider reusedDataProvider
  114. */
  115. public function testBar() {}
  116. public function reusedDataProvider() {}
  117. }',
  118. ];
  119. yield 'data provider call without function' => [
  120. '<?php
  121. class FooTest extends TestCase {
  122. /**
  123. * @dataProvider fooDataProvider
  124. */
  125. private $prop;
  126. }',
  127. ];
  128. yield 'data provider target name already used' => [
  129. '<?php
  130. class FooTest extends TestCase {
  131. /**
  132. * @dataProvider dataProvider
  133. */
  134. public function testFoo() {}
  135. public function dataProvider() {}
  136. public function provideFooCases() {}
  137. }',
  138. ];
  139. yield 'data provider defined for anonymous function' => [
  140. '<?php
  141. class FooTest extends TestCase {
  142. public function testFoo()
  143. {
  144. /**
  145. * @dataProvider notDataProvider
  146. */
  147. function () { return true; };
  148. }
  149. public function notDataProvider() {}
  150. }',
  151. ];
  152. yield 'multiple data providers for one test function' => [
  153. '<?php
  154. class FooTest extends TestCase {
  155. /**
  156. * @dataProvider foo1DataProvider
  157. * @dataProvider foo2DataProvider
  158. */
  159. public function testFoo() {}
  160. public function foo1DataProvider() {}
  161. public function foo2DataProvider() {}
  162. }',
  163. ];
  164. yield 'data provider with new name being part of FQCN used in the code' => [
  165. '<?php
  166. class FooTest extends TestCase {
  167. /**
  168. * @dataProvider provideFooCases
  169. */
  170. public function testFoo() {
  171. $x = Foo\ProvideFooCases::X_DEFAULT;
  172. }
  173. public function provideFooCases() {}
  174. }',
  175. '<?php
  176. class FooTest extends TestCase {
  177. /**
  178. * @dataProvider foo
  179. */
  180. public function testFoo() {
  181. $x = Foo\ProvideFooCases::X_DEFAULT;
  182. }
  183. public function foo() {}
  184. }',
  185. ];
  186. yield 'complex example' => [
  187. '<?php
  188. class FooTest extends TestCase {
  189. /** @dataProvider notExistingFunction */
  190. public function testClosure()
  191. {
  192. /** Preparing data */
  193. $x = 0;
  194. /** @dataProvider notDataProvider */
  195. function () { return true; };
  196. }
  197. /**
  198. * @dataProvider reusedDataProvider
  199. * @dataProvider testFooProvider
  200. */
  201. public function testFoo() {}
  202. /**
  203. * @dataProvider reusedDataProvider
  204. * @dataProvider testBarProvider
  205. */
  206. public function testBar() {}
  207. public function reusedDataProvider() {}
  208. /** @dataProvider provideBazCases */
  209. public function testBaz() {}
  210. public function provideBazCases() {}
  211. /** @dataProvider provideSomethingCases */
  212. public function testSomething() {}
  213. public function provideSomethingCases() {}
  214. public function testFooProvider() {}
  215. public function testBarProvider() {}
  216. }',
  217. '<?php
  218. class FooTest extends TestCase {
  219. /** @dataProvider notExistingFunction */
  220. public function testClosure()
  221. {
  222. /** Preparing data */
  223. $x = 0;
  224. /** @dataProvider notDataProvider */
  225. function () { return true; };
  226. }
  227. /**
  228. * @dataProvider reusedDataProvider
  229. * @dataProvider testFooProvider
  230. */
  231. public function testFoo() {}
  232. /**
  233. * @dataProvider reusedDataProvider
  234. * @dataProvider testBarProvider
  235. */
  236. public function testBar() {}
  237. public function reusedDataProvider() {}
  238. /** @dataProvider provideBazCases */
  239. public function testBaz() {}
  240. public function provideBazCases() {}
  241. /** @dataProvider someDataProvider */
  242. public function testSomething() {}
  243. public function someDataProvider() {}
  244. public function testFooProvider() {}
  245. public function testBarProvider() {}
  246. }',
  247. ];
  248. yield 'fixing when string like expected data provider name is present' => [
  249. '<?php
  250. class FooTest extends TestCase {
  251. /**
  252. * @dataProvider provideFooCases
  253. */
  254. public function testFoo() {
  255. $foo->provideFooCases(); // do not get fooled that data provider name is already taken
  256. }
  257. public function provideFooCases() {}
  258. }',
  259. '<?php
  260. class FooTest extends TestCase {
  261. /**
  262. * @dataProvider fooDataProvider
  263. */
  264. public function testFoo() {
  265. $foo->provideFooCases(); // do not get fooled that data provider name is already taken
  266. }
  267. public function fooDataProvider() {}
  268. }',
  269. ];
  270. foreach (['abstract', 'final', 'private', 'protected', 'static', '/* private */'] as $modifier) {
  271. yield \sprintf('test function with %s modifier', $modifier) => [
  272. \sprintf('<?php
  273. abstract class FooTest extends TestCase {
  274. /**
  275. * @dataProvider provideFooCases
  276. */
  277. %s function testFoo() %s
  278. public function provideFooCases() {}
  279. }', $modifier, 'abstract' === $modifier ? ';' : '{}'),
  280. \sprintf('<?php
  281. abstract class FooTest extends TestCase {
  282. /**
  283. * @dataProvider fooDataProvider
  284. */
  285. %s function testFoo() %s
  286. public function fooDataProvider() {}
  287. }', $modifier, 'abstract' === $modifier ? ';' : '{}'),
  288. ];
  289. }
  290. foreach (
  291. [
  292. 'custom prefix' => [
  293. 'theBestPrefixFooCases',
  294. 'testFoo',
  295. ['prefix' => 'theBestPrefix'],
  296. ],
  297. 'custom suffix' => [
  298. 'provideFooTheBestSuffix',
  299. 'testFoo',
  300. ['suffix' => 'TheBestSuffix'],
  301. ],
  302. 'custom prefix and suffix' => [
  303. 'theBestPrefixFooTheBestSuffix',
  304. 'testFoo',
  305. ['prefix' => 'theBestPrefix', 'suffix' => 'TheBestSuffix'],
  306. ],
  307. 'empty prefix' => [
  308. 'fooDataProvider',
  309. 'testFoo',
  310. ['prefix' => '', 'suffix' => 'DataProvider'],
  311. ],
  312. 'empty suffix' => [
  313. 'dataProviderForFoo',
  314. 'testFoo',
  315. ['prefix' => 'dataProviderFor', 'suffix' => ''],
  316. ],
  317. 'prefix and suffix with underscores' => [
  318. 'provide_foo_data',
  319. 'test_foo',
  320. ['prefix' => 'provide_', 'suffix' => '_data'],
  321. ],
  322. 'empty prefix and suffix with underscores' => [
  323. 'foo_data_provider',
  324. 'test_foo',
  325. ['prefix' => '', 'suffix' => '_data_provider'],
  326. ],
  327. 'prefix with underscores and empty suffix' => [
  328. 'data_provider_foo',
  329. 'test_foo',
  330. ['prefix' => 'data_provider_', 'suffix' => ''],
  331. ],
  332. 'prefix with underscores and empty suffix and test function starting with uppercase' => [
  333. 'data_provider_Foo',
  334. 'test_Foo',
  335. ['prefix' => 'data_provider_', 'suffix' => ''],
  336. ],
  337. 'prefix and suffix with underscores and test function having multiple consecutive underscores' => [
  338. 'provide_foo_data',
  339. 'test___foo',
  340. ['prefix' => 'provide_', 'suffix' => '_data'],
  341. ],
  342. 'uppercase naming' => [
  343. 'PROVIDE_FOO_DATA',
  344. 'TEST_FOO',
  345. ['prefix' => 'PROVIDE_', 'suffix' => '_DATA'],
  346. ],
  347. 'camelCase test function and prefix with underscores' => [
  348. 'data_provider_FooBar',
  349. 'testFooBar',
  350. ['prefix' => 'data_provider_', 'suffix' => ''],
  351. ],
  352. ] as $name => [$dataProvider, $testFunction, $config]
  353. ) {
  354. yield $name => [
  355. \sprintf('<?php
  356. class FooTest extends TestCase {
  357. /**
  358. * @dataProvider %s
  359. */
  360. public function %s() {}
  361. public function %s() {}
  362. }', $dataProvider, $testFunction, $dataProvider),
  363. \sprintf('<?php
  364. class FooTest extends TestCase {
  365. /**
  366. * @dataProvider dtPrvdr
  367. */
  368. public function %s() {}
  369. public function dtPrvdr() {}
  370. }', $testFunction),
  371. $config,
  372. ];
  373. }
  374. }
  375. }