DotsOutputTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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\Output\Progress;
  13. use PhpCsFixer\Console\Output\OutputContext;
  14. use PhpCsFixer\Console\Output\Progress\DotsOutput;
  15. use PhpCsFixer\Runner\Event\FileProcessed;
  16. use PhpCsFixer\Tests\TestCase;
  17. use Symfony\Component\Console\Output\BufferedOutput;
  18. /**
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Console\Output\Progress\DotsOutput
  22. */
  23. final class DotsOutputTest extends TestCase
  24. {
  25. /**
  26. * @param list<array{0: FileProcessed::STATUS_*, 1?: int}> $statuses
  27. *
  28. * @dataProvider provideDotsProgressOutputCases
  29. */
  30. public function testDotsProgressOutput(array $statuses, string $expectedOutput, int $width): void
  31. {
  32. $nbFiles = 0;
  33. $this->foreachStatus($statuses, static function () use (&$nbFiles): void {
  34. ++$nbFiles;
  35. });
  36. $output = new BufferedOutput();
  37. $processOutput = new DotsOutput(new OutputContext($output, $width, $nbFiles));
  38. $this->foreachStatus($statuses, static function (int $status) use ($processOutput): void {
  39. $processOutput->onFixerFileProcessed(new FileProcessed($status));
  40. });
  41. self::assertSame($expectedOutput, $output->fetch());
  42. }
  43. public static function provideDotsProgressOutputCases(): iterable
  44. {
  45. yield [
  46. [
  47. [FileProcessed::STATUS_NO_CHANGES, 4],
  48. ],
  49. '.... 4 / 4 (100%)',
  50. 80,
  51. ];
  52. yield [
  53. [
  54. [FileProcessed::STATUS_NO_CHANGES],
  55. [FileProcessed::STATUS_FIXED],
  56. [FileProcessed::STATUS_NO_CHANGES, 4],
  57. ],
  58. '.F.... 6 / 6 (100%)',
  59. 80,
  60. ];
  61. yield [
  62. [
  63. [FileProcessed::STATUS_NO_CHANGES, 65],
  64. ],
  65. '................................................................. 65 / 65 (100%)',
  66. 80,
  67. ];
  68. yield [
  69. [
  70. [FileProcessed::STATUS_NO_CHANGES, 66],
  71. ],
  72. '................................................................. 65 / 66 ( 98%)'.PHP_EOL.
  73. '. 66 / 66 (100%)',
  74. 80,
  75. ];
  76. yield [
  77. [
  78. [FileProcessed::STATUS_NO_CHANGES, 66],
  79. ],
  80. '......................... 25 / 66 ( 38%)'.PHP_EOL.
  81. '......................... 50 / 66 ( 76%)'.PHP_EOL.
  82. '................ 66 / 66 (100%)',
  83. 40,
  84. ];
  85. yield [
  86. [
  87. [FileProcessed::STATUS_NO_CHANGES, 66],
  88. ],
  89. '.................................................................. 66 / 66 (100%)',
  90. 100,
  91. ];
  92. yield [
  93. [
  94. [FileProcessed::STATUS_NO_CHANGES, 19],
  95. [FileProcessed::STATUS_EXCEPTION],
  96. [FileProcessed::STATUS_NO_CHANGES, 6],
  97. [FileProcessed::STATUS_LINT],
  98. [FileProcessed::STATUS_FIXED, 3],
  99. [FileProcessed::STATUS_NO_CHANGES, 50],
  100. [FileProcessed::STATUS_SKIPPED],
  101. [FileProcessed::STATUS_NO_CHANGES, 49],
  102. [FileProcessed::STATUS_INVALID],
  103. [FileProcessed::STATUS_NO_CHANGES],
  104. [FileProcessed::STATUS_INVALID],
  105. [FileProcessed::STATUS_NO_CHANGES, 40],
  106. [FileProcessed::STATUS_INVALID],
  107. [FileProcessed::STATUS_NO_CHANGES, 15],
  108. ],
  109. '...................E......EFFF................................. 63 / 189 ( 33%)'.PHP_EOL.
  110. '.................S............................................. 126 / 189 ( 67%)'.PHP_EOL.
  111. '....I.I........................................I............... 189 / 189 (100%)',
  112. 80,
  113. ];
  114. yield [
  115. [
  116. [FileProcessed::STATUS_NO_CHANGES, 19],
  117. [FileProcessed::STATUS_EXCEPTION],
  118. [FileProcessed::STATUS_NO_CHANGES, 6],
  119. [FileProcessed::STATUS_LINT],
  120. [FileProcessed::STATUS_FIXED, 3],
  121. [FileProcessed::STATUS_NO_CHANGES, 50],
  122. [FileProcessed::STATUS_SKIPPED],
  123. [FileProcessed::STATUS_NO_CHANGES, 49],
  124. [FileProcessed::STATUS_INVALID],
  125. [FileProcessed::STATUS_NO_CHANGES],
  126. [FileProcessed::STATUS_INVALID],
  127. [FileProcessed::STATUS_NO_CHANGES, 40],
  128. [FileProcessed::STATUS_INVALID],
  129. [FileProcessed::STATUS_NO_CHANGES, 15],
  130. ],
  131. '...................E......EFFF..................................................S...................... 103 / 189 ( 54%)'.PHP_EOL.
  132. '...........................I.I........................................I............... 189 / 189 (100%)',
  133. 120,
  134. ];
  135. }
  136. public function testSleep(): void
  137. {
  138. $this->expectException(\BadMethodCallException::class);
  139. $this->expectExceptionMessage('Cannot serialize '.DotsOutput::class);
  140. $processOutput = new DotsOutput(new OutputContext(new BufferedOutput(), 1, 1));
  141. $processOutput->__sleep();
  142. }
  143. public function testWakeup(): void
  144. {
  145. $this->expectException(\BadMethodCallException::class);
  146. $this->expectExceptionMessage('Cannot unserialize '.DotsOutput::class);
  147. $processOutput = new DotsOutput(new OutputContext(new BufferedOutput(), 1, 1));
  148. $processOutput->__wakeup();
  149. }
  150. /**
  151. * @param list<array{0: FileProcessed::STATUS_*, 1?: int}> $statuses
  152. * @param \Closure(FileProcessed::STATUS_*): void $action
  153. */
  154. private function foreachStatus(array $statuses, \Closure $action): void
  155. {
  156. foreach ($statuses as $status) {
  157. $multiplier = $status[1] ?? 1;
  158. $status = $status[0];
  159. for ($i = 0; $i < $multiplier; ++$i) {
  160. $action($status);
  161. }
  162. }
  163. }
  164. }