Find unused and duplicated definitions easily – without running Behat tests.
composer require behastan/behastan --devvendor/bin/behastan analyse testsDo you want to skip some rule? You can:
vendor/bin/behastan analyse tests --skip=<rule-identifier>
# e.g.
vendor/bin/behastan analyse tests --skip=duplicated-contentsHere are the available rules:
Some definitions have similar masks, even identical contents:
use Behat\Step\When;
#[When('load a user profile')]
public function loadAUserProfile()
{
$this->loadRoute('/user-profile');
}
#[When('load user profile')]
public function loadUserProfile()
{
$this->loadRoute('/user-profile');
}Better use a one definition with single mask, to make your tests more precise and easier to maintain.
Same as services, there should be unique definition masks:
use Behat\Step\When;
#[When('load homepage')]
public function loadUserProfile()
{
$this->loadRoute('/homepage');
}
#[When('load homepage')]
public function loadUserProfile()
{
$this->loadRoute('/homepage/default');
}Make them unique with different behavior, or merge them and use one definition instead.
Behat uses @When(), @Then() and @Given() annotations or attributes to define a class method that is called in *.feature files. Sometimes test change and lines from *.feature files are deleted. But what about definitions?
# some *.feature file
Scenario: Load admin dashboard
- When load admin dashboard
+ When load homepage↓
use Behat\Step\When;
#[When('load admin dashboard')]
public function loadAdminDashboard()
{
$this->loadRoute('/admin/dashboard');
}This rule spots definitions that are no longer needed, so you can remove them.
Protip: Add this command to your CI, to get instant feedback of any changes in every pull-request.
That's it!
Happy coding!