ulff / BehatRestApiExtension

Behat context for testing REST API calls
MIT License
10 stars 1 forks source link

Problem with the parameterBag in RestApiContext #16

Open ockam opened 7 years ago

ockam commented 7 years ago

I’m using your package in a Laravel 5.4 project and this is my first experience with Behat.

When running Behat, I get the followiing error:

Fatal error: Call to a member function replace() on null (Behat\Testwork\Call\Exception\FatalThrowableError)

I trace it back to the extractFromParameterBag method in RestApiContext. $this->getParameterBag() is null. If I plainly return the string (commenting out line 688), it works fine.

Am I missing a configuration?

Here’s by behat.yml:

default:
    extensions:
        Laracasts\Behat:
            # env_path: .env.behat
        Behat\MinkExtension:
            default_session: laravel
            laravel: ~
        Ulff\BehatRestApiExtension\ServiceContainer\BehatRestApiExtension: ~

My feature:

Feature: Programs

    In order to use the API for programs
    As a user
    I need to get data in JSON

    Scenario: List all programs
        When I make request "GET" "/api/v1/programs"
        Then the response status code should be 200
        And the response JSON "data" field should be a collection

And my FeatureContext:

<?php

use Ulff\BehatRestApiExtension\Context\RestApiContext;

class FeatureContext extends RestApiContext
{
    public function __construct()
    {
    }

}
ulff commented 7 years ago

@ockam thanks for your message. I've looked briefly into your code snippets. At first glance I can't see the problem. The configuration looks ok it's enough for the extension to start working, the definition of your FeatureContext should also be enough.

I only wonder if the codifico/parameter-bag-extension was installed correctly, especially if you wrote that it's returning null. Could you somehow make sure that extension works correct in your environment?

ockam commented 7 years ago

The extension is installed correctly. I tried loading it directly in my behat.yml extensions but that didn’t work.

Not sure I understand how it is initialized in the first place. Where should I look?

ulff commented 7 years ago

I've taken a look into my project using that extension and in behat.yml I have also ParameterBagExtension loaded, that like:

extensions:
    // ...
    Codifico\ParameterBagExtension\ServiceContainer\ParameterBagExtension:
      parameter_bag:
        class: Codifico\ParameterBagExtension\Bag\InMemoryPlaceholderBag
    // ...

You can also try adding it. You could add it my way, or the way described on that extension's documentation:

extensions:
    // ...
    Codifico\ParameterBagExtension\ServiceContainer\ParameterBagExtension: ~
    // ...

Please inform if it helped.

ockam commented 7 years ago

I tried both, before and after the call to your extension, and still no luck.

I also tried to dd inside ParameterBagAwareInitializer and that does nothing (like it’s never triggered).

ockam commented 7 years ago

OK. I’m making some progress: ParameterBagAwareInitializer initializeContext fails on this condition:

        if (!$context instanceof ParameterBagAwareContext && !$this->usesParameterBag($context)) {
            return;
        }