GithubHelp home page GithubHelp logo

behat-tokenizer's Introduction

The Behat Tokenizer allows you to set and utilize tokens in your Behat test suite.

Installing Behat Tokenizer

Install the Behat tokenizer using Composer:

$> composer require --dev kerasai/behat-tokenizer

Configuring Behat Tokenizer

Add the TokenizerExtension class to the behat.yml file and tokens will be processed.

  extensions:
    Kerasai\Behat\Tokenizer\TokenizerExtension: { }

Using Behat Tokenizer in Contexts

To utilize the Behat Tokenizer in context classes, simple implement \Kerasai\Behat\Tokenizer\TokenizerAwareInterface and the context will have its ::setTokenizer method called with the tokenizer.

Additionally you may use \Kerasai\Behat\Tokenizer\TokenizerAwareTrait to easily implement the interface.

From there you may access the tokenizer within the context:

<?php

use \Kerasai\Behat\Tokenizer\TokenizerAwareInterface;
use \Kerasai\Behat\Tokenizer\TokenizerAwareTrait;

class MyContext implements TokenizerAwareInterface {
  
  use TokenizerAwareTrait;
  
  /**
   * @Given something named :arg1
   */
  public function AssertSomethingNamed($arg1) {
    $something = new Something();
    $something->setName($arg1);
    $something->save();
    $this->tokenizer->setToken("[something:$arg1]", $something->id());
  }
  
}

The token will be processed for all arguments within Behat features:

Feature: View a something
  In order see something
  As an visitor to the website
  I need to view pages with somthing

  Scenario: Something page
    Given something named "great_thing"
    When I am on "something/[something:great_thing]"
    Then I should see the heading "great_thing"

Custom Token Replacers

In addition to explicit token setting and replacement, custom replacer classes may be implemented to perform token replacement.

Replacer classes must implement \Kerasai\Behat\Tokenizer\Replacer\ReplacerInterface and may extend \Kerasai\Behat\Tokenizer\Replacer\ReplacerBase for convenience.

See \Kerasai\Behat\Tokenizer\Replacer\TimestampReplacer.

In order to utilize the custom replacer classes, they must be exposed to the Behat Tokenizer in the behat.yml configuration. In the simplest form, just the class name may be utilized.

  extensions:
    Kerasai\Behat\Tokenizer\TokenizerExtension:
      replacers:
        - Kerasai\Behat\Tokenizer\Replacer\TimestampReplacer:
            timezone: 'America/New_York'
        - Custom\Replacer\ReplacerNoConfig
        - Custom\Replacer\ReplacerWithConfig:
            config_option_1: abc
            config_option_2: def
        - special_key:
            class: Custom\Replacer\ReplacerHasSpecialKey
            config_option_1: abc
            config_option_2: def

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.