AbstractDoctrineAnnotationFixerTestCase.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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;
  13. use PhpCsFixer\AbstractDoctrineAnnotationFixer;
  14. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  15. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  16. /**
  17. * @internal
  18. *
  19. * @template TFixer of AbstractDoctrineAnnotationFixer
  20. *
  21. * @extends AbstractFixerTestCase<TFixer>
  22. */
  23. abstract class AbstractDoctrineAnnotationFixerTestCase extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @param array<string, mixed> $configuration
  27. *
  28. * @dataProvider provideConfigureWithInvalidConfigurationCases
  29. */
  30. public function testConfigureWithInvalidConfiguration(array $configuration): void
  31. {
  32. $this->expectException(InvalidFixerConfigurationException::class);
  33. $this->fixer->configure($configuration);
  34. }
  35. /**
  36. * @return iterable<array{array<string, mixed>}>
  37. */
  38. public static function provideConfigureWithInvalidConfigurationCases(): iterable
  39. {
  40. yield [['foo' => 'bar']];
  41. yield [['ignored_tags' => 'foo']];
  42. }
  43. /**
  44. * @param list<array{0: string, 1?: string}> $commentCases
  45. *
  46. * @return iterable<array{0: string, 1?: string}>
  47. */
  48. protected static function createTestCases(array $commentCases): iterable
  49. {
  50. $noFixCases = [];
  51. foreach ($commentCases as $commentCase) {
  52. yield [
  53. self::withClassDocBlock($commentCase[0]),
  54. isset($commentCase[1]) ? self::withClassDocBlock($commentCase[1]) : null,
  55. ];
  56. yield [
  57. self::withPropertyDocBlock($commentCase[0]),
  58. isset($commentCase[1]) ? self::withPropertyDocBlock($commentCase[1]) : null,
  59. ];
  60. yield [
  61. self::withMethodDocBlock($commentCase[0]),
  62. isset($commentCase[1]) ? self::withMethodDocBlock($commentCase[1]) : null,
  63. ];
  64. $noFixCases[$commentCase[0]] = [
  65. self::withWrongElementDocBlock($commentCase[0]),
  66. ];
  67. }
  68. yield from array_values($noFixCases);
  69. }
  70. private static function withClassDocBlock(string $comment): string
  71. {
  72. return self::with('<?php
  73. %s
  74. class FooClass
  75. {
  76. }', $comment, false);
  77. }
  78. private static function withPropertyDocBlock(string $comment): string
  79. {
  80. return self::with('<?php
  81. class FooClass
  82. {
  83. %s
  84. private $foo;
  85. }', $comment, true);
  86. }
  87. private static function withMethodDocBlock(string $comment): string
  88. {
  89. return self::with('<?php
  90. class FooClass
  91. {
  92. %s
  93. public function foo()
  94. {
  95. }
  96. }', $comment, true);
  97. }
  98. private static function withWrongElementDocBlock(string $comment): string
  99. {
  100. return self::with('<?php
  101. %s
  102. $foo = bar();', $comment, false);
  103. }
  104. private static function with(string $php, string $comment, bool $indent): string
  105. {
  106. $comment = trim($comment);
  107. if ($indent) {
  108. $comment = str_replace("\n", "\n ", $comment);
  109. }
  110. return \sprintf($php, preg_replace('/^\n+/', '', $comment));
  111. }
  112. }