AbstractDoctrineAnnotationFixerTestCase.php 3.1 KB

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