PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpspec/phpspec/src/PhpSpec/Formatter/ProgressFormatter.php

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 171 lines | 114 code | 27 blank | 30 comment | 16 complexity | 330cb116d3eda9f55cb0b04acfcb6e9b MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of PhpSpec, A php toolset to drive emergent
  4. * design by specification.
  5. *
  6. * (c) Marcello Duarte <marcello.duarte@gmail.com>
  7. * (c) Konstantin Kudryashov <ever.zet@gmail.com>
  8. *
  9. * For the full copyright and license information, please view the LICENSE
  10. * file that was distributed with this source code.
  11. */
  12. namespace PhpSpec\Formatter;
  13. use PhpSpec\Console\IO;
  14. use PhpSpec\Event\SuiteEvent;
  15. use PhpSpec\Event\ExampleEvent;
  16. class ProgressFormatter extends ConsoleFormatter
  17. {
  18. const FPS = 10;
  19. private $lastDraw;
  20. public function afterExample(ExampleEvent $event)
  21. {
  22. $this->printException($event);
  23. $now = microtime(true);
  24. if (!$this->lastDraw || ($now - $this->lastDraw) > 1/self::FPS) {
  25. $this->lastDraw = $now;
  26. $this->drawStats();
  27. }
  28. }
  29. public function afterSuite(SuiteEvent $event)
  30. {
  31. $this->drawStats();
  32. $io = $this->getIO();
  33. $stats = $this->getStatisticsCollector();
  34. $io->freezeTemp();
  35. $io->writeln();
  36. $io->writeln(sprintf("%d specs", $stats->getTotalSpecs()));
  37. $counts = array();
  38. foreach ($stats->getCountsHash() as $type => $count) {
  39. if ($count) {
  40. $counts[] = sprintf('<%s>%d %s</%s>', $type, $count, $type, $type);
  41. }
  42. }
  43. $count = $stats->getEventsCount();
  44. $plural = $count !== 1 ? 's' : '';
  45. $io->write(sprintf("%d example%s ", $count, $plural));
  46. if (count($counts)) {
  47. $io->write(sprintf("(%s)", implode(', ', $counts)));
  48. }
  49. $io->writeln(sprintf("\n%sms", round($event->getTime() * 1000)));
  50. $io->writeln();
  51. }
  52. /**
  53. * @param $total
  54. * @param $counts
  55. * @return array
  56. */
  57. private function getPercentages($total, $counts)
  58. {
  59. return array_map(
  60. function ($count) use ($total) {
  61. if (0 == $total) {
  62. return 0;
  63. }
  64. $percent = ($count == $total) ? 100 : $count / ($total / 100);
  65. return $percent == 0 || $percent > 1 ? floor($percent) : 1;
  66. },
  67. $counts
  68. );
  69. }
  70. /**
  71. * @param array $counts
  72. * @return array
  73. */
  74. private function getBarLengths($counts)
  75. {
  76. $stats = $this->getStatisticsCollector();
  77. $totalSpecsCount = $stats->getTotalSpecsCount();
  78. $specProgress = ($totalSpecsCount == 0) ? 1 : ($stats->getTotalSpecs() / $totalSpecsCount);
  79. $targetWidth = ceil($this->getIO()->getBlockWidth() * $specProgress);
  80. asort($counts);
  81. $barLengths = array_map(function ($count) use ($targetWidth, $counts) {
  82. return $count ? max(1, round($targetWidth * $count / array_sum($counts))) : 0;
  83. }, $counts);
  84. return $barLengths;
  85. }
  86. /**
  87. * @param array $barLengths
  88. * @param array $percents
  89. * @param boolean $isDecorated
  90. * @return array
  91. */
  92. private function formatProgressOutput($barLengths, $percents, $isDecorated)
  93. {
  94. $size = $this->getIO()->getBlockWidth();
  95. $progress = array();
  96. foreach ($barLengths as $status => $length) {
  97. $percent = $percents[$status];
  98. $text = $percent.'%';
  99. $length = ($size - $length) >= 0 ? $length : $size;
  100. $size = $size - $length;
  101. if ($isDecorated) {
  102. if ($length > strlen($text) + 2) {
  103. $text = str_pad($text, $length, ' ', STR_PAD_BOTH);
  104. } else {
  105. $text = str_pad('', $length, ' ');
  106. }
  107. $progress[$status] = sprintf("<$status-bg>%s</$status-bg>", $text);
  108. } else {
  109. $progress[$status] = str_pad(
  110. sprintf('%s: %s', $status, $text),
  111. 15,
  112. ' ',
  113. STR_PAD_BOTH
  114. );
  115. }
  116. }
  117. krsort($progress);
  118. return $progress;
  119. }
  120. /**
  121. * @param IO $io
  122. * @param array $progress
  123. * @param int $total
  124. */
  125. private function updateProgressBar(IO $io, array $progress, $total)
  126. {
  127. if ($io->isDecorated()) {
  128. $progressBar = implode('', $progress);
  129. $pad = $this->getIO()->getBlockWidth() - strlen(strip_tags($progressBar));
  130. $io->writeTemp($progressBar.str_repeat(' ', $pad + 1).$total);
  131. } else {
  132. $io->writeTemp('/'.implode('/', $progress).'/ '.$total.' examples');
  133. }
  134. }
  135. private function drawStats()
  136. {
  137. $io = $this->getIO();
  138. $stats = $this->getStatisticsCollector();
  139. $percents = $this->getPercentages($stats->getEventsCount(), $stats->getCountsHash());
  140. $barLengths = $this->getBarLengths($stats->getCountsHash());
  141. $progress = $this->formatProgressOutput($barLengths, $percents, $io->isDecorated());
  142. $this->updateProgressBar($io, $progress, $stats->getEventsCount());
  143. }
  144. }