-
Notifications
You must be signed in to change notification settings - Fork 203
grab a field in a response and then use for a request #206
Comments
Intuitively, I’ll create a custom context based on |
That's more or less what i've got. /** } 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. |
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");
}
} |
I'm not sure this is working anymore. When I try to implement a context that extends RestContext I have this error :
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. |
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:
|
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
The text was updated successfully, but these errors were encountered: