AbstractDoctrineAnnotationFixerTestCase.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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<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. public static function provideConfigureWithInvalidConfigurationCases(): iterable
  31. {
  32. yield [['foo' => 'bar']];
  33. yield [['ignored_tags' => 'foo']];
  34. }
  35. /**
  36. * @param list<array{0: string, 1?: string}> $commentCases
  37. *
  38. * @return list<array{0: string, 1?: string}>
  39. */
  40. protected static function createTestCases(array $commentCases): array
  41. {
  42. $cases = [];
  43. foreach ($commentCases as $commentCase) {
  44. $cases[] = [
  45. self::withClassDocBlock($commentCase[0]),
  46. isset($commentCase[1]) ? self::withClassDocBlock($commentCase[1]) : null,
  47. ];
  48. $cases[] = [
  49. self::withPropertyDocBlock($commentCase[0]),
  50. isset($commentCase[1]) ? self::withPropertyDocBlock($commentCase[1]) : null,
  51. ];
  52. $cases[] = [
  53. self::withMethodDocBlock($commentCase[0]),
  54. isset($commentCase[1]) ? self::withMethodDocBlock($commentCase[1]) : null,
  55. ];
  56. $cases[] = [
  57. self::withWrongElementDocBlock($commentCase[0]),
  58. ];
  59. }
  60. return $cases;
  61. }
  62. private static function withClassDocBlock(string $comment): string
  63. {
  64. return self::with('<?php
  65. %s
  66. class FooClass
  67. {
  68. }', $comment, false);
  69. }
  70. private static function withPropertyDocBlock(string $comment): string
  71. {
  72. return self::with('<?php
  73. class FooClass
  74. {
  75. %s
  76. private $foo;
  77. }', $comment, true);
  78. }
  79. private static function withMethodDocBlock(string $comment): string
  80. {
  81. return self::with('<?php
  82. class FooClass
  83. {
  84. %s
  85. public function foo()
  86. {
  87. }
  88. }', $comment, true);
  89. }
  90. private static function withWrongElementDocBlock(string $comment): string
  91. {
  92. return self::with('<?php
  93. %s
  94. $foo = bar();', $comment, false);
  95. }
  96. private static function with(string $php, string $comment, bool $indent): string
  97. {
  98. $comment = trim($comment);
  99. if ($indent) {
  100. $comment = str_replace("\n", "\n ", $comment);
  101. }
  102. return sprintf($php, preg_replace('/^\n+/', '', $comment));
  103. }
  104. }