Search code examples
phpphpstormcodeceptionbehatgherkin

Undefined step reference in PhpStorm when using Codeception and Gherkin


I'd like to be able to use PhpStorm's "Go To Declaration" feature (Command + B on a Mac) in Gherkin feature files when using Codeception. However, PhpStorm doesn't seem to figure out where the steps are defined, and outputs this warning:

Undefined step reference: […] Warning screenshot

When I'm using Behat, PhpStorm understands where the steps are defined.

Steps to reproduce

  1. mkdir codeception
  2. cd codeception
  3. composer require "codeception/codeception" --dev
  4. ./vendor/bin/codecept bootstrap
  5. ./vendor/bin/codecept generate:feature acceptance first
  6. Open the project directory in PhpStorm.
  7. Make sure that PhpStorm knows that Codeception is installed: Codeception PhpStorm configuration
  8. Make sure that the PhpStorm plugins Gherkin and Codeception Framework are installed.
  9. Add a step to tests/acceptance/first.feature.
  10. ./vendor/bin/codecept gherkin:snippets acceptance

This results in the following code. (Not everything is included – let me know if I need to add anything.)

tests/acceptance/first.feature:

Feature: first
  In order to ...
  As a ...
  I need to ...

  Scenario: try first
    When I visit "/"

tests/_support/AcceptanceTester.php:

<?php

/**
 * Inherited Methods
 * @method void wantToTest($text)
 * @method void wantTo($text)
 * @method void execute($callable)
 * @method void expectTo($prediction)
 * @method void expect($prediction)
 * @method void amGoingTo($argumentation)
 * @method void am($role)
 * @method void lookForwardTo($achieveValue)
 * @method void comment($description)
 * @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL)
 *
 * @SuppressWarnings(PHPMD)
*/
class AcceptanceTester extends \Codeception\Actor
{
    use _generated\AcceptanceTesterActions;

   /**
    * Define custom actions here
    */

    /**
     * @When I visit :arg1
     */
    public function iVisit($arg1)
    {
        throw new \Codeception\Exception\Incomplete("Step `I visit :arg1` is not defined");
    }
}

However, PhpStorm doesn't know where iVisit() is. How can I fix this?


Solution

  • Currently PhpStorm seems to use the Behat Context interface to determine which classes define implementations for the Gherkin steps in .feature files, so a workaround to have PhpStorm find the steps in the codeception tester is to add the Behat\Behat\Context\Context interface somewhere in your source tree

    /* Context.php */
    namespace Behat\Behat\Context;
    
    interface Context { }
    

    and then have the AcceptanceTester implement that interface (which is an empty marker interface)

    class AcceptanceTester extends \Codeception\Actor implements Context ...