PhpUnitExpectationFixerTest.php 12 KB

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