PHPackages                             rocketage/behat-shortcode-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. rocketage/behat-shortcode-extension

ActiveLibrary[Testing &amp; Quality](/categories/testing)

rocketage/behat-shortcode-extension
===================================

A Behat extension that allows the use of shortcodes in step strings, tables and pystrings to translate runtime variables into feature files, eg dates.

0.1.0(9y ago)017MITPHPPHP &gt;=5.4.0

Since Jul 5Pushed 9y ago1 watchersCompare

[ Source](https://github.com/rocketage/behat_shortcode_extension)[ Packagist](https://packagist.org/packages/rocketage/behat-shortcode-extension)[ Docs](https://github.com/rocketage/behat_shortcode_extension)[ RSS](/packages/rocketage-behat-shortcode-extension/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (2)Versions (2)Used By (0)

Behat Shortcode Extension
=========================

[](#behat-shortcode-extension)

Description
-----------

[](#description)

This extension was created to provide a transparent way to feed runtime values into a feature.

For example my last use-case was verifying a sitemap:

```
  Scenario: Sitemap contains homepage
    When the sitemap is rendered
    Then the sitemap file contains:
      |https://example.com/[date type=now format=Y-m-d]daily1.0|

```

The extension will replace the shortcode `[date type=now format=Y-m-d]`with the current date so the step code can perform a direct comparison.

Considerations
--------------

[](#considerations)

You should always seek to avoid having to use this extension!

Firstly, it should be possible to abstract runtime values into the step methods directly, without exposing them in the feature. The above example could easily be rewritten as:

```
  Scenario: Sitemap contains homepage
    When the sitemap is rendered
    Then the sitemap file contains:
      | url                  | frequency | priority |
      | https://example.com/ | daily     | 1.0      |

```

Secondly runtimes values should be sought to be fixed by utilising doubles or mock services for your tests. Then runtime variables can be set in your 'given' steps:

```
  Scenario: Sitemap contains homepage
    Given the date is "2016-07-03"
    When the sitemap is rendered
    Then the sitemap file contains:
      |https://example.com/2016-07-03daily1.0|

```

Finally this extension uses reflection to inject the shortcode before the step method is called, which always feels wrong.

However, sometimes it's not always possible or practical to follow the perfect path and it can on occasions make features easier to read and step methods cleaner. Particularly when used in tables.

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

[](#installation)

Install via composer:

```
composer require --dev rocketage/behat-shortcode-extension

```

Then add the extension to your behat.yml file:

```
default:
  suites:
    default:
      contexts:
        - FeatureContext
  extensions:
    Rocketage\Behat\ShortcodeExtension:

```

Configuration
-------------

[](#configuration)

The extension currently only ships with a datetime shortcode, which effectively wraps the DateTime php class. The shortcode uses the following format `[date type=now format=Y-m-d zone=UTC]`. Because it uses DateTime you can use all the friendly contructor strings in the `type`field like 'yesterday', 'last week', 'next year', etc.

You can add more shortcodes by supplying classnames in the behat.yml file:

```
default:
  suites:
    default:
      contexts:
        - FeatureContext
  extensions:
    Rocketage\Behat\ShortcodeExtension:
      shortcodes:
        - MyProject\CustomShortcode1
        - MyProject\CustomShortcode2

```

The custom classes must return a SimpleShortCode instance via the \_\_invoke magic method:

```
