PhpUnitExpectationFixerTest.php 12 KB

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