4 * This file is part of the Behat.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Behat\Output\Node\Printer;
13 use Behat\Behat\Output\Node\Printer\Helper\ResultToStringConverter;
14 use Behat\Testwork\Output\Printer\OutputPrinter;
15 use Symfony\Component\Translation\TranslatorInterface;
18 * Behat counter printer.
20 * @author Konstantin Kudryashov <ever.zet@gmail.com>
22 final class CounterPrinter
25 * @var ResultToStringConverter
27 private $resultConverter;
29 * @var TranslatorInterface
34 * Initializes printer.
36 * @param ResultToStringConverter $resultConverter
37 * @param TranslatorInterface $translator
39 public function __construct(ResultToStringConverter $resultConverter, TranslatorInterface $translator)
41 $this->resultConverter = $resultConverter;
42 $this->translator = $translator;
46 * Prints scenario and step counters.
48 * @param OutputPrinter $printer
49 * @param string $intro
52 public function printCounters(OutputPrinter $printer, $intro, array $stats)
54 $stats = array_filter($stats, function ($count) { return 0 !== $count; });
56 if (0 === count($stats)) {
59 $totalCount = array_sum($stats);
62 $detailedStats = array();
63 foreach ($stats as $resultCode => $count) {
64 $style = $this->resultConverter->convertResultCodeToString($resultCode);
66 $transId = $style . '_count';
67 $message = $this->translator->transChoice($transId, $count, array('%1%' => $count), 'output');
69 $detailedStats[] = sprintf('{+%s}%s{-%s}', $style, $message, $style);
72 $message = $this->translator->transChoice($intro, $totalCount, array('%1%' => $totalCount), 'output');
73 $printer->write($message);
75 if (count($detailedStats)) {
76 $printer->write(sprintf(' (%s)', implode(', ', $detailedStats)));