AbstractDoctrineAnnotationFixerTestCase.php 3.2 KB

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