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

grab a field in a response and then use for a request #206

Open
coulon-xyz opened this issue Mar 24, 2017 · 5 comments
Open

grab a field in a response and then use for a request #206

coulon-xyz opened this issue Mar 24, 2017 · 5 comments
Labels

Comments

@coulon-xyz
Copy link

Hi,

I would like to test end-to-end functionality of my API.
Is there any one i can grab the ID of an an entity i've just created, and then use this ID to patch this entity.
POST /location
grab location id in node data[0].id
and then use this id to test
GET /location/{{id}}

I can do that easily with postman's runner but it's not as good as behatch to do functional testing...

anyone ?

Julien

@sanpii
Copy link
Member

sanpii commented Mar 24, 2017

Intuitively, I’ll create a custom context based on Behatch\Contex\RestContext with a method calls getTheLastCreatedLocation and use $this->request->getContent() to retreive the last response content.

@coulon-xyz
Copy link
Author

That's more or less what i've got.
class CustomJsonContext extends JsonContext
{
var $portfolioId;

/**
* @then I grab the value of the portfolioId
*/
public function iGrabTheValueOfThePortfolioid()
{
$json = $this->getJson();
$this->portfolioId = $this->inspector->evaluate($json, $node);
}

}
}

But now, I would like to be able to use this $portfolioId in an CustomRestContext class. I could write the variable in a file and read it, but that's a bit ugly.
There is probably a easy solution to this. I'm not a very good PHP dev...

@sanpii
Copy link
Member

sanpii commented Mar 24, 2017

class FeatureContext extends \Behatch\Context\RestContext
{
    public function iGrabTheValueOfThePortfolioid()
    {
        $json = json_decode($this->request->getContent());
        $id = $json->data[0]->id;
        $location = $this->iSendARequestTo('GET', "/location/$id");
    }
}

@brice
Copy link

brice commented Sep 9, 2019

I'm not sure this is working anymore. When I try to implement a context that extends RestContext I have this error :

Step "I send a :method request to :url" is already defined in

I just tried to acces to another context, using this method : http://behat.org/en/latest/cookbooks/accessing_contexts_from_each_other.html

But as $request is protected in BehatchContext it's impossible to get it.

@Nightbr
Copy link

Nightbr commented Jul 17, 2020

Hi here is my solution:

<?php

declare(strict_types=1);

namespace Cnty\CommonBundle\TestHelper\Behat;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behatch\Context\JsonContext;
use Behatch\HttpCall\HttpCallResultPool;

class CntyJsonContext extends JsonContext
{
    /**
     * @var \Behatch\Context\RestContext
     */
    private $restContext;

    public function __construct(HttpCallResultPool $httpCallResultPool, $evaluationMode = 'javascript')
    {
        parent::__construct($httpCallResultPool, $evaluationMode);
    }

    /** @BeforeScenario */
    public function gatherContexts(BeforeScenarioScope $scope)
    {
        $environment = $scope->getEnvironment();
        try {
            $this->restContext = $environment->getContext('Behatch\Context\RestContext');
        } catch (\Exception $e) {
            // no such context, probably not registered in behat.yml
        }
    }

    /**
     * @When /^I send a "([^"]*)" project request to "([^"]*)" where id is "([^"]*)" from last request$/
     */
    public function iSendARequestWithIdFormLastRequest($method, $url, $node)
    {
        $json      = $this->getJson();
        $id        = $this->inspector->evaluate($json, $node);
        $urlWithId = str_replace('$id', $id, $url);
        $this->restContext->iAddHeaderEqualTo('Accept', 'application/ld+json');
        $this->restContext->iAddHeaderEqualTo('Content-Type', 'application/ld+json');
        $this->restContext->iSendARequestTo($method, $urlWithId);
    }
}

Usage:

    Given I am authenticated as "[email protected]"
    When I send a "GET" project request to "/api/reviews/aa329a49-04ca-4bc7-a4de-915b30f3a18e/corrections"
    Then the response status code should be 200
    Then the JSON node "hydra:member" should have 1 element
    And the JSON node "hydra:member[0].review" should be equal to "/api/reviews/aa329a49-04ca-4bc7-a4de-915b30f3a18e"
    Given I am authenticated as "[email protected]"
    When I send a "GET" project request to "/api/corrections/$id/correction_items" where id is "hydra:member[0].id" from last request
    Then the response status code should be 200
    And print last JSON response
    And the JSON node "hydra:member" should have 1 element
    And the JSON node "hydra:member[0].comments" should have 1 element
    And the JSON node "hydra:member[0].comments[0].comment" should be equal to "this is a test"
    And the JSON node "hydra:member[0].tag.id" should be equal to "9948f0f3-2afd-4e82-b07d-138ad26faaa8"

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

No branches or pull requests

4 participants