yoanm/behat-utils-extension

This package is abandoned and no longer maintained. No replacement package was suggested.

A set of utility for Behat3

0.7.0 2017-01-22 08:49 UTC

This package is auto-updated.

Last update: 2022-02-23 02:12:05 UTC


README

Scrutinizer Build Status Scrutinizer Code Quality Code Coverage

Travis Build Status PHP Versions

Latest Stable Version

BehatUtilsExtension provide a set of utility for Behat 3.0+

Install

composer require --dev yoanm/behat-utils-extension

How to use

BehatUtilsExtension require behat/behat and monolog/monolog

Configuration

Add the following in your behat configuration file (usually behat.yml) :

default:
    extensions:
        Yoanm\BehatUtilsExtension: ~

In the box

Logger

See LoggerAwareInterface

Implements this interface and your context will have a logger injected

Example

<?php
namespace Functional\Yoanm\BehatUtilsExtension\Context;

use Behat\Behat\Context\Context;
use Psr\Log\LoggerInterface;
use Yoanm\BehatUtilsExtension\Context\LoggerAwareInterface;

class FeatureContext implements Context, LoggerAwareInterface
{
    /** @var LoggerInterface */
    private $logger;

    /**
     * {@inheritdoc}
     */
    public function setBehatLogger(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * @When my step
     */
    public function myStep()
    {
        $this->logger->info('Executing my step');
    }
}

Behat event subscription

See BehatContextSubscriberInterface

Enable initializer and implements BehatContextSubscriberInterface, then your context will be passed to Behat dispatcher in order to receive behat events

Enable context initializer

default:
    extensions:
        Yoanm\BehatUtilsExtension:
            event_subscriber: true

Example

<?php
namespace Functional\Yoanm\BehatUtilsExtension\Context;

use Behat\Behat\Context\Context;
use Behat\Behat\EventDispatcher\Event\ExampleTested;
use Behat\Behat\EventDispatcher\Event\GherkinNodeTested;
use Behat\Behat\EventDispatcher\Event\ScenarioTested;
use Yoanm\BehatUtilsExtension\Context\BehatContextSubscriberInterface;

class FeatureContext implements Context, BehatContextSubscriberInterface
{
    /**
     * @param GherkinNodeTested $event
     */
    public function reset(GherkinNodeTested $event)
    {
        /**
         * Reset here your data before a scenario or example is started
         */
    }

    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents()
    {
        return [
            ScenarioTested::BEFORE => ['reset'],
            ExampleTested::BEFORE => ['reset'],
            //Or
            // ScenarioTested::BEFORE => ['reset', ListenerPriority::HIGH_PRIORITY],
        ];
    }
}

Step logger

See BehatStepLoggerSubscriber

If enabled, will log each behat feature/background/outline/example/step start and end. Usefull to understand what happens behing the scene if you have a non understandable error in you features.

Could be use with Logger to easily spot an issue in context custom steps.

Enable step listener

default:
    extensions:
        Yoanm\BehatUtilsExtension:
            step_logger: true

Output example

The following will be appended in the configured log file (see default configuration reference below).

behatUtils.DEBUG: [BehatStepLoggerSubscriber] [FEATURE][IN] {"title":"FEATURE TITLE","file":"FEATURE FILE PATH"} []
behatUtils.DEBUG: [BehatStepLoggerSubscriber] [FEATURE][OUT] {"title":"FEATURE TITLE","file":"FEATURE FILE PATH"} []

behatUtils.DEBUG: [BehatStepLoggerSubscriber] [BACKGROUND][IN] {"title":"BACKGROUND TITLE","line":"BACKGROUND START LINE"} []
behatUtils.DEBUG: [BehatStepLoggerSubscriber] [BACKGROUND][OUT] {"title":"BACKGROUND TITLE","line":"BACKGROUND END LINE"} []

behatUtils.DEBUG: [BehatStepLoggerSubscriber] [OUTLINE][IN] {"title":"OUTLINE TITLE","line": "OUTLINE START LINE"} []
behatUtils.DEBUG: [BehatStepLoggerSubscriber] [OUTLINE][OUT] {"title":"OUTLINE TITLE","line": "OUTLINE END LINE"} []

behatUtils.DEBUG: [BehatStepLoggerSubscriber] [EXAMPLE][IN] {"tokens":{"EXAMPLE_TOKENS_NAME":"EXAMPLE_TOKENS_VALUE"},"title":"| EXAMPLE_TOKENS_VALUE|","line":"EXAMPLE START LINE"} []
behatUtils.DEBUG: [BehatStepLoggerSubscriber] [EXAMPLE][OUT] {"tokens":{"EXAMPLE_TOKENS_NAME":"EXAMPLE_TOKENS_VALUE"},"title":"| EXAMPLE_TOKENS_VALUE|","line":"EXAMPLE END LINE"} []

behatUtils.DEBUG: [BehatStepLoggerSubscriber] [STEP][IN] {"text":"STEP TEXT","line":"STEP LINE"} []
behatUtils.DEBUG: [BehatStepLoggerSubscriber] [STEP][OUT] {"text":"STEP TEXT","line":"STEP LINE"} []

Configuration reference

default:
    extensions:
        Yoanm\BehatUtilsExtension:
            logger:
                path: behat.log
                level: INFO
            event_subscriber: false
            step_logger: false

Tests

This repository follow a custom test strategy

Contributing

See contributing note