PhpUnitNoExpectationAnnotationFixerTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\PhpUnit;
  12. use PhpCsFixer\Fixer\PhpUnit\PhpUnitTargetVersion;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitNoExpectationAnnotationFixer
  21. */
  22. final class PhpUnitNoExpectationAnnotationFixerTest extends AbstractFixerTestCase
  23. {
  24. /**
  25. * @param string $expected
  26. * @param null|string $input
  27. *
  28. * @dataProvider provideTestFixCases
  29. */
  30. public function testFix($expected, $input = null, array $config = [])
  31. {
  32. $this->fixer->configure($config);
  33. $this->doTest($expected, $input);
  34. }
  35. public function provideTestFixCases()
  36. {
  37. return [
  38. 'empty exception message' => [
  39. '<?php
  40. final class MyTest extends \PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. */
  44. public function testFnc()
  45. {
  46. $this->setExpectedException(\FooException::class, \'\');
  47. aaa();
  48. }
  49. }',
  50. '<?php
  51. final class MyTest extends \PHPUnit_Framework_TestCase
  52. {
  53. /**
  54. * @expectedException FooException
  55. * @expectedExceptionMessage
  56. */
  57. public function testFnc()
  58. {
  59. aaa();
  60. }
  61. }',
  62. ],
  63. 'expecting exception' => [
  64. '<?php
  65. final class MyTest extends \PHPUnit_Framework_TestCase
  66. {
  67. /**
  68. */
  69. public function testFnc()
  70. {
  71. $this->setExpectedException(\FooException::class);
  72. aaa();
  73. }
  74. }',
  75. '<?php
  76. final class MyTest extends \PHPUnit_Framework_TestCase
  77. {
  78. /**
  79. * @expectedException FooException
  80. */
  81. public function testFnc()
  82. {
  83. aaa();
  84. }
  85. }',
  86. ],
  87. 'expecting rooted exception' => [
  88. '<?php
  89. final class MyTest extends \PHPUnit_Framework_TestCase
  90. {
  91. /**
  92. */
  93. public function testFnc()
  94. {
  95. $this->setExpectedException(\FooException::class);
  96. aaa();
  97. }
  98. }',
  99. '<?php
  100. final class MyTest extends \PHPUnit_Framework_TestCase
  101. {
  102. /**
  103. * @expectedException \FooException
  104. */
  105. public function testFnc()
  106. {
  107. aaa();
  108. }
  109. }',
  110. ],
  111. 'expecting exception with msg' => [
  112. '<?php
  113. final class MyTest extends \PHPUnit_Framework_TestCase
  114. {
  115. /**
  116. */
  117. public function testFnc()
  118. {
  119. $this->setExpectedException(\FooException::class, \'foo@bar\');
  120. aaa();
  121. }
  122. }',
  123. '<?php
  124. final class MyTest extends \PHPUnit_Framework_TestCase
  125. {
  126. /**
  127. * @expectedException FooException
  128. * @expectedExceptionMessage foo@bar
  129. */
  130. public function testFnc()
  131. {
  132. aaa();
  133. }
  134. }',
  135. ],
  136. 'expecting exception with code' => [
  137. '<?php
  138. final class MyTest extends \PHPUnit_Framework_TestCase
  139. {
  140. /**
  141. */
  142. public function testFnc()
  143. {
  144. $this->setExpectedException(\FooException::class, null, 123);
  145. aaa();
  146. }
  147. }',
  148. '<?php
  149. final class MyTest extends \PHPUnit_Framework_TestCase
  150. {
  151. /**
  152. * @expectedException FooException
  153. * @expectedExceptionCode 123
  154. */
  155. public function testFnc()
  156. {
  157. aaa();
  158. }
  159. }',
  160. ],
  161. 'expecting exception with msg and code' => [
  162. '<?php
  163. final class MyTest extends \PHPUnit_Framework_TestCase
  164. {
  165. /**
  166. */
  167. public function testFnc()
  168. {
  169. $this->setExpectedException(\FooException::class, \'foo\', 123);
  170. aaa();
  171. }
  172. }',
  173. '<?php
  174. final class MyTest extends \PHPUnit_Framework_TestCase
  175. {
  176. /**
  177. * @expectedException FooException
  178. * @expectedExceptionMessage foo
  179. * @expectedExceptionCode 123
  180. */
  181. public function testFnc()
  182. {
  183. aaa();
  184. }
  185. }',
  186. ],
  187. 'expecting exception with msg regex [but too low target]' => [
  188. '<?php
  189. final class MyTest extends \PHPUnit_Framework_TestCase
  190. {
  191. /**
  192. * @expectedException FooException
  193. * @expectedExceptionMessageRegExp /foo.*$/
  194. */
  195. public function testFnc()
  196. {
  197. aaa();
  198. }
  199. }',
  200. null,
  201. ['target' => PhpUnitTargetVersion::VERSION_3_2],
  202. ],
  203. 'expecting exception with msg regex' => [
  204. '<?php
  205. final class MyTest extends \PHPUnit_Framework_TestCase
  206. {
  207. /**
  208. */
  209. public function testFnc()
  210. {
  211. $this->setExpectedExceptionRegExp(\FooException::class, \'/foo.*$/\');
  212. aaa();
  213. }
  214. }',
  215. '<?php
  216. final class MyTest extends \PHPUnit_Framework_TestCase
  217. {
  218. /**
  219. * @expectedException FooException
  220. * @expectedExceptionMessageRegExp /foo.*$/
  221. */
  222. public function testFnc()
  223. {
  224. aaa();
  225. }
  226. }',
  227. ['target' => PhpUnitTargetVersion::VERSION_4_3],
  228. ],
  229. 'use_class_const=false' => [
  230. '<?php
  231. final class MyTest extends \PHPUnit_Framework_TestCase
  232. {
  233. /**
  234. */
  235. public function testFnc()
  236. {
  237. $this->setExpectedException(\'FooException\');
  238. aaa();
  239. }
  240. }',
  241. '<?php
  242. final class MyTest extends \PHPUnit_Framework_TestCase
  243. {
  244. /**
  245. * @expectedException FooException
  246. */
  247. public function testFnc()
  248. {
  249. aaa();
  250. }
  251. }',
  252. ['use_class_const' => false],
  253. ],
  254. 'keep rest of docblock' => [
  255. '<?php
  256. final class MyTest extends \PHPUnit_Framework_TestCase
  257. {
  258. /**
  259. * Summary.
  260. *
  261. * @param int $param
  262. * @return void
  263. */
  264. public function testFnc($param)
  265. {
  266. $this->setExpectedException(\FooException::class);
  267. aaa();
  268. }
  269. }',
  270. '<?php
  271. final class MyTest extends \PHPUnit_Framework_TestCase
  272. {
  273. /**
  274. * Summary.
  275. *
  276. * @param int $param
  277. * @expectedException FooException
  278. * @return void
  279. */
  280. public function testFnc($param)
  281. {
  282. aaa();
  283. }
  284. }',
  285. ],
  286. 'fix method without visibility' => [
  287. '<?php
  288. final class MyTest extends \PHPUnit_Framework_TestCase
  289. {
  290. /**
  291. */
  292. function testFnc($param)
  293. {
  294. $this->setExpectedException(\FooException::class);
  295. aaa();
  296. }
  297. }',
  298. '<?php
  299. final class MyTest extends \PHPUnit_Framework_TestCase
  300. {
  301. /**
  302. * @expectedException FooException
  303. */
  304. function testFnc($param)
  305. {
  306. aaa();
  307. }
  308. }',
  309. ],
  310. 'fix final method' => [
  311. '<?php
  312. final class MyTest extends \PHPUnit_Framework_TestCase
  313. {
  314. /**
  315. */
  316. final function testFnc($param)
  317. {
  318. $this->setExpectedException(\FooException::class);
  319. aaa();
  320. }
  321. }',
  322. '<?php
  323. final class MyTest extends \PHPUnit_Framework_TestCase
  324. {
  325. /**
  326. * @expectedException FooException
  327. */
  328. final function testFnc($param)
  329. {
  330. aaa();
  331. }
  332. }',
  333. ],
  334. 'ignore when no docblock' => [
  335. '<?php
  336. final class MyTest extends \PHPUnit_Framework_TestCase
  337. {
  338. final function testFoo($param)
  339. {
  340. aaa();
  341. }
  342. /**
  343. */
  344. final function testFnc($param)
  345. {
  346. $this->setExpectedException(\FooException::class);
  347. aaa();
  348. }
  349. }',
  350. '<?php
  351. final class MyTest extends \PHPUnit_Framework_TestCase
  352. {
  353. final function testFoo($param)
  354. {
  355. aaa();
  356. }
  357. /**
  358. * @expectedException FooException
  359. */
  360. final function testFnc($param)
  361. {
  362. aaa();
  363. }
  364. }',
  365. ],
  366. 'valid docblock but for property, not method' => [
  367. '<?php
  368. final class MyTest extends \PHPUnit_Framework_TestCase
  369. {
  370. /**
  371. * @expectedException FooException
  372. * @expectedExceptionCode 123
  373. */
  374. public $foo;
  375. public function bar()
  376. {
  377. /**
  378. * @expectedException FooException
  379. * @expectedExceptionCode 123
  380. */
  381. $baz = 1;
  382. /**
  383. * @expectedException FooException
  384. * @expectedExceptionCode 123
  385. */
  386. while (false) {}
  387. }
  388. }',
  389. ],
  390. 'respect \' and " in expected msg' => [
  391. '<?php
  392. final class MyTest extends \PHPUnit_Framework_TestCase
  393. {
  394. /**
  395. * Summary.
  396. *
  397. */
  398. public function testFnc($param)
  399. {
  400. $this->setExpectedException(\FooException::class, \'Foo \\\' bar " baz\');
  401. aaa();
  402. }
  403. }',
  404. '<?php
  405. final class MyTest extends \PHPUnit_Framework_TestCase
  406. {
  407. /**
  408. * Summary.
  409. *
  410. * @expectedException FooException
  411. * @expectedExceptionMessage Foo \' bar " baz
  412. */
  413. public function testFnc($param)
  414. {
  415. aaa();
  416. }
  417. }',
  418. ],
  419. 'special \\ handling' => [
  420. <<<'EOT'
  421. <?php
  422. final class MyTest extends \PHPUnit_Framework_TestCase
  423. {
  424. /**
  425. */
  426. public function testElementNonExistentOne()
  427. {
  428. $this->setExpectedException(\Cake\View\Exception\MissingElementException::class, 'A backslash at the end \\');
  429. $this->View->element('non_existent_element');
  430. }
  431. /**
  432. */
  433. public function testElementNonExistentTwo()
  434. {
  435. $this->setExpectedExceptionRegExp(\Cake\View\Exception\MissingElementException::class, '#^Element file "Element[\\\\/]non_existent_element\\.ctp" is missing\\.$#');
  436. $this->View->element('non_existent_element');
  437. }
  438. }
  439. EOT
  440. ,
  441. <<<'EOT'
  442. <?php
  443. final class MyTest extends \PHPUnit_Framework_TestCase
  444. {
  445. /**
  446. * @expectedException \Cake\View\Exception\MissingElementException
  447. * @expectedExceptionMessage A backslash at the end \
  448. */
  449. public function testElementNonExistentOne()
  450. {
  451. $this->View->element('non_existent_element');
  452. }
  453. /**
  454. * @expectedException \Cake\View\Exception\MissingElementException
  455. * @expectedExceptionMessageRegExp #^Element file "Element[\\/]non_existent_element\.ctp" is missing\.$#
  456. */
  457. public function testElementNonExistentTwo()
  458. {
  459. $this->View->element('non_existent_element');
  460. }
  461. }
  462. EOT
  463. ,
  464. ],
  465. 'message on newline' => [
  466. <<<'EOT'
  467. <?php
  468. final class MyTest extends \PHPUnit_Framework_TestCase
  469. {
  470. /**
  471. */
  472. public function testMessageOnMultilines()
  473. {
  474. $this->setExpectedException(\RuntimeException::class, 'Message on multilines AAA €');
  475. aaa();
  476. }
  477. /**
  478. * @foo
  479. */
  480. public function testMessageOnMultilinesWithAnotherTag()
  481. {
  482. $this->setExpectedException(\RuntimeException::class, 'Message on multilines BBB è');
  483. bbb();
  484. }
  485. /**
  486. *
  487. * @foo
  488. */
  489. public function testMessageOnMultilinesWithAnotherSpaceAndTag()
  490. {
  491. $this->setExpectedException(\RuntimeException::class, 'Message on multilines CCC ✔');
  492. ccc();
  493. }
  494. }
  495. EOT
  496. ,
  497. <<<'EOT'
  498. <?php
  499. final class MyTest extends \PHPUnit_Framework_TestCase
  500. {
  501. /**
  502. * @expectedException \RuntimeException
  503. * @expectedExceptionMessage Message
  504. * on
  505. * multilines AAA
  506. * €
  507. */
  508. public function testMessageOnMultilines()
  509. {
  510. aaa();
  511. }
  512. /**
  513. * @expectedException \RuntimeException
  514. * @expectedExceptionMessage Message
  515. * on
  516. * multilines BBB
  517. * è
  518. * @foo
  519. */
  520. public function testMessageOnMultilinesWithAnotherTag()
  521. {
  522. bbb();
  523. }
  524. /**
  525. * @expectedException \RuntimeException
  526. * @expectedExceptionMessage Message
  527. * on
  528. * multilines CCC
  529. * ✔
  530. *
  531. * @foo
  532. */
  533. public function testMessageOnMultilinesWithAnotherSpaceAndTag()
  534. {
  535. ccc();
  536. }
  537. }
  538. EOT
  539. ,
  540. ],
  541. 'annotation with double @' => [
  542. '<?php
  543. final class MyTest extends \PHPUnit_Framework_TestCase
  544. {
  545. /**
  546. * Double "@" is/was below
  547. */
  548. public function testFnc()
  549. {
  550. $this->setExpectedException(\FooException::class);
  551. aaa();
  552. }
  553. }',
  554. '<?php
  555. final class MyTest extends \PHPUnit_Framework_TestCase
  556. {
  557. /**
  558. * Double "@" is/was below
  559. * @@expectedException FooException
  560. */
  561. public function testFnc()
  562. {
  563. aaa();
  564. }
  565. }',
  566. ],
  567. 'annotation with text before @' => [
  568. '<?php
  569. final class MyTest extends \PHPUnit_Framework_TestCase
  570. {
  571. /**
  572. * We are providing invalid input, for that we @expectedException FooException
  573. */
  574. public function testFnc()
  575. {
  576. aaa();
  577. }
  578. }',
  579. ],
  580. [
  581. '<?php
  582. abstract class MyTest extends \PHPUnit_Framework_TestCase
  583. {
  584. /**
  585. * @expectedException FooException
  586. * @expectedExceptionMessage
  587. */
  588. abstract public function testFnc();
  589. }',
  590. ],
  591. 'expecting exception in single line comment' => [
  592. '<?php
  593. final class MyTest extends \PHPUnit_Framework_TestCase
  594. {
  595. /** */
  596. public function testFnc()
  597. {
  598. $this->setExpectedException(\FooException::class);
  599. aaa();
  600. }
  601. }',
  602. '<?php
  603. final class MyTest extends \PHPUnit_Framework_TestCase
  604. {
  605. /** @expectedException FooException */
  606. public function testFnc()
  607. {
  608. aaa();
  609. }
  610. }',
  611. ],
  612. ];
  613. }
  614. /**
  615. * @param string $expected
  616. * @param null|string $input
  617. *
  618. * @dataProvider provideMessyWhitespacesCases
  619. */
  620. public function testMessyWhitespaces($expected, $input = null)
  621. {
  622. $expected = str_replace([' ', "\n"], ["\t", "\r\n"], $expected);
  623. if (null !== $input) {
  624. $input = str_replace([' ', "\n"], ["\t", "\r\n"], $input);
  625. }
  626. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  627. $this->doTest($expected, $input);
  628. }
  629. public function provideMessyWhitespacesCases()
  630. {
  631. return [
  632. [
  633. '<?php
  634. final class MyTest extends \PHPUnit_Framework_TestCase
  635. {
  636. /**
  637. */
  638. public function testFnc()
  639. {
  640. $this->setExpectedException(\FooException::class, \'foo\', 123);
  641. aaa();
  642. }
  643. }',
  644. '<?php
  645. final class MyTest extends \PHPUnit_Framework_TestCase
  646. {
  647. /**
  648. * @expectedException FooException
  649. * @expectedExceptionMessage foo
  650. * @expectedExceptionCode 123
  651. */
  652. public function testFnc()
  653. {
  654. aaa();
  655. }
  656. }',
  657. ],
  658. [
  659. '<?php
  660. final class MyTest extends \PHPUnit_Framework_TestCase
  661. {
  662. /**
  663. */
  664. public function testFnc()
  665. {
  666. $this->setExpectedException(\FooException::class, \'foo\', 123);
  667. aaa();
  668. }
  669. }',
  670. '<?php
  671. final class MyTest extends \PHPUnit_Framework_TestCase
  672. {
  673. /**
  674. * @expectedException FooException
  675. * @expectedExceptionMessage foo
  676. * @expectedExceptionCode 123
  677. */
  678. public function testFnc()
  679. {
  680. aaa();
  681. }
  682. }',
  683. ],
  684. ];
  685. }
  686. }