PhpUnitDataProviderMethodOrderFixerTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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\PhpUnitDataProviderMethodOrderFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderMethodOrderFixer>
  20. *
  21. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderMethodOrderFixer
  22. */
  23. final class PhpUnitDataProviderMethodOrderFixerTest 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?: null|string, 2?: array<string, string>}>
  37. */
  38. public static function provideFixCases(): iterable
  39. {
  40. yield 'simple - placement after' => [
  41. <<<'PHP'
  42. <?php
  43. class FooTest extends TestCase {
  44. /** @dataProvider provideFooCases */
  45. public function testFoo(): void {}
  46. public function provideFooCases(): iterable {}
  47. }
  48. PHP,
  49. <<<'PHP'
  50. <?php
  51. class FooTest extends TestCase {
  52. public function provideFooCases(): iterable {}
  53. /** @dataProvider provideFooCases */
  54. public function testFoo(): void {}
  55. }
  56. PHP,
  57. ];
  58. yield 'simple - placement before' => [
  59. <<<'PHP'
  60. <?php
  61. class FooTest extends TestCase {
  62. public function provideFooCases(): iterable {}
  63. /** @dataProvider provideFooCases */
  64. public function testFoo(): void {}
  65. }
  66. PHP,
  67. <<<'PHP'
  68. <?php
  69. class FooTest extends TestCase {
  70. /** @dataProvider provideFooCases */
  71. public function testFoo(): void {}
  72. public function provideFooCases(): iterable {}
  73. }
  74. PHP,
  75. ['placement' => 'before'],
  76. ];
  77. yield 'empty test class' => [
  78. <<<'PHP'
  79. <?php
  80. class FooTest extends TestCase {}
  81. PHP,
  82. ];
  83. yield 'data provider named with different casing' => [
  84. <<<'PHP'
  85. <?php
  86. class FooTest extends TestCase {
  87. /** @dataProvider provideFooCases */
  88. public function testFoo(): void {}
  89. public function PROVIDEFOOCASES(): iterable {}
  90. }
  91. PHP,
  92. <<<'PHP'
  93. <?php
  94. class FooTest extends TestCase {
  95. public function PROVIDEFOOCASES(): iterable {}
  96. /** @dataProvider provideFooCases */
  97. public function testFoo(): void {}
  98. }
  99. PHP,
  100. ];
  101. yield 'with test method annotated' => [
  102. <<<'PHP'
  103. <?php
  104. class FooTest extends TestCase {
  105. /**
  106. * @test
  107. * @dataProvider provideFooCases
  108. */
  109. public function foo(): void {}
  110. public function provideFooCases(): iterable {}
  111. }
  112. PHP,
  113. <<<'PHP'
  114. <?php
  115. class FooTest extends TestCase {
  116. public function provideFooCases(): iterable {}
  117. /**
  118. * @test
  119. * @dataProvider provideFooCases
  120. */
  121. public function foo(): void {}
  122. }
  123. PHP,
  124. ];
  125. yield 'data provider not found' => [
  126. <<<'PHP'
  127. <?php
  128. class FooTest extends TestCase {
  129. public function provideFooCases(): iterable {}
  130. /** @dataProvider notExistingFunction */
  131. public function testFoo(): void {}
  132. }
  133. PHP,
  134. ];
  135. yield 'data provider used multiple times - placement after' => [
  136. <<<'PHP'
  137. <?php
  138. class FooTest extends TestCase {
  139. /** @dataProvider provideFooCases */
  140. public function testFoo(): void {}
  141. /** @dataProvider provideFooCases */
  142. public function testFoo2(): void {}
  143. public function provideFooCases(): iterable {}
  144. }
  145. PHP,
  146. <<<'PHP'
  147. <?php
  148. class FooTest extends TestCase {
  149. public function provideFooCases(): iterable {}
  150. /** @dataProvider provideFooCases */
  151. public function testFoo(): void {}
  152. /** @dataProvider provideFooCases */
  153. public function testFoo2(): void {}
  154. }
  155. PHP,
  156. ];
  157. yield 'data provider used multiple times - placement after - do not move provider if already after first test where used' => [
  158. <<<'PHP'
  159. <?php
  160. class FooTest extends TestCase {
  161. /** @dataProvider provideFooCases */
  162. public function testFoo(): void {}
  163. public function provideFooCases(): iterable {}
  164. /** @dataProvider provideFooCases */
  165. public function testBar(): void {}
  166. }
  167. PHP,
  168. ];
  169. yield 'data provider used multiple times - placement before' => [
  170. <<<'PHP'
  171. <?php
  172. class FooTest extends TestCase {
  173. public function provideFooCases(): iterable {}
  174. /** @dataProvider provideFooCases */
  175. public function testFoo(): void {}
  176. /** @dataProvider provideFooCases */
  177. public function testFoo2(): void {}
  178. }
  179. PHP,
  180. <<<'PHP'
  181. <?php
  182. class FooTest extends TestCase {
  183. /** @dataProvider provideFooCases */
  184. public function testFoo(): void {}
  185. /** @dataProvider provideFooCases */
  186. public function testFoo2(): void {}
  187. public function provideFooCases(): iterable {}
  188. }
  189. PHP,
  190. ['placement' => 'before'],
  191. ];
  192. yield 'multiple data providers for one test method' => [
  193. <<<'PHP'
  194. <?php
  195. class FooTest extends TestCase {
  196. /**
  197. * @dataProvider provideFooCases
  198. * @dataProvider provideFooCases3
  199. * @dataProvider provideFooCases2
  200. */
  201. public function testFoo(): void {}
  202. public function provideFooCases(): iterable {}
  203. public function provideFooCases3(): iterable {}
  204. public function provideFooCases2(): iterable {}
  205. }
  206. PHP,
  207. <<<'PHP'
  208. <?php
  209. class FooTest extends TestCase {
  210. public function provideFooCases(): iterable {}
  211. /**
  212. * @dataProvider provideFooCases
  213. * @dataProvider provideFooCases3
  214. * @dataProvider provideFooCases2
  215. */
  216. public function testFoo(): void {}
  217. public function provideFooCases2(): iterable {}
  218. public function provideFooCases3(): iterable {}
  219. }
  220. PHP,
  221. ];
  222. yield 'data provider used multiple times II - placement after' => [
  223. <<<'PHP'
  224. <?php
  225. class FooTest {
  226. /** @dataProvider provideACases */
  227. public function testA1(): void {}
  228. /** @dataProvider provideBCases */
  229. public function testB(): void {}
  230. public static function provideBCases(): iterable {}
  231. /** @dataProvider provideACases */
  232. public function testA2(): void {}
  233. public static function provideACases(): iterable {}
  234. }
  235. PHP,
  236. ];
  237. yield 'data provider used multiple times II - placement before' => [
  238. <<<'PHP'
  239. <?php
  240. class FooTest {
  241. public static function provideACases(): iterable {}
  242. /** @dataProvider provideACases */
  243. public function testA2(): void {}
  244. public static function provideBCases(): iterable {}
  245. /** @dataProvider provideBCases */
  246. public function testB(): void {}
  247. /** @dataProvider provideACases */
  248. public function testA1(): void {}
  249. }
  250. PHP,
  251. null,
  252. ['placement' => 'before'],
  253. ];
  254. yield 'with other methods - placement after' => [
  255. <<<'PHP'
  256. <?php
  257. class FooTest extends TestCase {
  258. public function testA(): void {}
  259. /** @dataProvider provideFooCases */
  260. public function testFoo(): void {}
  261. public function provideFooCases(): iterable {}
  262. public function testB(): void {}
  263. }
  264. PHP,
  265. <<<'PHP'
  266. <?php
  267. class FooTest extends TestCase {
  268. public function testA(): void {}
  269. public function provideFooCases(): iterable {}
  270. /** @dataProvider provideFooCases */
  271. public function testFoo(): void {}
  272. public function testB(): void {}
  273. }
  274. PHP,
  275. ];
  276. yield 'with other methods - placement before' => [
  277. <<<'PHP'
  278. <?php
  279. class FooTest extends TestCase {
  280. public function testA(): void {}
  281. public function provideFooCases(): iterable {}
  282. /** @dataProvider provideFooCases */
  283. public function testFoo(): void {}
  284. public function testB(): void {}
  285. }
  286. PHP,
  287. <<<'PHP'
  288. <?php
  289. class FooTest extends TestCase {
  290. public function testA(): void {}
  291. /** @dataProvider provideFooCases */
  292. public function testFoo(): void {}
  293. public function provideFooCases(): iterable {}
  294. public function testB(): void {}
  295. }
  296. PHP,
  297. ['placement' => 'before'],
  298. ];
  299. yield 'with other methods II' => [
  300. <<<'PHP'
  301. <?php
  302. class FooTest extends TestCase {
  303. public function testA(): void {}
  304. public function testB(): void {}
  305. /** @dataProvider provideFooCases */
  306. public function testFoo(): void {}
  307. public function provideFooCases(): iterable {}
  308. public function testC(): void {}
  309. }
  310. PHP,
  311. <<<'PHP'
  312. <?php
  313. class FooTest extends TestCase {
  314. public function testA(): void {}
  315. public function provideFooCases(): iterable {}
  316. public function testB(): void {}
  317. /** @dataProvider provideFooCases */
  318. public function testFoo(): void {}
  319. public function testC(): void {}
  320. }
  321. PHP,
  322. ];
  323. }
  324. /**
  325. * @requires PHP ^8.0
  326. *
  327. * @param _AutogeneratedInputConfiguration $configuration
  328. *
  329. * @dataProvider provideFix80Cases
  330. */
  331. public function testFix80(string $expected, ?string $input = null, array $configuration = []): void
  332. {
  333. $this->fixer->configure($configuration);
  334. $this->doTest($expected, $input);
  335. }
  336. /**
  337. * @return iterable<array{0: string, 1?: string, 2?: array<string, string>}>
  338. */
  339. public static function provideFix80Cases(): iterable
  340. {
  341. yield 'with an attribute between PHPDoc and data provider/test method - placement after' => [
  342. <<<'PHP'
  343. <?php
  344. class FooTest extends TestCase {
  345. /**
  346. * @dataProvider provideFooCases
  347. */
  348. #[CustomTestAttribute]
  349. public function testFoo(): void {}
  350. #[CustomProviderAttribute]
  351. public function provideFooCases(): iterable {}
  352. }
  353. PHP,
  354. <<<'PHP'
  355. <?php
  356. class FooTest extends TestCase {
  357. #[CustomProviderAttribute]
  358. public function provideFooCases(): iterable {}
  359. /**
  360. * @dataProvider provideFooCases
  361. */
  362. #[CustomTestAttribute]
  363. public function testFoo(): void {}
  364. }
  365. PHP,
  366. ];
  367. yield 'with an attribute between PHPDoc and data provider/test method - placement before' => [
  368. <<<'PHP'
  369. <?php
  370. class FooTest extends TestCase {
  371. #[CustomProviderAttribute]
  372. public function provideFooCases(): iterable {}
  373. /**
  374. * @dataProvider provideFooCases
  375. */
  376. #[CustomTestAttribute]
  377. public function testFoo(): void {}
  378. }
  379. PHP,
  380. <<<'PHP'
  381. <?php
  382. class FooTest extends TestCase {
  383. /**
  384. * @dataProvider provideFooCases
  385. */
  386. #[CustomTestAttribute]
  387. public function testFoo(): void {}
  388. #[CustomProviderAttribute]
  389. public function provideFooCases(): iterable {}
  390. }
  391. PHP,
  392. ['placement' => 'before'],
  393. ];
  394. yield 'data provider defined by an attribute' => [ // update expected once https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/pull/8197 is merged
  395. <<<'PHP'
  396. <?php
  397. class FooTest extends TestCase {
  398. public function provideFooCases(): iterable {}
  399. #[DataProvider('provideFooCases')]
  400. public function testFoo(): void {}
  401. }
  402. PHP,
  403. ];
  404. }
  405. }