Skip to content
This repository has been archived by the owner on Apr 20, 2021. It is now read-only.

How to access RestContext from my custom context? #272

Closed
tsantos84 opened this issue Feb 18, 2019 · 5 comments
Closed

How to access RestContext from my custom context? #272

tsantos84 opened this issue Feb 18, 2019 · 5 comments

Comments

@tsantos84
Copy link

I need to write a custom step on my custom context to add a header in the request. Is it possible? I've followed the official way to do so without success.

http://behat.org/en/latest/cookbooks/accessing_contexts_from_each_other.html

The error says that I can't call the private getContext method from InitializedSymfonyExtensionEnvironment class.

My code:

/** @BeforeScenario */
public function gatherContexts(BeforeScenarioScope $scope)
{
    $environment = $scope->getEnvironment();
    $this->restContext = $environment->getContext(RestContext::class);
}

Any idea?

@OskarStark
Copy link
Contributor

You can try it by using gatherContexts() method

I am using it like this:
https://github.com/OskarStark/SonataAdminBehatContext/blob/master/src/Context/SonataAdminContext.php

@tsantos84
Copy link
Author

I've tried this snippet but I still getting the calling private method exception.

@OskarStark
Copy link
Contributor

Can you share your behat.yaml please?

@PWalkow
Copy link

PWalkow commented Dec 3, 2019

@tsantos84 have you tried something like that?

use Behat\Behat\Hook\Scope\BeforeScenarioScope;

class YourContext implements Context {

(...)

    /**
     * @BeforeScenario
     */
    public function gatherContext(BeforeScenarioScope $beforeSuiteScope)
    {
        $env = $beforeSuiteScope->getEnvironment();
        $this->restContext = $env->getContext(RestContext::class);
    }

@tsantos84
Copy link
Author

Hi guys. Sorry for the very long reply. I think my initial issue were with some symfony/behat/behatch version. I've just make a fresh install and now I can access Behatch context from my custom one.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants