PhpUnitNoExpectationAnnotationFixerTest.php 18 KB

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