PhpdocNoAliasTagFixerTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. final class PhpdocNoAliasTagFixerTest extends AbstractFixerTestCase
  24. {
  25. public function testInvalidConfigCase1(): void
  26. {
  27. $this->expectException(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(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(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(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(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(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 providePropertyFixCases
  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 static function providePropertyFixCases(): iterable
  84. {
  85. yield [
  86. '<?php
  87. /**
  88. *
  89. */',
  90. ];
  91. yield [
  92. '<?php
  93. /**
  94. * @property string $foo
  95. */',
  96. '<?php
  97. /**
  98. * @property-read string $foo
  99. */',
  100. ];
  101. yield [
  102. '<?php /** @property mixed $bar */',
  103. '<?php /** @property-write mixed $bar */',
  104. ];
  105. }
  106. /**
  107. * @dataProvider provideTypeToVarFixCases
  108. */
  109. public function testTypeToVarFix(string $expected, ?string $input = null): void
  110. {
  111. $this->fixer->configure(['replacements' => [
  112. 'type' => 'var',
  113. ]]);
  114. $this->doTest($expected, $input);
  115. }
  116. public static function provideTypeToVarFixCases(): iterable
  117. {
  118. yield [
  119. '<?php
  120. /**
  121. *
  122. */',
  123. ];
  124. yield [
  125. '<?php
  126. /**
  127. * @var string Hello!
  128. */',
  129. '<?php
  130. /**
  131. * @type string Hello!
  132. */',
  133. ];
  134. yield [
  135. '<?php /** @var string Hello! */',
  136. '<?php /** @type string Hello! */',
  137. ];
  138. yield [
  139. '<?php
  140. /**
  141. * Initializes this class with the given options.
  142. *
  143. * @param array $options {
  144. * @var bool $required Whether this element is required
  145. * @var string $label The display name for this element
  146. * }
  147. */',
  148. '<?php
  149. /**
  150. * Initializes this class with the given options.
  151. *
  152. * @param array $options {
  153. * @type bool $required Whether this element is required
  154. * @type string $label The display name for this element
  155. * }
  156. */',
  157. ];
  158. }
  159. /**
  160. * @dataProvider provideVarToTypeFixCases
  161. */
  162. public function testVarToTypeFix(string $expected, ?string $input = null): void
  163. {
  164. $this->fixer->configure(['replacements' => [
  165. 'var' => 'type',
  166. ]]);
  167. $this->doTest($expected, $input);
  168. }
  169. public static function provideVarToTypeFixCases(): iterable
  170. {
  171. yield [
  172. '<?php
  173. /**
  174. *
  175. */',
  176. ];
  177. yield [
  178. '<?php
  179. /**
  180. * @type string Hello!
  181. */',
  182. '<?php
  183. /**
  184. * @var string Hello!
  185. */',
  186. ];
  187. yield [
  188. '<?php /** @type string Hello! */',
  189. '<?php /** @var string Hello! */',
  190. ];
  191. yield [
  192. '<?php
  193. /**
  194. * Initializes this class with the given options.
  195. *
  196. * @param array $options {
  197. * @type bool $required Whether this element is required
  198. * @type string $label The display name for this element
  199. * }
  200. */',
  201. '<?php
  202. /**
  203. * Initializes this class with the given options.
  204. *
  205. * @param array $options {
  206. * @var bool $required Whether this element is required
  207. * @var string $label The display name for this element
  208. * }
  209. */',
  210. ];
  211. }
  212. public function testLinkToSee(): void
  213. {
  214. $this->fixer->configure(['replacements' => [
  215. 'link' => 'see',
  216. ]]);
  217. $this->doTest(
  218. '<?php /** @see https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#710-link-deprecated */',
  219. '<?php /** @link https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#710-link-deprecated */'
  220. );
  221. }
  222. /**
  223. * @dataProvider provideDefaultConfigCases
  224. */
  225. public function testDefaultConfig(string $expected, ?string $input = null): void
  226. {
  227. $this->doTest($expected, $input);
  228. }
  229. public static function provideDefaultConfigCases(): iterable
  230. {
  231. yield [
  232. '<?php /** @see https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#710-link-deprecated */',
  233. '<?php /** @link https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#710-link-deprecated */',
  234. ];
  235. yield [
  236. '<?php /** @property mixed $bar */',
  237. '<?php /** @property-write mixed $bar */',
  238. ];
  239. yield [
  240. '<?php /** @property mixed $bar */',
  241. '<?php /** @property-read mixed $bar */',
  242. ];
  243. yield [
  244. '<?php /** @var string Hello! */',
  245. '<?php /** @type string Hello! */',
  246. ];
  247. }
  248. }