AbstractDoctrineAnnotationFixerTestCase.php 2.9 KB

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