PHPackages                             gorghoa/scenariostate-behat-extension - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. gorghoa/scenariostate-behat-extension

AbandonedArchivedBehat-extension[Testing &amp; Quality](/categories/testing)

gorghoa/scenariostate-behat-extension
=====================================

Scenario shared state extension for Behat

v1.0.7(5y ago)34605.9k↓27.8%7[2 PRs](https://github.com/gorghoa/ScenarioStateBehatExtension/pulls)1MITPHPPHP &gt;=5.5

Since Jul 31Pushed 3y ago3 watchersCompare

[ Source](https://github.com/gorghoa/ScenarioStateBehatExtension)[ Packagist](https://packagist.org/packages/gorghoa/scenariostate-behat-extension)[ RSS](/packages/gorghoa-scenariostate-behat-extension/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (20)Used By (1)

ScenarioStateBehatExtension
===========================

[](#scenariostatebehatextension)

[![Build Status](https://camo.githubusercontent.com/b701eb1431d49581eb69edd01cb0eb041d0e3bd1fb5323c99247489e6e6a19a0/68747470733a2f2f7472617669732d63692e6f72672f676f7267686f612f5363656e6172696f53746174654265686174457874656e73696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/gorghoa/ScenarioStateBehatExtension)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/0e11a7061a62aa2b9cbebf4abafb6e47e07dffe66c0bddd798074c2908d29d55/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f676f7267686f612f5363656e6172696f53746174654265686174457874656e73696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/gorghoa/ScenarioStateBehatExtension/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/520cc4eecace8a60c26a40ebc453781143e7a860729d2b4d822276cac43df54e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f676f7267686f612f5363656e6172696f53746174654265686174457874656e73696f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/gorghoa/ScenarioStateBehatExtension/?branch=master)

⚠️ This projet is not maintained anymore. Still, anyone interested to take over is welcome to do so :).

When to use
-----------

[](#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.

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.).

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.

Installation
------------

[](#installation)

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

Then update your project's `behat.yml` config file by loading the extension:

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

Usage
-----

[](#usage)

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

Each scenario get it's own isolated and unique state.

Let's say a feature like this:

```
    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.

### Provide state fragment

[](#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.

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.

```
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.

```
/**
 * @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

[](#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:

- 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

[](#using-state-fragments-in-behat-hook-methods)

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

```
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
-----------------------------------------------------

[](#why-injecting-states-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

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity44

Moderate usage in the ecosystem

Community21

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 65.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~191 days

Recently: every ~313 days

Total

9

Last Release

2050d ago

PHP version history (2 changes)v1.0.0-rc.1PHP &gt;=7.0.0

v1.0.0-rc.2PHP &gt;=5.5

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/407859?v=4)[Vincent](/maintainers/vincentchalamon)[@vincentchalamon](https://github.com/vincentchalamon)

![](https://avatars.githubusercontent.com/u/662768?v=4)[Rodrigue Villetard](/maintainers/gorghoa)[@gorghoa](https://github.com/gorghoa)

---

Top Contributors

[![vincentchalamon](https://avatars.githubusercontent.com/u/407859?v=4)](https://github.com/vincentchalamon "vincentchalamon (17 commits)")[![walterdolce](https://avatars.githubusercontent.com/u/6195629?v=4)](https://github.com/walterdolce "walterdolce (5 commits)")[![Simperfit](https://avatars.githubusercontent.com/u/3451634?v=4)](https://github.com/Simperfit "Simperfit (2 commits)")[![alanpoulain](https://avatars.githubusercontent.com/u/10920253?v=4)](https://github.com/alanpoulain "alanpoulain (1 commits)")[![magarzon](https://avatars.githubusercontent.com/u/351039?v=4)](https://github.com/magarzon "magarzon (1 commits)")

---

Tags

bddbehatbehat-extensionphpBDDBehatstateless api testing

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/gorghoa-scenariostate-behat-extension/health.svg)

```
[![Health](https://phpackages.com/badges/gorghoa-scenariostate-behat-extension/health.svg)](https://phpackages.com/packages/gorghoa-scenariostate-behat-extension)
```

###  Alternatives

[polishsymfonycommunity/symfony-mocker-container

Provides base Symfony dependency injection container enabling service mocking.

1468.0M237](/packages/polishsymfonycommunity-symfony-mocker-container)[dvdoug/behat-code-coverage

Generate Code Coverage reports for Behat tests

593.6M37](/packages/dvdoug-behat-code-coverage)[sensiolabs/behat-page-object-extension

Page object extension for Behat

1166.5M27](/packages/sensiolabs-behat-page-object-extension)[dmarynicz/behat-parallel-extension

Parallel extension for Behat

27544.3k](/packages/dmarynicz-behat-parallel-extension)[leanphp/behat-code-coverage

Generate Code Coverage reports for Behat tests

50359.8k2](/packages/leanphp-behat-code-coverage)[shvetsgroup/parallelrunner

Parallel runner extension for Behat

74143.5k](/packages/shvetsgroup-parallelrunner)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
