X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fbehat%2Fbehat%2Fsrc%2FBehat%2FBehat%2FOutput%2FNode%2FEventListener%2FStatistics%2FHookStatsListener.php;fp=vendor%2Fbehat%2Fbehat%2Fsrc%2FBehat%2FBehat%2FOutput%2FNode%2FEventListener%2FStatistics%2FHookStatsListener.php;h=a8d6007b94ba39b044a170ac5b80c44b8a1d0c2a;hp=0000000000000000000000000000000000000000;hb=1270d9129ce8f27c9b28b10518e32132c58e0aca;hpb=c27c0f0cdaa3f354b1fe54a56ae7e854be6e3f68 diff --git a/vendor/behat/behat/src/Behat/Behat/Output/Node/EventListener/Statistics/HookStatsListener.php b/vendor/behat/behat/src/Behat/Behat/Output/Node/EventListener/Statistics/HookStatsListener.php new file mode 100644 index 000000000..a8d6007b9 --- /dev/null +++ b/vendor/behat/behat/src/Behat/Behat/Output/Node/EventListener/Statistics/HookStatsListener.php @@ -0,0 +1,123 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Behat\Behat\Output\Node\EventListener\Statistics; + +use Behat\Behat\Output\Statistics\HookStat; +use Behat\Behat\Output\Statistics\Statistics; +use Behat\Testwork\Call\CallResult; +use Behat\Testwork\EventDispatcher\Event\AfterSetup; +use Behat\Testwork\EventDispatcher\Event\AfterTested; +use Behat\Testwork\Exception\ExceptionPresenter; +use Behat\Testwork\Hook\Tester\Setup\HookedSetup; +use Behat\Testwork\Hook\Tester\Setup\HookedTeardown; +use Behat\Testwork\Output\Formatter; +use Behat\Testwork\Output\Node\EventListener\EventListener; +use Symfony\Component\EventDispatcher\Event; + +/** + * Listens and records hook stats. + * + * @author Konstantin Kudryashov + */ +final class HookStatsListener implements EventListener +{ + /** + * @var Statistics + */ + private $statistics; + /** + * @var ExceptionPresenter + */ + private $exceptionPresenter; + + /** + * Initializes listener. + * + * @param Statistics $statistics + * @param ExceptionPresenter $exceptionPresenter + */ + public function __construct(Statistics $statistics, ExceptionPresenter $exceptionPresenter) + { + $this->statistics = $statistics; + $this->exceptionPresenter = $exceptionPresenter; + } + + /** + * {@inheritdoc} + */ + public function listenEvent(Formatter $formatter, Event $event, $eventName) + { + $this->captureHookStatsOnEvent($event); + } + + /** + * Captures hook stats on hooked event. + * + * @param Event $event + */ + private function captureHookStatsOnEvent(Event $event) + { + if ($event instanceof AfterSetup && $event->getSetup() instanceof HookedSetup) { + $this->captureBeforeHookStats($event->getSetup()); + } + + if ($event instanceof AfterTested && $event->getTeardown() instanceof HookedTeardown) { + $this->captureAfterHookStats($event->getTeardown()); + } + } + + /** + * Captures before hook stats. + * + * @param HookedSetup $setup + */ + private function captureBeforeHookStats(HookedSetup $setup) + { + $hookCallResults = $setup->getHookCallResults(); + + foreach ($hookCallResults as $hookCallResult) { + $this->captureHookStat($hookCallResult); + } + } + + /** + * Captures before hook stats. + * + * @param HookedTeardown $teardown + */ + private function captureAfterHookStats(HookedTeardown $teardown) + { + $hookCallResults = $teardown->getHookCallResults(); + + foreach ($hookCallResults as $hookCallResult) { + $this->captureHookStat($hookCallResult); + } + } + + /** + * Captures hook call result. + * + * @param CallResult $hookCallResult + */ + private function captureHookStat(CallResult $hookCallResult) + { + $callee = $hookCallResult->getCall()->getCallee(); + $hook = (string) $callee; + $path = $callee->getPath(); + $stdOut = $hookCallResult->getStdOut(); + $error = $hookCallResult->getException() + ? $this->exceptionPresenter->presentException($hookCallResult->getException()) + : null; + + $stat = new HookStat($hook, $path, $error, $stdOut); + $this->statistics->registerHookStat($stat); + } +}