user2076907
user2076907

Reputation: 11

Behat+symfony2 access container parameters set in custom extension

(I'm going to explain situation, incase someone knows of a better way to accomplice what I want to do).

Using Symfony2 + Behat + Symfony2Extension + Mink +

We have an application with multiple urls that will be visited during scenarios.

I do understand that you use the parameters sent in from the FeatureContext __construct method, but what I'm trying to do is set up the urls in the behat.yml file so that we can use them in our custom Context to visit the urls.

Looking at how the extensions work I have setup the dependency injection as follows:

class Extension implements ExtensionInterface
{

    /**
     * Loads a specific configuration.
     *
     * @param array            $config    Extension configuration hash (from behat.yml)
     * @param ContainerBuilder $container ContainerBuilder instance
     */
    public function load(array $config, ContainerBuilder $container)
    {
        $container->setParameter('url_one', $config['url_one']);
        $container->setParameter('url_two', $config['url_two']);

    }

    /**
     * Setups configuration for current extension.
     *
     * @param ArrayNodeDefinition $builder
     */
    public function getConfig(ArrayNodeDefinition $builder)
    {
        $builder->
            children()->
                scalarNode('one_url')->
                isRequired()->
                end()->
                scalarNode('two_url')->
                isRequired()->
                end()->
            end();
    }  
 code continues....

And my behat.yml looks like this:

default:
    extensions:
        Behat\MinkExtension\Extension:
            goutte: ~
            selenium2: ~
        Behat\Symfony2Extension\Extension: ~
        Acme\AcmeExtension\Extension:
            url_one: 'http://example1.com'
            url_two: 'http:/example2.com'

Now in my FeatureContext.php class I would like to do the following:

        $url = $kernel->getContainer()->getParameter('url_one');

But this is not working, it is returning parameters from my Symfony2 application, which is expected since I have symfony2extension enabled. But I can not access the parameters or services from the extension class.

(Please note that if I'm in the Extension class in the load method and I call the parameter I just set it returns it, so I know it is set, but it must be set to a different container?)

First off is this possible? And if so what should I do to make it work.

Many thanks for any help.

Upvotes: 1

Views: 3066

Answers (1)

everzet
everzet

Reputation: 1745

Obviously, $kernel->getContainer() returns you container of the Symfony2 app kernel. Those kernel and container are not shared with Behat. Behat has its own container instance, which it uses to manage own services. Which means that extension is setting parameters inside Behat container, but you are attempting to access your app kernel container. That's why you have different results.

Now, the question is, how would you pass some value from your extension to context class. Answer is context initialiser. Check out:

Upvotes: 4

Related Questions