2017 © Pedro Peláez
 

behat-extension scenariostate-behat-extension

Scenario shared state extension for Behat

image

gorghoa/scenariostate-behat-extension

Scenario shared state extension for Behat

  • Wednesday, June 6, 2018
  • by vincentchalamon
  • Repository
  • 3 Watchers
  • 23 Stars
  • 14,677 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 5 Forks
  • 6 Open issues
  • 17 Versions
  • 26 % Grown

The README.md

ScenarioStateBehatExtension

Build Status Scrutinizer Code Quality Code Coverage, (*1)

:warning: This projet is not maintained anymore. Still, anyone interested to take over is welcome to do so :)., (*2)

When to use

Behat scenarios are all about state. First you put the system under test to a special state through the Given steps. Then you continue to manipulate your system through When steps and finally testing the resulting state via the Then steps., (*3)

When testing a system like a single page app or a stateful website, the resulting state of our steps is handled by the system itself (either by the browser, or by the php session, etc.)., (*4)

But, when you are testing a stateless system, chiefly an API, then the resulting state of our steps is handled by no one. This is the case for this extension., (*5)

Installation

composer require --dev gorghoa/scenariostate-behat-extension @RC

Then update your project's behat.yml config file by loading the extension:, (*6)

default:
    extensions:
        Gorghoa\ScenarioStateBehatExtension\ServiceContainer\ScenarioStateExtension: ~

Usage

This behat extension will allow scenarios steps to provide and consume what I call "fragments" of the resulting state., (*7)

Each scenario get it's own isolated and unique state., (*8)

Let's say a feature like this:, (*9)


Feature: Monkey gathering bananas Scenario: Monkey gives a banana to another monkey When bonobo takes a banana And bonobo gives this banana to "gorilla"

See the "this banana"? What we want during the second step execution is a reference to the exact banana the bonobo initially took. This behat extension will help us to propagate the banana refence amongst steps., (*10)

Provide state fragment

To share a piece of state with all other scenario's steps, your contexts need to implement the Gorghoa\ScenarioStateBehatExtension\Context\ScenarioStateAwareContext interface., (*11)

This interface declares one method to implement: public function setScenarioState(ScenarioStateInterface $scenarioState) which can be imported using ScenarioStateAwareTrait. This ScenarioState is responsible for storing your state., (*12)

use Gorghoa\ScenarioStateBehatExtension\Context\ScenarioStateAwareContext;
use Gorghoa\ScenarioStateBehatExtension\Context\ScenarioStateAwareTrait;
use Gorghoa\ScenarioStateBehatExtension\ScenarioStateInterface;

class FeatureContext implements ScenarioStateAwareContext
{
    use ScenarioStateAwareTrait;
}

Then you can publish state fragment through the ScenarioStateInterface::provideStateFragment(string $key, mixed $value) method., (*13)

/**
 * @When bonobo takes a banana
 */
public function takeBanana()
{
    $banana = 'Yammy Banana';
    $bonobo = new Bonobo('Gerard');

    // Here, the banana `Yammy Banana` is shared amongst steps through the key "scenarioBanana"
    $this->scenarioState->provideStateFragment('scenarioBanana', $banana);

    // Here, the bonobo Gerard is shared amongst steps through the key "scenarioBonobo"
    $this->scenarioState->provideStateFragment('scenarioBonobo', $bonobo);
}

Consuming state fragments

To consume state fragments provided to the scenario's state, you must add needed arguments to step's methods using ScenarioStateArgument annotation. It can be used easily:, (*14)

  • inject argument from store with the exact same name: @ScenarioStateArgument("scenarioBanana") or @ScenarioStateArgument(name="scenarioBanana")
  • inject argument from store changing its name: @ScenarioStateArgument(name="scenarioBanana", argument="banana")
use Gorghoa\ScenarioStateBehatExtension\Annotation\ScenarioStateArgument;

/**
 * @When bonobo gives this banana to :monkey
 *
 * @ScenarioStateArgument("scenarioBanana")
 * @ScenarioStateArgument(name="scenarioBonobo", argument="bonobo")
 *
 * @param string $monkey
 * @param string $scenarioBanana
 * @param Bonobo $bonobo
 */
public function giveBananaToGorilla($monkey, $scenarioBanana, Bonobo $bonobo)
{
    // (note that PHPUnit is here only given as an example, feel free to use any asserter you want)
    \PHPUnit_Framework_Assert::assertEquals($monkey, 'gorilla');
    \PHPUnit_Framework_Assert::assertEquals($scenarioBanana, 'Yammy Banana');
    \PHPUnit_Framework_Assert::assertEquals($bonobo->getName(), 'Gerard');
}

Using state fragments in Behat hook methods

It's also possible to consume state fragments in hook methods: BeforeScenario & AfterScenario. And much better, the order is not important, you can set your arguments in any order you want:, (*15)

