4

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?

Yngve Høiseth
  • 570
  • 6
  • 26

4 Answers4

6

Not supported yet, please vote: https://youtrack.jetbrains.com/issue/WI-34963

Eugene Morozov
  • 2,790
  • 1
  • 10
  • 17
5

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 ...
roverwolf
  • 451
  • 3
  • 4
2

Building off Roverwolf's answer.

Add this to the top of the AcceptanceTester file.

namespace Behat\Behat\Context { 
   interface Context { } 
}

Then have AcceptanceTester implement that. Wrapping namespaces like this is a common trick in PHP testing to fake methods that exist in other namespaces.

namespace {
    class AcceptanceTester extends \Codeception\Actor implements \Behat\Behat\Context\Context
    }
}
Michael Ryan Soileau
  • 1,763
  • 17
  • 28
0

I have another step definition file, and I use Behat's Context. Then implements Context in the class declaration.

use Behat\Behat\Context;

class myClassSteps implements Context\Context
{
     step definitions
}

It works for me.

Letter
  • 1