Extensions to integrate Behat and PhpSpec
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs/images
features
spec/Extension/ErrorExtension/Observer
src/Extension Merge remote-tracking branch 'origin/fix-exemplify-tests' into move-t… Feb 19, 2017
.gitignore
CHANGELOG.md
LICENSE
README.md README: update namespace examples according to PSR-4 + info for phpsp… Feb 2, 2017
behat.yml.dist ExemplifyExtensions: move phpspec / behat tests to main repo Feb 19, 2017
composer.json
phpspec.yml Enable PhpSpecRunExtension in phpspec tests of main repo Feb 19, 2017

README.md

BehatSpec

Scrutinizer Code Quality Build Status

What

Integration between Behat and PhpSpec.

  • Creates specs for any missing classes encountered when running behat
  • Adds examples for any missing methods encountered when running behat
  • Executes the phpspec run command after this to add the described class/method

Why

This is useful when rather than using the Behat context to run an application through its UI, it is instead used to implement the domain model. For more information on this way of using Gherkin features to drive domain modelling read Everzet's post on Modelling by Example

When running a feature with a Context that contains a new class that does not exist or a method you will get a fatal error. If using PhpSpec the next step would be to start specifying that class using the describe command. This set of extensions provides integration between Behat and PhpSpec so that instead of a fatal error you can choose to run the describe command for the missing class automatically.

It also uses my ExemplifyExtension so that you can automatically add examples for missing methods in a similar way.

After describing a class or method in this way the phpspec run command can be used to automatically create the class or model. Since this is the typical next step in most cases, this extension automates that by asking you if you would like to do that next.

An Example

So to use an example from the blog post linked above:

/**
 * @Given a product named :name and priced £:price was added to the catalogue
 */
public function aProductNamedAndPricedWasAddedToTheCatalogue($name, Money $price)
{
    $aProduct = Product::namedAndPriced($name, $price);
    $this->catalogue->add($aProduct);
}

Running the feature relating to this context would result in a fatal exception normally. Instead you get a simple error and the offer to create a spec for the missing class:

Product not found

The spec is created followed by an offer to run phpspec run:

Spec created

Which asks to create the class:

Created Product?

Product created

Running Behat again will result in a similar process for the namedAndPriced method. Where an example for the method will be added to the spec and the method added to the class.

How

Installation

This package provides an extension for PhpSpec and an extension for Behat. Both need to be enabled for the full functionality.

Requires:

  • Behat 3.0+
  • PhpSpec 3.0+
  • PHP 5.6+

Require the extension:

$ composer require --dev rmiller/behat-spec:^0.5

In order to use BehatSpec with PHPSpec 2.0 series, use 0.3.* version series:

$ composer require --dev rmiller/behat-spec:0.3.*

To get the phpspec run command running, you need to use latest phpspec >2.1. Otherwise that functionality will silently fail.

Configuration

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

# behat.yml
# ...
extensions:
    RMiller\BehatSpec\Extension\BehatSpecExtension\BehatExtension:
        path:  bin/phpspec #default value is bin/phpspec
        config:  path/to/phpspec.yml #optional

Activate the PhpSpec extension by specifying its class in your phpspec.yml:

# phpspec.yml
extensions:
    RMiller\BehatSpec\Extension\BehatSpecExtension\PhpSpecExtension: ~

Additional configuration can be provided for the running of the phpspec run command:

It defaults to bin/phpspec for the path of phpspec and to run after the describe command. These can be overridden as follows:

# phpspec.yml
rerunner:
    path: vendor/bin/phpspec
    commands: [describe, exemplify, your_own_fancy_command]
    config: path/to/phpspec.yml #optional

extensions:
    RMiller\BehatSpec\Extension\BehatSpecExtension\PhpSpecExtension: ~

Some Details

This package pulls together some other PhpSpec and Behat extensions which can also be used standalone:

PhpSpec

Behat