Skip to content
This repository was archived by the owner on Dec 28, 2021. It is now read-only.

Step Definitions

olbrich edited this page Mar 23, 2011 · 5 revisions

During initialization of the cuke4php server, all php files in the features/step_definitions directory within your project will be loaded (after the contents of the features/support directory are loaded). All step definitions are available to all feature files during a run.

Structure

Cuke4php step definition files must be located within the features/step_definitions directory. They may be named in any way that makes sense for your project, but I suggest they be named something like 'ArbitraryNameSteps.php'. You may organize them into subdirectories of arbitrary depth if needed. PHP step definitions should also extend the 'CucumberSteps' class.

Steps

A step definition consists of two parts, the comment block and the function body. The comment block is parsed by cuke4php and utilizes some simple notations to associate a particular regular expression to a method call. For the most part, cucumber will suggest the formatting of a step definition the first time it encounters an unknown step definition, so it is easy to create them by simply cutting and pasting the suggested code into a file in the step definitions directory.

There are some simple rules to remember when defining steps.

  • the function call should start with 'step'
  • The regular expression in the generated docblock is used by cuke4php to determine which step to run for a step in a feature file.
  • Some versions of PHP are very particular about the formatting of the comment block. Try to use the generated one when possible.
  • The return value of a step is ignored.
  • Steps are considered to have failed if a PHPUnit assertion fails or if an exception is thrown.

Before and After hooks

Cucumber supports before and after hooks. These are step definitions that are run before or after other steps, and can be scoped to scenarios with particular tags. These hooks should be defined in a cucumber step file (as functions of a class derived from CucumberSteps). Hook functions should begin with 'before' or 'after'. The rest of the name does not matter so long as it is unique within the same step class.

function beforeAll() {
  // run before all scenarios
}

If the function has a comment block containing one or more tags, then the hook will only run before or after scenarios tagged with the same tag.

/**
 * @wire
 */
function beforeWire() {
    // only run before scenarios tagged with @wire
}

Cuke4php does not currently provide any special support for 'Around', 'AfterStep', and 'AfterConfiguration' hooks, although they will probably still work if they are implemented in ruby and don't require direct communication with the php code.

Passing data between steps

To facilitate passing data around between feature steps, there is an array available to all step definitions. This allows storage and retrieval of chunks of information that may be used in subsequent steps.

function beforeSaveTime() {
    // save current time into the array before every scenario
    $this->aGlobals['time'] = time();
    // or use magic getters and setters for convenience
    $this->time = time();
    var_dump($this->time);
    // check to see if a value has been set
    isset($this->time);
};

The array is cleared out at the beginning of every scenario.

Example


/**
 * @package Cuke4Php
 */
 
/**
 * @package Cuke4Php
 */ 
class TestSteps extends CucumberSteps {

    /**
     * Given /^successful$/
     **/
    public function stepSuccessful() {}

    /**
     * Given /^incomplete$/
     */
    public function stepIncomplete() {
        self::markTestIncomplete('incomplete');
    }

    /**
     * Given /^skipped$/
     */
    public function stepSkipped() {
        self::markTestSkipped('skipped');
    }

    /**
     * Given /^pending$/
     */
    public function stepPending() {
        self::markPending('pending');
    }

    /**
     * Given /^a failed expectation$/
     */
    public function stepFailed() {
        self::assertEquals(true, false);
    }

    /**
     * Given /^an exception is thrown$/
     */
    public function stepException() {
        throw new TestException('Exception');
    }

    /**
     * Given /^"arg1" not equal to "arg2"$/
     */
    public function stepNotEqual($arg1,$arg2) {
        self::assertTrue($arg1 !== $arg2);
    }

}

Clone this wiki locally