PhpdocNoAliasTagFixerTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 Graham Campbell <graham@alt-three.com>
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. * @author SpacePossum
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocNoAliasTagFixer
  21. */
  22. final class PhpdocNoAliasTagFixerTest extends AbstractFixerTestCase
  23. {
  24. public function testInvalidConfigCase1()
  25. {
  26. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  27. $this->expectExceptionMessageRegExp('#^\[phpdoc_no_alias_tag\] Invalid configuration: Tag to replace must be a string\.$#');
  28. $this->fixer->configure(['replacements' => [1 => 'abc']]);
  29. }
  30. public function testInvalidConfigCase2()
  31. {
  32. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  33. $this->expectExceptionMessageRegExp('#^\[phpdoc_no_alias_tag\] Invalid configuration: Tag to replace to from "a" must be a string\.$#');
  34. $this->fixer->configure(['replacements' => ['a' => null]]);
  35. }
  36. public function testInvalidConfigCase3()
  37. {
  38. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  39. $this->expectExceptionMessageRegExp('#^\[phpdoc_no_alias_tag\] Invalid configuration: Tag "see" cannot be replaced by invalid tag "link\*\/"\.$#');
  40. $this->fixer->configure(['replacements' => ['see' => 'link*/']]);
  41. }
  42. public function testInvalidConfigCase4()
  43. {
  44. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  45. $this->expectExceptionMessageRegExp('#^\[phpdoc_no_alias_tag\] Invalid configuration: Cannot change tag "link" to tag "see", as the tag "see" is configured to be replaced to "link"\.$#');
  46. $this->fixer->configure(['replacements' => [
  47. 'link' => 'see',
  48. 'a' => 'b',
  49. 'see' => 'link',
  50. ]]);
  51. }
  52. public function testInvalidConfigCase5()
  53. {
  54. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  55. $this->expectExceptionMessageRegExp('#^\[phpdoc_no_alias_tag\] Invalid configuration: Cannot change tag "b" to tag "see", as the tag "see" is configured to be replaced to "link"\.$#');
  56. $this->fixer->configure(['replacements' => [
  57. 'b' => 'see',
  58. 'see' => 'link',
  59. 'link' => 'b',
  60. ]]);
  61. }
  62. public function testInvalidConfigCase6()
  63. {
  64. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  65. $this->expectExceptionMessageRegExp('#^\[phpdoc_no_alias_tag\] Invalid configuration: Cannot change tag "see" to tag "link", as the tag "link" is configured to be replaced to "b"\.$#');
  66. $this->fixer->configure(['replacements' => [
  67. 'see' => 'link',
  68. 'link' => 'b',
  69. ]]);
  70. }
  71. /**
  72. * @param string $expected
  73. * @param null|string $input
  74. *
  75. * @group legacy
  76. * @dataProvider providePropertyCases
  77. * @expectedDeprecation Passing "replacements" at the root of the configuration for rule "phpdoc_no_alias_tag" is deprecated and will not be supported in 3.0, use "replacements" => array(...) option instead.
  78. */
  79. public function testLegacyPropertyFix($expected, $input = null)
  80. {
  81. $this->fixer->configure([
  82. 'property-read' => 'property',
  83. 'property-write' => 'property',
  84. ]);
  85. $this->doTest($expected, $input);
  86. }
  87. /**
  88. * @param string $expected
  89. * @param null|string $input
  90. *
  91. * @dataProvider providePropertyCases
  92. */
  93. public function testPropertyFix($expected, $input = null)
  94. {
  95. $this->fixer->configure(['replacements' => [
  96. 'property-read' => 'property',
  97. 'property-write' => 'property',
  98. ]]);
  99. $this->doTest($expected, $input);
  100. }
  101. public function providePropertyCases()
  102. {
  103. return [
  104. [
  105. '<?php
  106. /**
  107. *
  108. */',
  109. ],
  110. [
  111. '<?php
  112. /**
  113. * @property string $foo
  114. */',
  115. '<?php
  116. /**
  117. * @property-read string $foo
  118. */',
  119. ],
  120. [
  121. '<?php /** @property mixed $bar */',
  122. '<?php /** @property-write mixed $bar */',
  123. ],
  124. ];
  125. }
  126. /**
  127. * @param string $expected
  128. * @param null|string $input
  129. *
  130. * @dataProvider provideTypeToVarCases
  131. */
  132. public function testTypeToVarFix($expected, $input = null)
  133. {
  134. $this->fixer->configure(['replacements' => [
  135. 'type' => 'var',
  136. ]]);
  137. $this->doTest($expected, $input);
  138. }
  139. public function provideTypeToVarCases()
  140. {
  141. return [
  142. [
  143. '<?php
  144. /**
  145. *
  146. */',
  147. ],
  148. [
  149. '<?php
  150. /**
  151. * @var string Hello!
  152. */',
  153. '<?php
  154. /**
  155. * @type string Hello!
  156. */',
  157. ],
  158. [
  159. '<?php /** @var string Hello! */',
  160. '<?php /** @type string Hello! */',
  161. ],
  162. [
  163. '<?php
  164. /**
  165. * Initializes this class with the given options.
  166. *
  167. * @param array $options {
  168. * @var bool $required Whether this element is required
  169. * @var string $label The display name for this element
  170. * }
  171. */',
  172. '<?php
  173. /**
  174. * Initializes this class with the given options.
  175. *
  176. * @param array $options {
  177. * @type bool $required Whether this element is required
  178. * @type string $label The display name for this element
  179. * }
  180. */',
  181. ],
  182. ];
  183. }
  184. /**
  185. * @param string $expected
  186. * @param null|string $input
  187. *
  188. * @dataProvider provideVarToTypeCases
  189. */
  190. public function testVarToTypeFix($expected, $input = null)
  191. {
  192. $this->fixer->configure(['replacements' => [
  193. 'var' => 'type',
  194. ]]);
  195. $this->doTest($expected, $input);
  196. }
  197. public function provideVarToTypeCases()
  198. {
  199. return [
  200. [
  201. '<?php
  202. /**
  203. *
  204. */',
  205. ],
  206. [
  207. '<?php
  208. /**
  209. * @type string Hello!
  210. */',
  211. '<?php
  212. /**
  213. * @var string Hello!
  214. */',
  215. ],
  216. [
  217. '<?php /** @type string Hello! */',
  218. '<?php /** @var string Hello! */',
  219. ],
  220. [
  221. '<?php
  222. /**
  223. * Initializes this class with the given options.
  224. *
  225. * @param array $options {
  226. * @type bool $required Whether this element is required
  227. * @type string $label The display name for this element
  228. * }
  229. */',
  230. '<?php
  231. /**
  232. * Initializes this class with the given options.
  233. *
  234. * @param array $options {
  235. * @var bool $required Whether this element is required
  236. * @var string $label The display name for this element
  237. * }
  238. */',
  239. ],
  240. ];
  241. }
  242. public function testLinkToSee()
  243. {
  244. $this->fixer->configure(['replacements' => [
  245. 'link' => 'see',
  246. ]]);
  247. $this->doTest(
  248. '<?php /** @see https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#710-link-deprecated */',
  249. '<?php /** @link https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#710-link-deprecated */'
  250. );
  251. }
  252. }