Skip to content

Codifico/ParameterBagExtension

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ParameterBagExtension

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

About

Behat ParameterBag Extension

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •