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\EventListener\Statistics;
13 use Behat\Behat\Output\Node\Printer\StatisticsPrinter;
14 use Behat\Behat\Output\Statistics\Statistics;
15 use Behat\Testwork\EventDispatcher\Event\ExerciseCompleted;
16 use Behat\Testwork\Output\Formatter;
17 use Behat\Testwork\Output\Node\EventListener\EventListener;
18 use Symfony\Component\EventDispatcher\Event;
21 * Collects general suite stats such as time and memory during its execution and prints it afterwards.
23 * @author Konstantin Kudryashov <ever.zet@gmail.com>
25 final class StatisticsListener implements EventListener
32 * @var StatisticsPrinter
37 * Initializes listener.
39 * @param Statistics $statistics
40 * @param StatisticsPrinter $statisticsPrinter
42 public function __construct(Statistics $statistics, StatisticsPrinter $statisticsPrinter)
44 $this->statistics = $statistics;
45 $this->printer = $statisticsPrinter;
51 public function listenEvent(Formatter $formatter, Event $event, $eventName)
53 $this->startTimerOnBeforeExercise($eventName);
54 $this->printStatisticsOnAfterExerciseEvent($formatter, $eventName);
58 * Starts timer on exercise BEFORE event.
60 * @param string $eventName
62 private function startTimerOnBeforeExercise($eventName)
64 if (ExerciseCompleted::BEFORE !== $eventName) {
68 $this->statistics->startTimer();
72 * Prints statistics on after exercise event.
74 * @param Formatter $formatter
75 * @param string $eventName
77 private function printStatisticsOnAfterExerciseEvent(Formatter $formatter, $eventName)
79 if (ExerciseCompleted::AFTER !== $eventName) {
83 $this->statistics->stopTimer();
84 $this->printer->printStatistics($formatter, $this->statistics);