DoctrineAnnotationArrayAssignmentFixerTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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\DoctrineAnnotation;
  13. use PhpCsFixer\Tests\AbstractDoctrineAnnotationFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\AbstractDoctrineAnnotationFixer
  18. * @covers \PhpCsFixer\Doctrine\Annotation\DocLexer
  19. * @covers \PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationArrayAssignmentFixer
  20. *
  21. * @extends AbstractDoctrineAnnotationFixerTestCase<\PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationArrayAssignmentFixer>
  22. *
  23. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\DoctrineAnnotation\DoctrineAnnotationArrayAssignmentFixer
  24. */
  25. final class DoctrineAnnotationArrayAssignmentFixerTest extends AbstractDoctrineAnnotationFixerTestCase
  26. {
  27. /**
  28. * @dataProvider provideFixCases
  29. */
  30. public function testFix(string $expected, ?string $input = null): void
  31. {
  32. $this->doTest($expected, $input);
  33. }
  34. /**
  35. * @dataProvider provideFixCases
  36. */
  37. public function testFixWithEqual(string $expected, ?string $input = null): void
  38. {
  39. $this->fixer->configure(['operator' => '=']);
  40. $this->doTest($expected, $input);
  41. }
  42. /**
  43. * @return iterable<array{0: string, 1?: null|string}>
  44. */
  45. public static function provideFixCases(): iterable
  46. {
  47. yield from self::createTestCases([
  48. ['
  49. /**
  50. * @Foo
  51. */',
  52. ],
  53. ['
  54. /**
  55. * @Foo()
  56. */',
  57. ],
  58. ['
  59. /**
  60. * @Foo(bar="baz")
  61. */',
  62. ],
  63. [
  64. '
  65. /**
  66. * @Foo({bar="baz"})
  67. */',
  68. '
  69. /**
  70. * @Foo({bar:"baz"})
  71. */',
  72. ],
  73. [
  74. '
  75. /**
  76. * @Foo({bar = "baz"})
  77. */',
  78. '
  79. /**
  80. * @Foo({bar : "baz"})
  81. */',
  82. ],
  83. ['
  84. /**
  85. * See {@link https://help Help} or {@see BarClass} for details.
  86. */',
  87. ],
  88. ]);
  89. yield [
  90. '<?php
  91. /**
  92. * @see \User getId()
  93. */
  94. ',
  95. ];
  96. }
  97. /**
  98. * @dataProvider provideFixWithColonCases
  99. */
  100. public function testFixWithColon(string $expected, ?string $input = null): void
  101. {
  102. $this->fixer->configure(['operator' => ':']);
  103. $this->doTest($expected, $input);
  104. }
  105. /**
  106. * @return iterable<array{0: string, 1?: null|string}>
  107. */
  108. public static function provideFixWithColonCases(): iterable
  109. {
  110. yield from self::createTestCases([
  111. ['
  112. /**
  113. * @Foo
  114. */',
  115. ],
  116. ['
  117. /**
  118. * @Foo()
  119. */',
  120. ],
  121. [
  122. '
  123. /**
  124. * @Foo(bar:"baz")
  125. */',
  126. ],
  127. [
  128. '
  129. /**
  130. * @Foo({bar:"baz"})
  131. */',
  132. '
  133. /**
  134. * @Foo({bar="baz"})
  135. */',
  136. ],
  137. [
  138. '
  139. /**
  140. * @Foo({bar : "baz"})
  141. */',
  142. '
  143. /**
  144. * @Foo({bar = "baz"})
  145. */',
  146. ],
  147. [
  148. '
  149. /**
  150. * @Foo(foo="bar", {bar:"baz"})
  151. */',
  152. '
  153. /**
  154. * @Foo(foo="bar", {bar="baz"})
  155. */',
  156. ],
  157. ['
  158. /**
  159. * See {@link https://help Help} or {@see BarClass} for details.
  160. */'],
  161. ]);
  162. }
  163. /**
  164. * @dataProvider provideFix81Cases
  165. *
  166. * @requires PHP 8.1
  167. */
  168. public function testFix81(string $expected, ?string $input = null): void
  169. {
  170. $this->doTest($expected, $input);
  171. }
  172. /**
  173. * @return iterable<array{string, string}>
  174. */
  175. public static function provideFix81Cases(): iterable
  176. {
  177. yield [
  178. '<?php class FooClass{
  179. /**
  180. * @Foo({bar = "baz"})
  181. */
  182. private readonly Foo $foo;
  183. }',
  184. '<?php class FooClass{
  185. /**
  186. * @Foo({bar : "baz"})
  187. */
  188. private readonly Foo $foo;
  189. }',
  190. ];
  191. yield [
  192. '<?php class FooClass{
  193. /**
  194. * @Foo({bar = "baz"})
  195. */
  196. readonly private Foo $foo;
  197. }',
  198. '<?php class FooClass{
  199. /**
  200. * @Foo({bar : "baz"})
  201. */
  202. readonly private Foo $foo;
  203. }',
  204. ];
  205. yield [
  206. '<?php class FooClass{
  207. /**
  208. * @Foo({bar = "baz"})
  209. */
  210. readonly Foo $foo;
  211. }',
  212. '<?php class FooClass{
  213. /**
  214. * @Foo({bar : "baz"})
  215. */
  216. readonly Foo $foo;
  217. }',
  218. ];
  219. }
  220. }