PhpdocNoAliasTagFixerTest.php 8.0 KB

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