SingleLineThrowFixerTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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\FunctionNotation;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @internal
  15. *
  16. * @covers \PhpCsFixer\Fixer\FunctionNotation\SingleLineThrowFixer
  17. */
  18. final class SingleLineThrowFixerTest extends AbstractFixerTestCase
  19. {
  20. /**
  21. * @param string $expected
  22. * @param null|string $input
  23. *
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix($expected, $input = null)
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. public function provideFixCases()
  31. {
  32. yield ['<?php throw new Exception; foo(
  33. "Foo"
  34. );'];
  35. yield ['<?php throw new $exceptionName; foo(
  36. "Foo"
  37. );'];
  38. yield ['<?php throw $exception; foo(
  39. "Foo"
  40. );'];
  41. yield ['<?php throw new Exception("Foo", 0);'];
  42. yield [
  43. '<?php throw new Exception("Foo", 0);',
  44. '<?php throw new Exception(
  45. "Foo",
  46. 0
  47. );',
  48. ];
  49. yield [
  50. '<?php throw new Exception("Foo" . "Bar");',
  51. '<?php throw new Exception(
  52. "Foo"
  53. .
  54. "Bar"
  55. );',
  56. ];
  57. yield [
  58. '<?php throw new Exception(new ExceptionReport("Foo"), 0);',
  59. '<?php throw new Exception(
  60. new
  61. ExceptionReport("Foo"),
  62. 0
  63. );',
  64. ];
  65. yield [
  66. '<?php throw new Exception(sprintf("Error with number %s", 42));',
  67. '<?php throw new Exception(sprintf(
  68. "Error with number %s",
  69. 42
  70. ));',
  71. ];
  72. yield [
  73. '<?php throw new SomeVendor\\Exception("Foo");',
  74. '<?php throw new SomeVendor\\Exception(
  75. "Foo"
  76. );',
  77. ];
  78. yield [
  79. '<?php throw new \SomeVendor\\Exception("Foo");',
  80. '<?php throw new \SomeVendor\\Exception(
  81. "Foo"
  82. );',
  83. ];
  84. yield [
  85. '<?php throw $this->exceptionFactory->createAnException("Foo");',
  86. '<?php throw $this
  87. ->exceptionFactory
  88. ->createAnException(
  89. "Foo"
  90. );',
  91. ];
  92. yield [
  93. '<?php throw $this->getExceptionFactory()->createAnException("Foo");',
  94. '<?php throw $this
  95. ->getExceptionFactory()
  96. ->createAnException(
  97. "Foo"
  98. );',
  99. ];
  100. yield [
  101. '<?php throw $this->getExceptionFactory()->createAnException(function ($x, $y) { return $x === $y + 2; });',
  102. '<?php throw $this
  103. ->getExceptionFactory()
  104. ->createAnException(
  105. function
  106. (
  107. $x,
  108. $y
  109. )
  110. {
  111. return $x === $y + 2
  112. ;
  113. }
  114. );',
  115. ];
  116. yield [
  117. '<?php throw ExceptionFactory::createAnException("Foo");',
  118. '<?php throw ExceptionFactory
  119. ::
  120. createAnException(
  121. "Foo"
  122. );',
  123. ];
  124. yield [
  125. '<?php throw new Exception("Foo", 0);',
  126. '<?php throw
  127. new
  128. Exception
  129. (
  130. "Foo"
  131. ,
  132. 0
  133. );',
  134. ];
  135. yield [
  136. '<?php throw new $exceptionName("Foo");',
  137. '<?php throw new $exceptionName(
  138. "Foo"
  139. );',
  140. ];
  141. yield [
  142. '<?php throw new $exceptions[4];',
  143. '<?php throw new $exceptions[
  144. 4
  145. ];',
  146. ];
  147. yield [
  148. '<?php throw clone $exceptionName("Foo");',
  149. '<?php throw clone $exceptionName(
  150. "Foo"
  151. );',
  152. ];
  153. yield [
  154. '<?php throw new WeirdException("Foo", -20, "An elephant", 1, 2, 3, 4, 5, 6, 7, 8);',
  155. '<?php throw new WeirdException("Foo", -20, "An elephant",
  156. 1,
  157. 2,
  158. 3, 4, 5, 6, 7, 8
  159. );',
  160. ];
  161. yield [
  162. '<?php
  163. if ($foo) {
  164. throw new Exception("It is foo", 1);
  165. } else {
  166. throw new \Exception("It is not foo", 0);
  167. }
  168. ',
  169. '<?php
  170. if ($foo) {
  171. throw new Exception(
  172. "It is foo",
  173. 1
  174. );
  175. } else {
  176. throw new \Exception(
  177. "It is not foo", 0
  178. );
  179. }
  180. ',
  181. ];
  182. yield [
  183. '<?php throw new Exception( /* 0 */"Foo", /* 1 */0 /* 2 */); //3',
  184. '<?php throw new Exception( // 0
  185. "Foo", // 1
  186. 0 // 2
  187. ); //3',
  188. ];
  189. yield [
  190. '<?php throw new Exception( 0, /* 1 2 3 */ /*4*/ "Foo", /*5*/ /*6*/0 /*7*/);',
  191. '<?php throw new Exception( 0, /*
  192. 1
  193. 2
  194. 3
  195. */
  196. /*4*/ "Foo", /*5*/
  197. /*6*/0 /*7*/);',
  198. ];
  199. yield [
  200. '<?php throw new Exception( /* 0 */${"Foo" /* a */}, /*b */fnx/*c */(/*d */0/*e */)/*f */);',
  201. '<?php throw new Exception( // 0
  202. ${"Foo"
  203. # a
  204. }, #b
  205. fnx#c
  206. (#d
  207. 0#e
  208. )#f
  209. );',
  210. ];
  211. yield [
  212. "<?php throw new Exception('a'. 1);",
  213. "<?php throw new Exception('a'.
  214. 1
  215. );",
  216. ];
  217. }
  218. }