PhpUnitDataProviderNameFixerTest.php 11 KB

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