PhpUnitExpectationFixerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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\Fixer\PhpUnit\PhpUnitTargetVersion;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. use PhpCsFixer\WhitespacesFixerConfig;
  16. /**
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitExpectationFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitExpectationFixer>
  24. *
  25. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitExpectationFixer
  26. */
  27. final class PhpUnitExpectationFixerTest extends AbstractFixerTestCase
  28. {
  29. /**
  30. * @param _AutogeneratedInputConfiguration $config
  31. *
  32. * @dataProvider provideFixCases
  33. */
  34. public function testFix(string $expected, ?string $input = null, array $config = []): void
  35. {
  36. $this->fixer->configure($config);
  37. $this->doTest($expected, $input);
  38. }
  39. public static function provideFixCases(): iterable
  40. {
  41. yield [
  42. '<?php
  43. final class MyTest extends \PHPUnit_Framework_TestCase
  44. {
  45. function testFnc()
  46. {
  47. aaa();
  48. $this->expectException(\'RuntimeException\');
  49. $this->expectExceptionMessage(\'msg\'/*B*/);
  50. $this->expectExceptionCode(/*C*/123);
  51. zzz();
  52. }
  53. }',
  54. '<?php
  55. final class MyTest extends \PHPUnit_Framework_TestCase
  56. {
  57. function testFnc()
  58. {
  59. aaa();
  60. $this->setExpectedException(\'RuntimeException\', \'msg\'/*B*/, /*C*/123);
  61. zzz();
  62. }
  63. }',
  64. ];
  65. yield [
  66. '<?php
  67. final class MyTest extends \PHPUnit_Framework_TestCase
  68. {
  69. function testFnc()
  70. {
  71. aaa();
  72. $this->expectException(\'RuntimeException\'/*B*/ /*B2*/);
  73. $this->expectExceptionCode(/*C*/123);
  74. zzz();
  75. }
  76. function testFnc2()
  77. {
  78. aaa();
  79. $this->expectException(\'RuntimeException\');
  80. $this->expectExceptionMessage(/*B*/ null /*B2*/ + 1);
  81. $this->expectExceptionCode(/*C*/123);
  82. zzz();
  83. }
  84. }',
  85. '<?php
  86. final class MyTest extends \PHPUnit_Framework_TestCase
  87. {
  88. function testFnc()
  89. {
  90. aaa();
  91. $this->setExpectedException(\'RuntimeException\',/*B*/ null /*B2*/,/*C*/123);
  92. zzz();
  93. }
  94. function testFnc2()
  95. {
  96. aaa();
  97. $this->setExpectedException(\'RuntimeException\',/*B*/ null /*B2*/ + 1,/*C*/123);
  98. zzz();
  99. }
  100. }',
  101. ];
  102. yield [
  103. '<?php
  104. final class MyTest extends \PHPUnit_Framework_TestCase
  105. {
  106. function testFnc()
  107. {
  108. $this->expectException(
  109. \Exception::class
  110. );
  111. }
  112. }',
  113. '<?php
  114. final class MyTest extends \PHPUnit_Framework_TestCase
  115. {
  116. function testFnc()
  117. {
  118. $this->setExpectedException(
  119. \Exception::class
  120. );
  121. }
  122. }',
  123. ];
  124. yield [
  125. '<?php
  126. final class MyTest extends \PHPUnit_Framework_TestCase
  127. {
  128. function testFnc()
  129. {
  130. $this->expectException(
  131. \Exception::class
  132. );
  133. $this->expectExceptionMessage(
  134. "foo"
  135. );
  136. $this->expectExceptionCode(
  137. 123
  138. );
  139. }
  140. }',
  141. '<?php
  142. final class MyTest extends \PHPUnit_Framework_TestCase
  143. {
  144. function testFnc()
  145. {
  146. $this->setExpectedException(
  147. \Exception::class,
  148. "foo",
  149. 123
  150. );
  151. }
  152. }',
  153. ];
  154. yield [
  155. '<?php
  156. final class MyTest extends \PHPUnit_Framework_TestCase
  157. {
  158. public function testFoo()
  159. {
  160. $this->expectException("RuntimeException");
  161. $this->expectExceptionMessage("Msg");
  162. $this->expectExceptionCode(123);
  163. foo();
  164. }
  165. public function testBar()
  166. {
  167. $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
  168. bar();
  169. }
  170. }',
  171. '<?php
  172. final class MyTest extends \PHPUnit_Framework_TestCase
  173. {
  174. public function testFoo()
  175. {
  176. $this->setExpectedException("RuntimeException", "Msg", 123);
  177. foo();
  178. }
  179. public function testBar()
  180. {
  181. $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
  182. bar();
  183. }
  184. }',
  185. ['target' => PhpUnitTargetVersion::VERSION_5_2],
  186. ];
  187. yield [
  188. '<?php
  189. final class MyTest extends \PHPUnit_Framework_TestCase
  190. {
  191. public function testFoo()
  192. {
  193. $this->expectException("RuntimeException");
  194. $this->expectExceptionMessage("Msg");
  195. $this->expectExceptionCode(123);
  196. foo();
  197. }
  198. public function testBar()
  199. {
  200. $this->expectException("RuntimeException");
  201. $this->expectExceptionMessageRegExp("/Msg.*/");
  202. $this->expectExceptionCode(123);
  203. bar();
  204. }
  205. }',
  206. '<?php
  207. final class MyTest extends \PHPUnit_Framework_TestCase
  208. {
  209. public function testFoo()
  210. {
  211. $this->setExpectedException("RuntimeException", "Msg", 123);
  212. foo();
  213. }
  214. public function testBar()
  215. {
  216. $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
  217. bar();
  218. }
  219. }',
  220. ['target' => PhpUnitTargetVersion::VERSION_5_6],
  221. ];
  222. yield [
  223. '<?php
  224. final class MyTest extends \PHPUnit_Framework_TestCase
  225. {
  226. public function testFoo()
  227. {
  228. $this->expectException("RuntimeException");
  229. $this->expectExceptionMessage("Msg");
  230. $this->expectExceptionCode(123);
  231. foo();
  232. }
  233. public function testBar()
  234. {
  235. $this->expectException("RuntimeException");
  236. $this->expectExceptionMessageMatches("/Msg.*/");
  237. $this->expectExceptionCode(123);
  238. bar();
  239. }
  240. }',
  241. '<?php
  242. final class MyTest extends \PHPUnit_Framework_TestCase
  243. {
  244. public function testFoo()
  245. {
  246. $this->setExpectedException("RuntimeException", "Msg", 123);
  247. foo();
  248. }
  249. public function testBar()
  250. {
  251. $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
  252. bar();
  253. }
  254. }',
  255. ['target' => PhpUnitTargetVersion::VERSION_8_4],
  256. ];
  257. yield [
  258. '<?php
  259. final class MyTest extends \PHPUnit_Framework_TestCase
  260. {
  261. public function testFoo()
  262. {
  263. $this->expectExceptionMessageMatches("/Msg.*/");
  264. foo();
  265. }
  266. }',
  267. '<?php
  268. final class MyTest extends \PHPUnit_Framework_TestCase
  269. {
  270. public function testFoo()
  271. {
  272. $this->expectExceptionMessageRegExp("/Msg.*/");
  273. foo();
  274. }
  275. }',
  276. ['target' => PhpUnitTargetVersion::VERSION_8_4],
  277. ];
  278. yield [
  279. '<?php
  280. final class MyTest extends \PHPUnit_Framework_TestCase
  281. {
  282. public function testFoo()
  283. {
  284. // turns wrong into wrong: has a single argument only, but ...
  285. $this->expectExceptionMessageMatches("/Msg.*/");
  286. $this->expectExceptionMessageMatches("fail-case");
  287. foo();
  288. }
  289. }',
  290. '<?php
  291. final class MyTest extends \PHPUnit_Framework_TestCase
  292. {
  293. public function testFoo()
  294. {
  295. // turns wrong into wrong: has a single argument only, but ...
  296. $this->expectExceptionMessageRegExp("/Msg.*/", "fail-case");
  297. foo();
  298. }
  299. }',
  300. ['target' => PhpUnitTargetVersion::VERSION_8_4],
  301. ];
  302. yield [
  303. '<?php
  304. final class MyTest extends \PHPUnit_Framework_TestCase
  305. {
  306. public function testFoo()
  307. {
  308. $this->expectException("RuntimeException");
  309. $this->expectExceptionMessage("Msg");
  310. $this->expectExceptionCode(123);
  311. foo();
  312. }
  313. public function testBar()
  314. {
  315. $this->expectException("RuntimeException");
  316. $this->expectExceptionMessageMatches("/Msg.*/");
  317. $this->expectExceptionCode(123);
  318. bar();
  319. }
  320. }',
  321. '<?php
  322. final class MyTest extends \PHPUnit_Framework_TestCase
  323. {
  324. public function testFoo()
  325. {
  326. $this->setExpectedException("RuntimeException", "Msg", 123);
  327. foo();
  328. }
  329. public function testBar()
  330. {
  331. $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
  332. bar();
  333. }
  334. }',
  335. ['target' => PhpUnitTargetVersion::VERSION_NEWEST],
  336. ];
  337. yield [
  338. '<?php
  339. final class MyTest extends \PHPUnit_Framework_TestCase
  340. {
  341. function testFnc()
  342. {
  343. aaa();
  344. $this->expectException("RuntimeException");
  345. $this->expectExceptionMessage("msg"/*B*/);
  346. $this->expectExceptionCode(/*C*/123);
  347. zzz();
  348. }
  349. }',
  350. '<?php
  351. final class MyTest extends \PHPUnit_Framework_TestCase
  352. {
  353. function testFnc()
  354. {
  355. aaa();
  356. $this->setExpectedException("RuntimeException", "msg"/*B*/, /*C*/123, );
  357. zzz();
  358. }
  359. }',
  360. ];
  361. yield [
  362. '<?php
  363. final class MyTest extends \PHPUnit_Framework_TestCase
  364. {
  365. function testFnc()
  366. {
  367. aaa();
  368. $this->expectException("RuntimeException");
  369. $this->expectExceptionMessage("msg"/*B*/);
  370. $this->expectExceptionCode(/*C*/123/*D*/);
  371. zzz();
  372. }
  373. }',
  374. '<?php
  375. final class MyTest extends \PHPUnit_Framework_TestCase
  376. {
  377. function testFnc()
  378. {
  379. aaa();
  380. $this->setExpectedException("RuntimeException", "msg"/*B*/, /*C*/123, /*D*/);
  381. zzz();
  382. }
  383. }',
  384. ];
  385. }
  386. /**
  387. * @dataProvider provideWithWhitespacesConfigCases
  388. */
  389. public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
  390. {
  391. $expected = str_replace([' ', "\n"], ["\t", "\r\n"], $expected);
  392. if (null !== $input) {
  393. $input = str_replace([' ', "\n"], ["\t", "\r\n"], $input);
  394. }
  395. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  396. $this->doTest($expected, $input);
  397. }
  398. /**
  399. * @return iterable<array{string, string}>
  400. */
  401. public static function provideWithWhitespacesConfigCases(): iterable
  402. {
  403. $expectedTemplate =
  404. '
  405. function testFnc%d()
  406. {
  407. aaa();
  408. $this->expectException(\'RuntimeException\');
  409. $this->expectExceptionMessage(\'msg\'/*B*/);
  410. $this->expectExceptionCode(/*C*/123);
  411. zzz();
  412. }
  413. ';
  414. $inputTemplate =
  415. '
  416. function testFnc%d()
  417. {
  418. aaa();
  419. $this->setExpectedException(\'RuntimeException\', \'msg\'/*B*/, /*C*/123);
  420. zzz();
  421. }
  422. ';
  423. $input = $expected = '<?php
  424. final class MyTest extends \PHPUnit_Framework_TestCase
  425. {
  426. ';
  427. for ($i = 0; $i < 8; ++$i) {
  428. $expected .= \sprintf($expectedTemplate, $i);
  429. $input .= \sprintf($inputTemplate, $i);
  430. }
  431. $expected .= "\n}";
  432. $input .= "\n}";
  433. yield [$expected, $input];
  434. }
  435. /**
  436. * @requires PHP 8.0
  437. */
  438. public function testFix80(): void
  439. {
  440. $this->doTest(
  441. '<?php
  442. final class MyTest extends \PHPUnit_Framework_TestCase
  443. {
  444. function testFnc()
  445. {
  446. aaa();
  447. $this?->expectException("RuntimeException");
  448. $this->expectExceptionMessage("message");
  449. $this->expectExceptionCode(123);
  450. zzz();
  451. }
  452. }',
  453. '<?php
  454. final class MyTest extends \PHPUnit_Framework_TestCase
  455. {
  456. function testFnc()
  457. {
  458. aaa();
  459. $this?->setExpectedException("RuntimeException", "message", 123);
  460. zzz();
  461. }
  462. }'
  463. );
  464. }
  465. }