Behat / docs

Behat documentation repository
http://behat.org
28 stars 69 forks source link

`$environment->getContext` triggers the Psalm issue UndefinedInterfaceMethod #141

Open Kwadz opened 3 years ago

Kwadz commented 3 years ago

The example about accessing contexts from each other triggers:

Indeed, the method getContext() is not necessarily present in every possible Environment interface implementation since it is not declared in this interface. Actually it is declared in InitializedContextEnvironment only.

The piece of code:

use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;

class FeatureContext implements Context
{
    /** @var \Behat\MinkExtension\Context\MinkContext */
    private $minkContext;

    /** @BeforeScenario */
    public function gatherContexts(BeforeScenarioScope $scope)
    {
        $environment = $scope->getEnvironment();

        $this->minkContext = $environment->getContext('Behat\MinkExtension\Context\MinkContext');
    }
}
stof commented 3 years ago

This might be solved by improving types based on the different scopes. The BeforeScenarioScope is guaranteed to get an environment implementing Behat\Behat\Context\Environment\ContextEnvironment, and I think even an InitializedContextEnvironment for that one (for BeforeSuite, that might not be an InitializedContextEnvironment, but it would still be a ContextEnvironment)

pamil commented 2 years ago

getContext public method is available only in specific ContextEnvironment implementations as it's not listed on the interface. FriendsOfBehat/SymfonyExtension implements that interface as well and provides getContext method additionally, but there's still no guarantee any other implementation will have that method.