use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Gorghoa\ScenarioStateBehatExtension\Annotation\ScenarioStateArgument;

/**
 * @BeforeScenario
 *
 * @ScenarioStateArgument("scenarioBanana")
 *
 * @param string              $scenarioBanana
 * @param BeforeScenarioScope $scope
 */
public function checkBananaBeforeScenario($scenarioBanana, BeforeScenarioScope $scope)
{
    // (note that PHPUnit is here only given as an example, feel free to use any asserter you want)
    \PHPUnit_Framework_Assert::assertEquals($scenarioBanana, 'Yammy Banana');
    \PHPUnit_Framework_Assert::assertNotNull($scope);
}

/**
 * @AfterScenario
 *
 * @ScenarioStateArgument("scenarioBanana")
 *
 * @param string             $scenarioBanana
 * @param AfterScenarioScope $scope
 */
public function checkBananaAfterScenario($scenarioBanana, AfterScenarioScope $scope)
{
    // (note that PHPUnit is here only given as an example, feel free to use any asserter you want)
    \PHPUnit_Framework_Assert::assertEquals($scenarioBanana, 'Yammy Banana');
    \PHPUnit_Framework_Assert::assertNotNull($scope);
}

Why injecting state's fragments through method params

  1. Clear dependencies declaration for the step method
  2. Runtime checks by php: fail quickly if the argument is not present or does not match type hint
  3. The less verbose way of consuming shared scenario state

The Versions

06/06 2018

dev-master

9999999-dev

Scenario shared state extension for Behat

  Sources   Download

MIT

The Requires

 

The Development Requires

bdd behat stateless api testing

06/06 2018

v1.0.6

1.0.6.0

Scenario shared state extension for Behat

  Sources   Download

MIT

The Requires

 

The Development Requires

bdd behat stateless api testing

31/07 2017

v1.0.5

1.0.5.0

Scenario shared state extension for Behat

  Sources   Download

MIT

The Requires

 

The Development Requires

bdd behat stateless api testing

28/07 2017

dev-30-going-to-events

dev-30-going-to-events

Scenario shared state extension for Behat

  Sources   Download

MIT

The Requires

 

The Development Requires

bdd behat stateless api testing

27/07 2017

dev-research/30-decoupling

dev-research/30-decoupling

Scenario shared state extension for Behat

  Sources   Download

MIT

The Requires

 

The Development Requires

bdd behat stateless api testing

24/07 2017

v1.0.4

1.0.4.0

Scenario shared state extension for Behat

  Sources   Download

MIT

The Requires

 

The Development Requires

bdd behat stateless api testing

02/05 2017

v1.0.3

1.0.3.0

Scenario shared state extension for Behat

  Sources   Download

MIT

The Requires

 

The Development Requires

bdd behat stateless api testing

09/03 2017

v1.0.0

1.0.0.0

Scenario shared state extension for Behat

  Sources   Download

MIT

The Requires

 

The Development Requires

bdd behat stateless api testing

09/03 2017

dev-feat/composer-version

dev-feat/composer-version

Scenario shared state extension for Behat

  Sources   Download

MIT

The Requires

 

The Development Requires

bdd behat stateless api testing

15/11 2016

v1.0.0-rc.3

1.0.0.0-RC3

Scenario shared state extension for Behat

  Sources   Download

MIT

The Requires

 

The Development Requires

bdd behat stateless api testing

05/08 2016

v1.0.0-rc.2

1.0.0.0-RC2

Scenario shared state extension for Behat

  Sources   Download

MIT

The Requires

 

The Development Requires

bdd behat stateless api testing

05/08 2016

dev-fix/coding-standards--14

dev-fix/coding-standards--14

Scenario shared state extension for Behat

  Sources   Download

MIT

The Requires

 

The Development Requires

bdd behat stateless api testing

05/08 2016

dev-fix/readme-typo

dev-fix/readme-typo

Scenario shared state extension for Behat

  Sources   Download

MIT

The Requires

 

The Development Requires

bdd behat stateless api testing

05/08 2016

dev-feat/scrutinizr

dev-feat/scrutinizr

Scenario shared state extension for Behat

  Sources   Download

MIT

The Requires

 

The Development Requires

bdd behat stateless api testing

03/08 2016

dev-fix/updatecopyright

dev-fix/updatecopyright

Scenario shared state extension for Behat

  Sources   Download

MIT

The Requires

 

The Development Requires

bdd behat stateless api testing

31/07 2016

v1.0.0-rc.1

1.0.0.0-RC1

Scenario shared state extension for Behat

  Sources   Download

MIT

The Requires

 

The Development Requires

bdd behat stateless api testing

31/07 2016

dev-feat/update-readme

dev-feat/update-readme

Scenario shared state extension for Behat

  Sources   Download

MIT

The Requires

 

The Development Requires

bdd behat stateless api testing