PhpdocLineSpanFixerTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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\Phpdoc;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author Gert de Pagter <BackEndTea@gmail.com>
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocLineSpanFixer
  19. */
  20. final class PhpdocLineSpanFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @param string $expected
  24. * @param null|string $input
  25. * @param array $config
  26. *
  27. * @dataProvider provideFixCases
  28. */
  29. public function testFix($expected, $input = null, array $config = [])
  30. {
  31. $this->fixer->configure($config);
  32. $this->doTest($expected, $input);
  33. }
  34. /**
  35. * @return array
  36. */
  37. public function provideFixCases()
  38. {
  39. return [
  40. 'It does not change doc blocks if not needed' => [
  41. '<?php
  42. class Foo
  43. {
  44. /**
  45. * Important
  46. */
  47. const FOO_BAR = "foobar";
  48. /**
  49. * @var bool
  50. */
  51. public $variable = true;
  52. /**
  53. * @var bool
  54. */
  55. private $var = false;
  56. /**
  57. * @return void
  58. */
  59. public function hello() {}
  60. }
  61. ',
  62. ],
  63. 'It does change doc blocks to multi by default' => [
  64. '<?php
  65. class Foo
  66. {
  67. /**
  68. * Important
  69. */
  70. const FOO_BAR = "foobar";
  71. /**
  72. * @var bool
  73. */
  74. public $variable = true;
  75. /**
  76. * @var bool
  77. */
  78. private $var = false;
  79. /**
  80. * @return void
  81. */
  82. public function hello() {}
  83. }
  84. ',
  85. '<?php
  86. class Foo
  87. {
  88. /** Important */
  89. const FOO_BAR = "foobar";
  90. /** @var bool */
  91. public $variable = true;
  92. /** @var bool */
  93. private $var = false;
  94. /** @return void */
  95. public function hello() {}
  96. }
  97. ',
  98. ],
  99. 'It does change doc blocks to single if configured to do so' => [
  100. '<?php
  101. class Foo
  102. {
  103. /** Important */
  104. const FOO_BAR = "foobar";
  105. /** @var bool */
  106. public $variable = true;
  107. /** @var bool */
  108. private $var = false;
  109. /** @return void */
  110. public function hello() {}
  111. }
  112. ',
  113. '<?php
  114. class Foo
  115. {
  116. /**
  117. * Important
  118. */
  119. const FOO_BAR = "foobar";
  120. /**
  121. * @var bool
  122. */
  123. public $variable = true;
  124. /**
  125. * @var bool
  126. */
  127. private $var = false;
  128. /**
  129. * @return void
  130. */
  131. public function hello() {}
  132. }
  133. ',
  134. [
  135. 'property' => 'single',
  136. 'const' => 'single',
  137. 'method' => 'single',
  138. ],
  139. ],
  140. 'It does not changes doc blocks from single if configured to do so' => [
  141. '<?php
  142. class Foo
  143. {
  144. /** Important */
  145. const FOO_BAR = "foobar";
  146. /** @var bool */
  147. public $variable = true;
  148. /** @var bool */
  149. private $var = false;
  150. /** @return void */
  151. public function hello() {}
  152. }
  153. ',
  154. null,
  155. [
  156. 'property' => 'single',
  157. 'const' => 'single',
  158. 'method' => 'single',
  159. ],
  160. ],
  161. 'It can be configured to change certain elements to single line' => [
  162. '<?php
  163. class Foo
  164. {
  165. /**
  166. * Important
  167. */
  168. const FOO_BAR = "foobar";
  169. /** @var bool */
  170. public $variable = true;
  171. /** @var bool */
  172. private $var = false;
  173. /**
  174. * @return void
  175. */
  176. public function hello() {}
  177. }
  178. ',
  179. '<?php
  180. class Foo
  181. {
  182. /**
  183. * Important
  184. */
  185. const FOO_BAR = "foobar";
  186. /**
  187. * @var bool
  188. */
  189. public $variable = true;
  190. /**
  191. * @var bool
  192. */
  193. private $var = false;
  194. /**
  195. * @return void
  196. */
  197. public function hello() {}
  198. }
  199. ',
  200. [
  201. 'property' => 'single',
  202. ],
  203. ],
  204. 'It wont change a doc block to single line if it has multiple useful lines' => [
  205. '<?php
  206. class Foo
  207. {
  208. /**
  209. * Important
  210. * Really important
  211. */
  212. const FOO_BAR = "foobar";
  213. }
  214. ',
  215. null,
  216. [
  217. 'const' => 'single',
  218. ],
  219. ],
  220. 'It updates doc blocks correctly, even with more indentation' => [
  221. '<?php
  222. if (false) {
  223. class Foo
  224. {
  225. /** @var bool */
  226. public $var = true;
  227. /**
  228. * @return void
  229. */
  230. public function hello () {}
  231. }
  232. }
  233. ',
  234. '<?php
  235. if (false) {
  236. class Foo
  237. {
  238. /**
  239. * @var bool
  240. */
  241. public $var = true;
  242. /** @return void */
  243. public function hello () {}
  244. }
  245. }
  246. ',
  247. [
  248. 'property' => 'single',
  249. ],
  250. ],
  251. 'It can convert empty doc blocks' => [
  252. '<?php
  253. class Foo
  254. {
  255. /**
  256. *
  257. */
  258. const FOO = "foobar";
  259. /** */
  260. private $foo;
  261. }',
  262. '<?php
  263. class Foo
  264. {
  265. /** */
  266. const FOO = "foobar";
  267. /**
  268. *
  269. */
  270. private $foo;
  271. }',
  272. [
  273. 'property' => 'single',
  274. ],
  275. ],
  276. 'It can update doc blocks of static properties' => [
  277. '<?php
  278. class Bar
  279. {
  280. /**
  281. * Important
  282. */
  283. public static $variable = "acme";
  284. }
  285. ',
  286. '<?php
  287. class Bar
  288. {
  289. /** Important */
  290. public static $variable = "acme";
  291. }
  292. ',
  293. ],
  294. 'It can update doc blocks of properties that use the var keyword instead of public' => [
  295. '<?php
  296. class Bar
  297. {
  298. /**
  299. * Important
  300. */
  301. var $variable = "acme";
  302. }
  303. ',
  304. '<?php
  305. class Bar
  306. {
  307. /** Important */
  308. var $variable = "acme";
  309. }
  310. ',
  311. ],
  312. 'It can update doc blocks of static that do not declare visibility' => [
  313. '<?php
  314. class Bar
  315. {
  316. /**
  317. * Important
  318. */
  319. static $variable = "acme";
  320. }
  321. ',
  322. '<?php
  323. class Bar
  324. {
  325. /** Important */
  326. static $variable = "acme";
  327. }
  328. ',
  329. ],
  330. ];
  331. }
  332. /**
  333. * @requires PHP 7.1
  334. * @dataProvider provideFix71Cases
  335. *
  336. * @param string $expected
  337. * @param string $input
  338. * @param array $config
  339. */
  340. public function testFix71($expected, $input = null, array $config = [])
  341. {
  342. $this->fixer->configure($config);
  343. $this->doTest($expected, $input);
  344. }
  345. public function provideFix71Cases()
  346. {
  347. return [
  348. 'It can handle constants with visibility' => [
  349. '<?php
  350. class Foo
  351. {
  352. /**
  353. *
  354. */
  355. public const FOO = "foobar";
  356. /** */
  357. private $foo;
  358. }',
  359. '<?php
  360. class Foo
  361. {
  362. /** */
  363. public const FOO = "foobar";
  364. /**
  365. *
  366. */
  367. private $foo;
  368. }',
  369. [
  370. 'property' => 'single',
  371. ],
  372. ],
  373. ];
  374. }
  375. }