使用 Codeception 和 Gherkin 时 PhpStorm 中未定义的步骤引用

Yng*_*eth 4 php gherkin phpstorm behat codeception

使用 Codeception 时,我希望能够在 Gherkin 功能文件中使用 PhpStorm 的“转到声明”功能(Mac 上的Command + B)。但是,PhpStorm 似乎没有弄清楚步骤在哪里定义,并输出以下警告:

未定义的步骤参考:[…] 警告截图

当我使用 Behat 时,PhpStorm 了解步骤的定义位置。

重现步骤

  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. 在 PhpStorm 中打开项目目录。
  7. 确保 PhpStorm 知道 Codeception 已安装: Codeception PhpStorm 配置
  8. 确保安装了 PhpStorm 插件GherkinCodeception Framework
  9. 添加一个步骤到tests/acceptance/first.feature.
  10. ./vendor/bin/codecept gherkin:snippets acceptance

这导致以下代码。(并非所有内容都包括在内——如果我需要添加任何内容,请告诉我。)

tests/acceptance/first.feature

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

  Scenario: try first
    When I visit "/"
Run Code Online (Sandbox Code Playgroud)

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,PhpStorm 不知道在哪里iVisit()。我怎样才能解决这个问题?

rov*_*olf 5

目前 PhpStorm 似乎使用 Behat Context 接口来确定哪些类定义了.feature文件中Gherkin 步骤的实现,因此让 PhpStorm 在代码接收测试器中找到步骤的解决方法是Behat\Behat\Context\Context在源代码树中的某处添加接口

/* Context.php */
namespace Behat\Behat\Context;

interface Context { }
Run Code Online (Sandbox Code Playgroud)

然后AcceptanceTester实现该接口(这是一个空的标记接口)

class AcceptanceTester extends \Codeception\Actor implements Context ...
Run Code Online (Sandbox Code Playgroud)