PhpdocNoAliasTagFixerTest.php 8.1 KB

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