AbstractDoctrineAnnotationFixerTestCase.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\Test\AbstractFixerTestCase;
  13. /**
  14. * Base class for Doctrine annotation fixer tests.
  15. *
  16. * @internal
  17. */
  18. abstract class AbstractDoctrineAnnotationFixerTestCase extends AbstractFixerTestCase
  19. {
  20. /**
  21. * @param array $configuration
  22. *
  23. * @dataProvider getInvalidConfigurationCases
  24. */
  25. public function testConfigureWithInvalidConfiguration(array $configuration)
  26. {
  27. $this->setExpectedException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  28. $this->fixer->configure($configuration);
  29. }
  30. /**
  31. * @return array
  32. */
  33. public function getInvalidConfigurationCases()
  34. {
  35. return [
  36. [['foo' => 'bar']],
  37. [['ignored_tags' => 'foo']],
  38. ];
  39. }
  40. /**
  41. * @param array<array<string>> $commentCases
  42. *
  43. * @return array
  44. */
  45. protected function createTestCases(array $commentCases)
  46. {
  47. $cases = [];
  48. foreach ($commentCases as $commentCase) {
  49. $cases[] = [
  50. $this->withClassDocBlock($commentCase[0]),
  51. isset($commentCase[1]) ? $this->withClassDocBlock($commentCase[1]) : null,
  52. ];
  53. $cases[] = [
  54. $this->withPropertyDocBlock($commentCase[0]),
  55. isset($commentCase[1]) ? $this->withPropertyDocBlock($commentCase[1]) : null,
  56. ];
  57. $cases[] = [
  58. $this->withMethodDocBlock($commentCase[0]),
  59. isset($commentCase[1]) ? $this->withMethodDocBlock($commentCase[1]) : null,
  60. ];
  61. $cases[] = [
  62. $this->withWrongElementDocBlock($commentCase[0]),
  63. ];
  64. }
  65. return $cases;
  66. }
  67. /**
  68. * @param string $comment
  69. *
  70. * @return string
  71. */
  72. private function withClassDocBlock($comment)
  73. {
  74. return $this->with('<?php
  75. %s
  76. class FooClass
  77. {
  78. }', $comment, false);
  79. }
  80. /**
  81. * @param string $comment
  82. *
  83. * @return string
  84. */
  85. private function withPropertyDocBlock($comment)
  86. {
  87. return $this->with('<?php
  88. class FooClass
  89. {
  90. %s
  91. private $foo;
  92. }', $comment, true);
  93. }
  94. /**
  95. * @param string $comment
  96. *
  97. * @return string
  98. */
  99. private function withMethodDocBlock($comment)
  100. {
  101. return $this->with('<?php
  102. class FooClass
  103. {
  104. %s
  105. public function foo()
  106. {
  107. }
  108. }', $comment, true);
  109. }
  110. /**
  111. * @param string $comment
  112. *
  113. * @return string
  114. */
  115. private function withWrongElementDocBlock($comment)
  116. {
  117. return $this->with('<?php
  118. %s
  119. $foo = bar();', $comment, false);
  120. }
  121. /**
  122. * @param string $php
  123. * @param string $comment
  124. * @param bool $indent
  125. *
  126. * @return string
  127. */
  128. private function with($php, $comment, $indent)
  129. {
  130. $comment = trim($comment);
  131. if ($indent) {
  132. $comment = str_replace("\n", "\n ", $comment);
  133. }
  134. return sprintf($php, preg_replace('/^\n+/', '', $comment));
  135. }
  136. }