AbstractDoctrineAnnotationFixerTestCase.php 3.0 KB

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