AbstractReporterTestCase.php 5.5 KB

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