Behat / Behat

BDD in PHP
http://behat.org
MIT License
3.91k stars 609 forks source link

Generate snippets with Attributes instead of Annotations #1379

Open rpkamp opened 3 years ago

rpkamp commented 3 years ago

Behat has full support for PHP 8 Attributes since version 3.10.0.

For example, this means that instead of

/**
 * @Given something
 */
public function something() {}

it is now possible to write

#[Given('something')]
public function something() {}

and the same goes for When, Then and all hooks (BeforeScenario et al).

However, when generating snippets1 Behat will use Annotations instead of Attributes.

Behat should be able to generate snippets using Attributes instead of Annotions when applicable (ie on PHP >= 8). Note that when generating a snippet with an Attribute that Attribute should also be used in the class where it's added, otherwise it won't work.

Since Behat supports PHP7.2+ at the time of writing it still needs to support generating snippets with Annotations, but it could default to Attributes when PHP >=8 is detected for example. This could also be an option, but I'm unsure if that is worth it. It feels like something that would be OK te enforce, since the entire ecosystem is moving away from Annotations to Attributes anyway. Curently features/snippets.feature tests for output with Annotations. Those scenarios could be run with a new tag @php7 similar to the already existing @php8 to prevent them from being checked on PHP8. And then the same scenarios but updated using attributes could be added with @php8 tags.

1 A snippet can be generated when at the end of a run Behat has detected undefined steps. It can add a skeleton of the step to a feature context.

ciaranmcnulty commented 3 years ago

There is an existing --snippets-type CLI option that hints what type of snippets to generate. We could easily add Annotations as a supported type, the question would be how to handle defaulting

rpkamp commented 3 years ago

I looked into that. --snippets-type is for regex vs turnip. Annotations and attributes seem orthogonal to those options.

Maybe --snippets-syntax with options annotations and attributes? And when not supplied, default depends on PHP version, i.e. attributes for PHP >= 8 and annotations for PHP < 8?

rpkamp commented 2 years ago

I think the smart thing for now would be to hold off on implementing this until PHPStorm supports Attributes for Behat.

Right now the DX is absolutely terrible, all the nice things you could do with Behat (find usages, ctrl+click to jump to definition from gherkin, etc) don't work with Attributes.

I did create an issue for this in Youtrack: https://youtrack.jetbrains.com/issue/WI-63963

pieterocp commented 1 year ago

This mostly works in later versions of PHPStorm (still falls over a bit at times), so we might want to add this snippet generation in.