ProcessOutputTest.php 8.7 KB

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