PhpUnitNoExpectationAnnotationFixerTest.php 19 KB

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