codifico/parameter-bag-extension

Parameter bag extension for Behat

Installs: 286 654

Dependents: 2

Suggesters: 0

Security: 0

Stars: 7

Watchers: 2

Forks: 5

Open Issues: 0

Type:behat-extension

dev-master 2015-09-02 14:09 UTC

This package is not auto-updated.

Last update: 2025-01-18 18:00:16 UTC


README

for Behat 3.x

Build Status Scrutinizer Code Quality SensioLabsInsight

Latest Stable Version Latest Unstable Version License Total Downloads

Provides parameter bag for Behat contexts:

  • ParameterBagAwareContext provides an parameter bag instance for contexts

Instalation

php composer.phar require codifico/parameter-bag-extension:dev-master --dev

Activate extension by specifying its class in your behat.yml:

# behat.yml
default:
    # ...
    extensions:
        Codifico\ParameterBagExtension\ServiceContainer\ParameterBagExtension: ~

Parameter Bag Usage

Prepare parameter:

<?php

use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Codifico\ParameterBagExtension\Context\ParameterBagDictionary;

class FeatureContext implements SnippetAcceptingContext
{
    use ParameterBagDictionary;

    /**
     * @Given Entity :entityName exists:
     */
    public function entityExists($entityName)
    {
        // ... create entity
        $this->getParameterBag()->set($entityName, $entity);
    }
}

Use the parameter:

<?php

use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Codifico\ParameterBagExtension\Context\ParameterBagDictionary;

class AnotherFeatureContext implements SnippetAcceptingContext
{
    use ParameterBagDictionary;

    /**
     * @Then I need entity :entityName
     */
    public function iNeedEntity($entityName)
    {
        $entity = $this->getParameterBag()->get($entityName);
    }
}

Placeholder Bag Usage

You can also use it as a placeholder bag. To switch to a placeholder bag just

 # behat.yml
 default:
     # ...
     extensions:
         Codifico\ParameterBagExtension\ServiceContainer\ParameterBagExtension:
            parameter_bag:
                class: Codifico\ParameterBagExtension\Bag\InMemoryPlaceholderBag

Replacing placeholders

Additionally to setting and getting placeholder values you can replace placeholders in strings

<?php

class AnotherFeatureContext implements SnippetAcceptingContext
{
    use ParameterBagDictionary;

    /**
     * @Then I should get :message
     */
    public function iShouldGet($message)
    {
        /*
         * let's assume that
         * $message = 'User USER_ID is active'
         * and placeholder bag contains value 123 under key USER_ID
         */
        $message = $this->getParameterBag()->replace($message)

        // $message = 'User 123 is active'
    }
}

Copyright

Copyright (c) 2014 Marcin Dryka (drymek). See LICENSE for details.

Thanks

Extension is based on a solution developed by Przemysław Piechota (kibao) in gist.

Contributors