AbstractReporterTestCase.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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\Console\Report\FixReport;
  13. use PhpCsFixer\Console\Report\FixReport\ReporterInterface;
  14. use PhpCsFixer\Console\Report\FixReport\ReportSummary;
  15. use PhpCsFixer\Tests\TestCase;
  16. /**
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. *
  19. * @internal
  20. */
  21. abstract class AbstractReporterTestCase extends TestCase
  22. {
  23. /**
  24. * @var null|ReporterInterface
  25. */
  26. protected $reporter;
  27. protected function setUp(): void
  28. {
  29. parent::setUp();
  30. $this->reporter = $this->createReporter();
  31. }
  32. protected function tearDown(): void
  33. {
  34. parent::tearDown();
  35. $this->reporter = null;
  36. }
  37. final public function testGetFormat(): void
  38. {
  39. self::assertSame(
  40. $this->getFormat(),
  41. $this->reporter->getFormat()
  42. );
  43. }
  44. /**
  45. * @dataProvider provideGenerateCases
  46. */
  47. final public function testGenerate(string $expectedReport, ReportSummary $reportSummary): void
  48. {
  49. $actualReport = $this->reporter->generate($reportSummary);
  50. $this->assertFormat($expectedReport, $actualReport);
  51. }
  52. /**
  53. * @return iterable<string, array{string, ReportSummary}>
  54. */
  55. final public static function provideGenerateCases(): iterable
  56. {
  57. yield 'no errors' => [
  58. static::createNoErrorReport(),
  59. new ReportSummary(
  60. [],
  61. 10,
  62. 0,
  63. 0,
  64. false,
  65. false,
  66. false
  67. ),
  68. ];
  69. yield 'simple' => [
  70. static::createSimpleReport(),
  71. new ReportSummary(
  72. [
  73. 'someFile.php' => [
  74. 'appliedFixers' => ['some_fixer_name_here'],
  75. 'diff' => '--- Original
  76. +++ New
  77. @@ -2,7 +2,7 @@
  78. class Foo
  79. {
  80. - public function bar($foo = 1, $bar)
  81. + public function bar($foo, $bar)
  82. {
  83. }
  84. }',
  85. ],
  86. ],
  87. 10,
  88. 0,
  89. 0,
  90. false,
  91. false,
  92. false
  93. ),
  94. ];
  95. yield 'with diff' => [
  96. static::createWithDiffReport(),
  97. new ReportSummary(
  98. [
  99. 'someFile.php' => [
  100. 'appliedFixers' => ['some_fixer_name_here'],
  101. 'diff' => '--- Original
  102. +++ New
  103. @@ -2,7 +2,7 @@
  104. class Foo
  105. {
  106. - public function bar($foo = 1, $bar)
  107. + public function bar($foo, $bar)
  108. {
  109. }
  110. }',
  111. ],
  112. ],
  113. 10,
  114. 0,
  115. 0,
  116. false,
  117. false,
  118. false
  119. ),
  120. ];
  121. yield 'with applied fixers' => [
  122. static::createWithAppliedFixersReport(),
  123. new ReportSummary(
  124. [
  125. 'someFile.php' => [
  126. 'appliedFixers' => ['some_fixer_name_here_1', 'some_fixer_name_here_2'],
  127. 'diff' => '',
  128. ],
  129. ],
  130. 10,
  131. 0,
  132. 0,
  133. true,
  134. false,
  135. false
  136. ),
  137. ];
  138. yield 'with time and memory' => [
  139. static::createWithTimeAndMemoryReport(),
  140. new ReportSummary(
  141. [
  142. 'someFile.php' => [
  143. 'appliedFixers' => ['some_fixer_name_here'],
  144. 'diff' => '--- Original
  145. +++ New
  146. @@ -2,7 +2,7 @@
  147. class Foo
  148. {
  149. - public function bar($foo = 1, $bar)
  150. + public function bar($foo, $bar)
  151. {
  152. }
  153. }',
  154. ],
  155. ],
  156. 10,
  157. 1_234,
  158. 2_621_440, // 2.5 * 1024 * 1024
  159. false,
  160. false,
  161. false
  162. ),
  163. ];
  164. yield 'complex' => [
  165. static::createComplexReport(),
  166. new ReportSummary(
  167. [
  168. 'someFile.php' => [
  169. 'appliedFixers' => ['some_fixer_name_here_1', 'some_fixer_name_here_2'],
  170. 'diff' => 'this text is a diff ;)',
  171. ],
  172. 'anotherFile.php' => [
  173. 'appliedFixers' => ['another_fixer_name_here'],
  174. 'diff' => 'another diff here ;)',
  175. ],
  176. ],
  177. 10,
  178. 1_234,
  179. 2_621_440, // 2.5 * 1024 * 1024
  180. true,
  181. true,
  182. true
  183. ),
  184. ];
  185. }
  186. abstract protected function createReporter(): ReporterInterface;
  187. abstract protected function getFormat(): string;
  188. abstract protected static function createNoErrorReport(): string;
  189. abstract protected static function createSimpleReport(): string;
  190. abstract protected static function createWithDiffReport(): string;
  191. abstract protected static function createWithAppliedFixersReport(): string;
  192. abstract protected static function createWithTimeAndMemoryReport(): string;
  193. abstract protected static function createComplexReport(): string;
  194. abstract protected function assertFormat(string $expected, string $input): void;
  195. }