Undefined step reference in PhpStorm when using Codeception and Gherkin
I would like to use PhpStorm's Go To Declaration (Command + B on Mac) feature in Gherkin function files when using Codeception. However, PhpStorm doesn't seem to figure out where these steps are defined and prints this warning:
Undefined step reference: [...]
When I use Behat, PhpStorm understands where the stages are defined.
Steps to reproduce
-
mkdir codeception
-
cd codeception
-
composer require "codeception/codeception" --dev
-
./vendor/bin/codecept bootstrap
-
./vendor/bin/codecept generate:feature acceptance first
- Open your project directory in PhpStorm.
- Make sure PhpStorm knows Codeception is set:
- Make sure the PhpStorm plugins installed by Gherkin and the Codeception Framework are installed.
- Add a step to
tests/acceptance/first.feature
. -
./vendor/bin/codecept gherkin:snippets acceptance
The result is the following code. (Not all inclusive - 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()
. How can I fix this?
source to share
PhpStorm currently seems to use the Behat Context interface to determine which classes define the Gherkin step implementations in the .feature files , so a workaround for PhpStorm to detect the steps in the coding test is to add the interface Behat\Behat\Context\Context
somewhere in the original tree
/* Context.php */
namespace Behat\Behat\Context;
interface Context { }
and then implement AcceptanceTester
this interface (which is an empty marker interface)
class AcceptanceTester extends \Codeception\Actor implements Context ...
source to share
Build Roverwolf's answer .
Add this to the top of the AcceptanceTester.
namespace Behat\Behat\Context {
interface Context { }
}
Then it will use AcceptanceTester. Wrapping namespaces like this is a common PHP testing trick to spoof methods that exist in other namespaces.
namespace {
class AcceptanceTester extends \Codeception\Actor implements \Behat\Behat\Context\Context
}
}
source to share