NoBreakCommentFixerTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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\ControlStructure;
  12. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\ControlStructure\NoBreakCommentFixer
  19. */
  20. final class NoBreakCommentFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @param string $expected
  24. * @param null|string $input
  25. *
  26. * @dataProvider provideTestFixCases
  27. */
  28. public function testFix($expected, $input = null)
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @param string $expected
  34. * @param null|string $input
  35. *
  36. * @dataProvider provideTestFixCases
  37. */
  38. public function testFixWithExplicitDefaultConfiguration($expected, $input = null)
  39. {
  40. $this->fixer->configure([
  41. 'comment_text' => 'no break',
  42. ]);
  43. $this->doTest($expected, $input);
  44. }
  45. public function provideTestFixCases()
  46. {
  47. return [
  48. [
  49. '<?php
  50. switch ($foo) {
  51. case 1:
  52. foo();
  53. // no break
  54. case 2:
  55. bar();
  56. // no break
  57. default:
  58. baz();
  59. }',
  60. '<?php
  61. switch ($foo) {
  62. case 1:
  63. foo();
  64. case 2:
  65. bar();
  66. default:
  67. baz();
  68. }',
  69. ],
  70. [
  71. '<?php
  72. switch ($foo) {
  73. case 1:
  74. foo();
  75. // no break
  76. case 2:
  77. bar();
  78. // no break
  79. default:
  80. baz();
  81. }',
  82. '<?php
  83. switch ($foo) {
  84. case 1:
  85. foo();
  86. case 2:
  87. bar();
  88. default:
  89. baz();
  90. }',
  91. ],
  92. [
  93. '<?php
  94. switch ($foo) {
  95. case 1:
  96. foo();
  97. // no break
  98. case 2:
  99. bar();
  100. // no break
  101. default:
  102. baz();
  103. }',
  104. '<?php
  105. switch ($foo) {
  106. case 1:
  107. foo(); // no break
  108. case 2:
  109. bar(); // no break
  110. default:
  111. baz();
  112. }',
  113. ],
  114. [
  115. '<?php
  116. switch ($foo) {
  117. case 1;
  118. foo();
  119. // no break
  120. case 2;
  121. bar();
  122. // no break
  123. default;
  124. baz();
  125. }',
  126. '<?php
  127. switch ($foo) {
  128. case 1;
  129. foo();
  130. case 2;
  131. bar();
  132. default;
  133. baz();
  134. }',
  135. ],
  136. [
  137. '<?php
  138. switch ($foo) {
  139. case 1:
  140. foo();
  141. // foo
  142. // no break
  143. case 2:
  144. bar();
  145. }',
  146. '<?php
  147. switch ($foo) {
  148. case 1:
  149. foo();
  150. // foo
  151. case 2:
  152. bar();
  153. }',
  154. ],
  155. [
  156. '<?php
  157. switch ($foo) { case 1: foo();
  158. // no break
  159. case 2: bar(); }',
  160. '<?php
  161. switch ($foo) { case 1: foo(); case 2: bar(); }',
  162. ],
  163. [
  164. '<?php
  165. switch ($foo) { case 1: foo();
  166. // no break
  167. case 2: bar(); }',
  168. '<?php
  169. switch ($foo) { case 1: foo();case 2: bar(); }',
  170. ],
  171. [
  172. '<?php
  173. switch ($foo) {
  174. case 1;
  175. foreach ($bar as $baz) {
  176. break;
  177. }
  178. // no break
  179. case 2;
  180. bar();
  181. }',
  182. '<?php
  183. switch ($foo) {
  184. case 1;
  185. foreach ($bar as $baz) {
  186. break;
  187. }
  188. case 2;
  189. bar();
  190. }',
  191. ],
  192. [
  193. '<?php
  194. switch ($foo) {
  195. case 1;
  196. for ($i = 0; $i < 1; ++$i) {
  197. break;
  198. }
  199. // no break
  200. case 2;
  201. bar();
  202. }',
  203. '<?php
  204. switch ($foo) {
  205. case 1;
  206. for ($i = 0; $i < 1; ++$i) {
  207. break;
  208. }
  209. case 2;
  210. bar();
  211. }',
  212. ],
  213. [
  214. '<?php
  215. switch ($foo) {
  216. case 1;
  217. foreach ($bar as $baz) {
  218. break;
  219. }
  220. // no break
  221. case 2;
  222. bar();
  223. }',
  224. '<?php
  225. switch ($foo) {
  226. case 1;
  227. foreach ($bar as $baz) {
  228. break;
  229. }
  230. case 2;
  231. bar();
  232. }',
  233. ],
  234. [
  235. '<?php
  236. switch ($foo) {
  237. case 1;
  238. if ($foo) {
  239. break;
  240. }
  241. // no break
  242. case 2;
  243. bar();
  244. }',
  245. '<?php
  246. switch ($foo) {
  247. case 1;
  248. if ($foo) {
  249. break;
  250. }
  251. case 2;
  252. bar();
  253. }',
  254. ],
  255. [
  256. '<?php
  257. switch ($foo) {
  258. case 1;
  259. do {
  260. break;
  261. } while ($bar);
  262. // no break
  263. case 2;
  264. bar();
  265. }',
  266. '<?php
  267. switch ($foo) {
  268. case 1;
  269. do {
  270. break;
  271. } while ($bar);
  272. case 2;
  273. bar();
  274. }',
  275. ],
  276. [
  277. '<?php
  278. switch ($foo) {
  279. case 1;
  280. $foo = function ($bar) {
  281. foreach ($bar as $baz) {
  282. break;
  283. }
  284. };
  285. // no break
  286. case 2;
  287. bar();
  288. }',
  289. '<?php
  290. switch ($foo) {
  291. case 1;
  292. $foo = function ($bar) {
  293. foreach ($bar as $baz) {
  294. break;
  295. }
  296. };
  297. case 2;
  298. bar();
  299. }',
  300. ],
  301. [
  302. '<?php
  303. switch ($foo) {
  304. case 1:
  305. switch ($bar) {
  306. case 1:
  307. foo();
  308. // no break
  309. case 2:
  310. bar();
  311. }
  312. break;
  313. case 2:
  314. switch ($bar) {
  315. case 1:
  316. bar();
  317. // no break
  318. case 2:
  319. foo();
  320. }
  321. }',
  322. '<?php
  323. switch ($foo) {
  324. case 1:
  325. switch ($bar) {
  326. case 1:
  327. foo();
  328. case 2:
  329. bar();
  330. }
  331. break;
  332. case 2:
  333. switch ($bar) {
  334. case 1:
  335. bar();
  336. case 2:
  337. foo();
  338. }
  339. }',
  340. ],
  341. [
  342. '<?php
  343. switch ($foo) {
  344. case 1:
  345. switch ($bar):
  346. case 1:
  347. foo();
  348. // no break
  349. case 2:
  350. bar();
  351. endswitch;
  352. break;
  353. case 2:
  354. switch ($bar):
  355. case 1:
  356. bar();
  357. // no break
  358. case 2:
  359. foo();
  360. endswitch;
  361. }',
  362. '<?php
  363. switch ($foo) {
  364. case 1:
  365. switch ($bar):
  366. case 1:
  367. foo();
  368. case 2:
  369. bar();
  370. endswitch;
  371. break;
  372. case 2:
  373. switch ($bar):
  374. case 1:
  375. bar();
  376. case 2:
  377. foo();
  378. endswitch;
  379. }',
  380. ],
  381. [
  382. '<?php
  383. switch ($foo) {
  384. case 1:
  385. foo();
  386. continue;
  387. case 2:
  388. bar();
  389. continue;
  390. default:
  391. baz();
  392. }',
  393. '<?php
  394. switch ($foo) {
  395. case 1:
  396. foo();
  397. // no break
  398. continue;
  399. case 2:
  400. bar();
  401. // no break
  402. continue;
  403. default:
  404. baz();
  405. }',
  406. ],
  407. [
  408. '<?php
  409. switch ($foo) {
  410. case 1:
  411. return foo();
  412. case 2:
  413. return bar();
  414. default:
  415. return baz();
  416. }',
  417. '<?php
  418. switch ($foo) {
  419. case 1:
  420. return foo();
  421. // no break
  422. case 2:
  423. return bar();
  424. // no break
  425. default:
  426. return baz();
  427. }',
  428. ],
  429. [
  430. '<?php
  431. switch ($foo) {
  432. case 1:
  433. return foo();
  434. case 2:
  435. return bar();
  436. default:
  437. return baz();
  438. }',
  439. '<?php
  440. switch ($foo) {
  441. case 1:
  442. // no break
  443. return foo();
  444. case 2:
  445. // no break
  446. return bar();
  447. default:
  448. return baz();
  449. }',
  450. ],
  451. [
  452. '<?php
  453. switch ($foo) {
  454. case 1:
  455. foo();
  456. break;
  457. case 2:
  458. bar();
  459. break;
  460. default:
  461. baz();
  462. }',
  463. '<?php
  464. switch ($foo) {
  465. case 1:
  466. foo();
  467. // no break
  468. break;
  469. case 2:
  470. bar();
  471. // no break
  472. break;
  473. default:
  474. baz();
  475. }',
  476. ],
  477. [
  478. '<?php
  479. switch ($foo) {
  480. case 1:
  481. foo();
  482. break;
  483. case 2:
  484. bar();
  485. break;
  486. case 3:
  487. baz();
  488. break;
  489. default:
  490. qux();
  491. }',
  492. '<?php
  493. switch ($foo) {
  494. case 1:
  495. foo();
  496. # no break
  497. break;
  498. case 2:
  499. bar();
  500. /* no break */
  501. break;
  502. case 3:
  503. baz();
  504. /** no break */
  505. break;
  506. default:
  507. qux();
  508. }',
  509. ],
  510. [
  511. '<?php
  512. switch ($foo) {
  513. case 1:
  514. case 2:
  515. bar();
  516. break;
  517. default:
  518. baz();
  519. }',
  520. '<?php
  521. switch ($foo) {
  522. case 1:
  523. // no break
  524. case 2:
  525. bar();
  526. // no break
  527. break;
  528. default:
  529. baz();
  530. }',
  531. ],
  532. [
  533. '<?php
  534. switch ($foo) {
  535. case 1:
  536. foo();
  537. }',
  538. '<?php
  539. switch ($foo) {
  540. case 1:
  541. foo();
  542. // no break
  543. }',
  544. ],
  545. [
  546. '<?php
  547. switch ($foo) {
  548. default:
  549. foo();
  550. }',
  551. '<?php
  552. switch ($foo) {
  553. default:
  554. foo();
  555. // no break
  556. }',
  557. ],
  558. [
  559. '<?php switch ($foo) { case 1: switch ($bar) { case 1: switch ($baz) { case 1: $foo = 1;
  560. // no break
  561. case 2: $foo = 2; }
  562. // no break
  563. case 2: switch ($baz) { case 1: $foo = 3;
  564. // no break
  565. case 2: $foo = 4; } }
  566. // no break
  567. case 2: switch ($bar) { case 1: switch ($baz) { case 1: $foo = 5;
  568. // no break
  569. case 2: $foo = 6; }
  570. // no break
  571. case 2: switch ($baz) { case 1: $foo = 7;
  572. // no break
  573. case 2: $foo = 8; } } }',
  574. '<?php switch ($foo) { case 1: switch ($bar) { case 1: switch ($baz) { case 1: $foo = 1; case 2: $foo = 2; } case 2: switch ($baz) { case 1: $foo = 3; case 2: $foo = 4; } } case 2: switch ($bar) { case 1: switch ($baz) { case 1: $foo = 5; case 2: $foo = 6; } case 2: switch ($baz) { case 1: $foo = 7; case 2: $foo = 8; } } }',
  575. ],
  576. [
  577. '<?php
  578. switch ($foo):
  579. case 1:
  580. foo();
  581. // no break
  582. case 2:
  583. bar();
  584. endswitch;',
  585. '<?php
  586. switch ($foo):
  587. case 1:
  588. foo();
  589. case 2:
  590. bar();
  591. // no break
  592. endswitch;',
  593. ],
  594. [
  595. '<?php
  596. switch ($foo) {
  597. case 1:
  598. ?>foo<?php
  599. // no break
  600. case 2:
  601. ?>bar<?php
  602. break;
  603. }',
  604. '<?php
  605. switch ($foo) {
  606. case 1:
  607. ?>foo<?php
  608. case 2:
  609. ?>bar<?php
  610. // no break
  611. break;
  612. }',
  613. ],
  614. [
  615. '<?php
  616. switch ($foo) {
  617. case 1:
  618. ?>foo<?php
  619. // no break
  620. case 2:
  621. ?>bar<?php
  622. break;
  623. }',
  624. '<?php
  625. switch ($foo) {
  626. case 1:
  627. ?>foo<?php
  628. case 2:
  629. ?>bar<?php
  630. // no break
  631. break;
  632. }',
  633. ],
  634. [
  635. '<?php
  636. switch ($foo) {
  637. case 1:
  638. ?>foo<?php // foo
  639. // no break
  640. case 2:
  641. ?>bar<?php // bar
  642. break;
  643. }',
  644. '<?php
  645. switch ($foo) {
  646. case 1:
  647. ?>foo<?php // foo
  648. case 2:
  649. ?>bar<?php // bar
  650. // no break
  651. break;
  652. }',
  653. ],
  654. [
  655. '<?php
  656. switch ($foo) {
  657. case 1:
  658. foo();
  659. // no break
  660. case 2:
  661. bar();
  662. // no break
  663. default:
  664. baz();
  665. }',
  666. '<?php
  667. switch ($foo) {
  668. case 1:
  669. // no break
  670. foo();
  671. case 2:
  672. // no break
  673. bar();
  674. default:
  675. baz();
  676. }',
  677. ],
  678. [
  679. '<?php
  680. switch ($foo) {
  681. case 1:
  682. die;
  683. case 2:
  684. exit;
  685. default:
  686. die;
  687. }',
  688. '<?php
  689. switch ($foo) {
  690. case 1:
  691. // no break
  692. die;
  693. case 2:
  694. // no break
  695. exit;
  696. default:
  697. die;
  698. }',
  699. ],
  700. [
  701. '<?php
  702. switch ($foo) {
  703. case 1:
  704. throw new \Exception();
  705. case 2:
  706. throw new \Exception();
  707. default:
  708. throw new \Exception();
  709. }',
  710. '<?php
  711. switch ($foo) {
  712. case 1:
  713. // no break
  714. throw new \Exception();
  715. case 2:
  716. // no break
  717. throw new \Exception();
  718. default:
  719. throw new \Exception();
  720. }',
  721. ],
  722. [
  723. '<?php
  724. switch ($foo) {
  725. case 1:
  726. goto a;
  727. case 2:
  728. goto a;
  729. default:
  730. goto a;
  731. }
  732. a:
  733. echo \'foo\';',
  734. '<?php
  735. switch ($foo) {
  736. case 1:
  737. // no break
  738. goto a;
  739. case 2:
  740. // no break
  741. goto a;
  742. default:
  743. goto a;
  744. }
  745. a:
  746. echo \'foo\';',
  747. ],
  748. [
  749. '<?php
  750. switch ($foo) {
  751. case "bar":
  752. if (1) {
  753. } else {
  754. }
  755. $aaa = new Bar();
  756. break;
  757. default:
  758. $aaa = new Baz();
  759. }',
  760. ],
  761. [
  762. '<?php
  763. switch ($foo) {
  764. case 1:
  765. ?>
  766. <?php
  767. // no break
  768. default:
  769. ?>
  770. <?php
  771. }',
  772. '<?php
  773. switch ($foo) {
  774. case 1:
  775. ?>
  776. <?php
  777. default:
  778. ?>
  779. <?php
  780. }',
  781. ],
  782. [
  783. '<?php
  784. switch ($foo) {
  785. case 1:
  786. ?>
  787. <?php
  788. // no break
  789. default:
  790. ?>
  791. <?php }',
  792. '<?php
  793. switch ($foo) {
  794. case 1:
  795. ?>
  796. <?php default:
  797. ?>
  798. <?php }',
  799. ],
  800. [
  801. '<?php
  802. switch ($foo) {
  803. case 1:
  804. foo();
  805. // no break
  806. case 2:
  807. bar();
  808. }',
  809. '<?php
  810. switch ($foo) {
  811. case 1:
  812. foo();
  813. // No break
  814. case 2:
  815. bar();
  816. }',
  817. ],
  818. ];
  819. }
  820. /**
  821. * @param string $expected
  822. * @param null|string $input
  823. *
  824. * @dataProvider provideTestFixPhp70Cases
  825. * @requires PHP 7.0
  826. */
  827. public function testFixPhp70($expected, $input = null)
  828. {
  829. $this->doTest($expected, $input);
  830. }
  831. public function provideTestFixPhp70Cases()
  832. {
  833. return [
  834. [
  835. '<?php
  836. switch ($foo) {
  837. case 1;
  838. $foo = new class {
  839. public function foo($bar)
  840. {
  841. foreach ($bar as $baz) {
  842. break;
  843. }
  844. }
  845. };
  846. // no break
  847. case 2;
  848. bar();
  849. }',
  850. '<?php
  851. switch ($foo) {
  852. case 1;
  853. $foo = new class {
  854. public function foo($bar)
  855. {
  856. foreach ($bar as $baz) {
  857. break;
  858. }
  859. }
  860. };
  861. case 2;
  862. bar();
  863. }',
  864. ],
  865. [
  866. '<?php
  867. switch ($foo) {
  868. case 1;
  869. $foo = new class(1) {
  870. public function foo($bar)
  871. {
  872. foreach ($bar as $baz) {
  873. break;
  874. }
  875. }
  876. };
  877. // no break
  878. case 2;
  879. bar();
  880. }',
  881. '<?php
  882. switch ($foo) {
  883. case 1;
  884. $foo = new class(1) {
  885. public function foo($bar)
  886. {
  887. foreach ($bar as $baz) {
  888. break;
  889. }
  890. }
  891. };
  892. case 2;
  893. bar();
  894. }',
  895. ],
  896. ];
  897. }
  898. /**
  899. * @param string $expected
  900. * @param null|string $input
  901. *
  902. * @dataProvider provideTestFixWithDifferentCommentTextCases
  903. */
  904. public function testFixWithDifferentCommentText($expected, $input = null)
  905. {
  906. $this->fixer->configure([
  907. 'comment_text' => 'fall-through case!',
  908. ]);
  909. $this->doTest($expected, $input);
  910. }
  911. public function provideTestFixWithDifferentCommentTextCases()
  912. {
  913. $cases = $this->provideTestFixCases();
  914. $replaceCommentText = static function ($php) {
  915. return strtr($php, [
  916. 'No break' => 'Fall-through case!',
  917. 'no break' => 'fall-through case!',
  918. ]);
  919. };
  920. foreach ($cases as &$case) {
  921. $case[0] = $replaceCommentText($case[0]);
  922. if (isset($case[1])) {
  923. $case[1] = $replaceCommentText($case[1]);
  924. }
  925. }
  926. $cases = array_merge($cases, [
  927. [
  928. '<?php
  929. switch ($foo) {
  930. case 1:
  931. foo();
  932. // no break
  933. // fall-through case!
  934. case 2:
  935. bar();
  936. // no break
  937. // fall-through case!
  938. default:
  939. baz();
  940. }',
  941. '<?php
  942. switch ($foo) {
  943. case 1:
  944. foo();
  945. // no break
  946. case 2:
  947. bar();
  948. // no break
  949. default:
  950. baz();
  951. }',
  952. ],
  953. ]);
  954. return $cases;
  955. }
  956. /**
  957. * @param string $expected
  958. * @param null|string $input
  959. *
  960. * @dataProvider provideTestFixWithDifferentLineEndingCases
  961. */
  962. public function testFixWithDifferentLineEnding($expected, $input = null)
  963. {
  964. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig(' ', "\r\n"));
  965. $this->doTest($expected, $input);
  966. }
  967. public function provideTestFixWithDifferentLineEndingCases()
  968. {
  969. $cases = [];
  970. foreach ($this->provideTestFixCases() as $case) {
  971. $case[0] = str_replace("\n", "\r\n", $case[0]);
  972. if (isset($case[1])) {
  973. $case[1] = str_replace("\n", "\r\n", $case[1]);
  974. }
  975. $cases[] = $case;
  976. }
  977. return $cases;
  978. }
  979. public function testFixWithCommentTextWithSpecialRegexpCharacters()
  980. {
  981. $this->fixer->configure([
  982. 'comment_text' => '~***(//[No break here.]\\\\)***~',
  983. ]);
  984. $this->doTest(
  985. '<?php
  986. switch ($foo) {
  987. case 1:
  988. foo();
  989. // ~***(//[No break here.]\\\\)***~
  990. case 2:
  991. bar();
  992. // ~***(//[No break here.]\\\\)***~
  993. default:
  994. baz();
  995. }',
  996. '<?php
  997. switch ($foo) {
  998. case 1:
  999. foo();
  1000. // ~***(//[No break here.]\\\\)***~
  1001. case 2:
  1002. bar();
  1003. default:
  1004. baz();
  1005. }'
  1006. );
  1007. }
  1008. public function testFixWithCommentTextWithTrailingSpaces()
  1009. {
  1010. $this->fixer->configure([
  1011. 'comment_text' => 'no break ',
  1012. ]);
  1013. $this->doTest(
  1014. '<?php
  1015. switch ($foo) {
  1016. case 1:
  1017. foo();
  1018. // no break
  1019. default:
  1020. baz();
  1021. }',
  1022. '<?php
  1023. switch ($foo) {
  1024. case 1:
  1025. foo();
  1026. default:
  1027. baz();
  1028. }'
  1029. );
  1030. }
  1031. /**
  1032. * @param string $text
  1033. *
  1034. * @dataProvider provideFixWithCommentTextContainingNewLinesCases
  1035. */
  1036. public function testFixWithCommentTextContainingNewLines($text)
  1037. {
  1038. $this->expectException(InvalidFixerConfigurationException::class);
  1039. $this->expectExceptionMessageRegExp('/^\[no_break_comment\] Invalid configuration: The comment text must not contain new lines\.$/');
  1040. $this->fixer->configure([
  1041. 'comment_text' => $text,
  1042. ]);
  1043. }
  1044. public function provideFixWithCommentTextContainingNewLinesCases()
  1045. {
  1046. return [
  1047. ["No\nbreak"],
  1048. ["No\r\nbreak"],
  1049. ["No\rbreak"],
  1050. ];
  1051. }
  1052. public function testConfigureWithInvalidOptions()
  1053. {
  1054. $this->expectException(InvalidFixerConfigurationException::class);
  1055. $this->expectExceptionMessageRegExp('/^\[no_break_comment\] Invalid configuration: The option "foo" does not exist\. Defined options are: "comment_text"\.$/');
  1056. $this->fixer->configure(['foo' => true]);
  1057. }
  1058. }