AbstractDoctrineAnnotationFixerTestCase.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @internal
  15. */
  16. abstract class AbstractDoctrineAnnotationFixerTestCase extends AbstractFixerTestCase
  17. {
  18. /**
  19. * @dataProvider provideInvalidConfigurationCases
  20. */
  21. public function testConfigureWithInvalidConfiguration(array $configuration)
  22. {
  23. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  24. $this->fixer->configure($configuration);
  25. }
  26. /**
  27. * @return array
  28. */
  29. public function provideInvalidConfigurationCases()
  30. {
  31. return [
  32. [['foo' => 'bar']],
  33. [['ignored_tags' => 'foo']],
  34. ];
  35. }
  36. /**
  37. * @param array<array<string>> $commentCases
  38. *
  39. * @return array
  40. */
  41. protected function createTestCases(array $commentCases)
  42. {
  43. $cases = [];
  44. foreach ($commentCases as $commentCase) {
  45. $cases[] = [
  46. $this->withClassDocBlock($commentCase[0]),
  47. isset($commentCase[1]) ? $this->withClassDocBlock($commentCase[1]) : null,
  48. ];
  49. $cases[] = [
  50. $this->withPropertyDocBlock($commentCase[0]),
  51. isset($commentCase[1]) ? $this->withPropertyDocBlock($commentCase[1]) : null,
  52. ];
  53. $cases[] = [
  54. $this->withMethodDocBlock($commentCase[0]),
  55. isset($commentCase[1]) ? $this->withMethodDocBlock($commentCase[1]) : null,
  56. ];
  57. $cases[] = [
  58. $this->withWrongElementDocBlock($commentCase[0]),
  59. ];
  60. }
  61. return $cases;
  62. }
  63. /**
  64. * @param string $comment
  65. *
  66. * @return string
  67. */
  68. private function withClassDocBlock($comment)
  69. {
  70. return $this->with('<?php
  71. %s
  72. class FooClass
  73. {
  74. }', $comment, false);
  75. }
  76. /**
  77. * @param string $comment
  78. *
  79. * @return string
  80. */
  81. private function withPropertyDocBlock($comment)
  82. {
  83. return $this->with('<?php
  84. class FooClass
  85. {
  86. %s
  87. private $foo;
  88. }', $comment, true);
  89. }
  90. /**
  91. * @param string $comment
  92. *
  93. * @return string
  94. */
  95. private function withMethodDocBlock($comment)
  96. {
  97. return $this->with('<?php
  98. class FooClass
  99. {
  100. %s
  101. public function foo()
  102. {
  103. }
  104. }', $comment, true);
  105. }
  106. /**
  107. * @param string $comment
  108. *
  109. * @return string
  110. */
  111. private function withWrongElementDocBlock($comment)
  112. {
  113. return $this->with('<?php
  114. %s
  115. $foo = bar();', $comment, false);
  116. }
  117. /**
  118. * @param string $php
  119. * @param string $comment
  120. * @param bool $indent
  121. *
  122. * @return string
  123. */
  124. private function with($php, $comment, $indent)
  125. {
  126. $comment = trim($comment);
  127. if ($indent) {
  128. $comment = str_replace("\n", "\n ", $comment);
  129. }
  130. return sprintf($php, preg_replace('/^\n+/', '', $comment));
  131. }
  132. }