Last active
December 12, 2025 13:00
-
-
Save ziadoz/70c46d63f6055324309cb31959b346cf to your computer and use it in GitHub Desktop.
Pest - Gherkin Formatted Test Names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function feature(string $feature, callable $callable): void | |
| { | |
| group('Feature: ' . $feature, $callable); // Call Pest's group() method with the feature part of the Gherkin string. | |
| } | |
| function given(string $given) | |
| { | |
| return new class($given) implements Stringable { | |
| private array $gherkin = []; | |
| public function __construct(string $given) | |
| { | |
| $this->gherkin[] = 'Given: ' . $given; | |
| } | |
| public function and(string $and): self | |
| { | |
| $this->gherkin[] = ' And: ' . $and; | |
| return $this; | |
| } | |
| public function when(string $when): self | |
| { | |
| $this->gherkin[] = ' When: ' . $when; | |
| return $this; | |
| } | |
| public function then(string $then): self | |
| { | |
| $this->gherkin[] = ' Then: ' . $then; | |
| return $this; | |
| } | |
| public function test(callable $callable): void | |
| { | |
| test($this->__toString(), $callable); // Call Pest's test()/it() method with the Gherkin string. | |
| } | |
| public function __toString() | |
| { | |
| return implode("\n", $this->gherkin); | |
| } | |
| }; | |
| } | |
| feature('User Login', function () { | |
| given('A user is logged in') | |
| ->and('The user has admin privileges') | |
| ->and('The user navigates to the admin page') | |
| ->when('The user clicks on the dashboard link') | |
| ->then('The user can access the admin dashboard') | |
| ->test(function () { | |
| // Do some testing... | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment