Skip to content

Commit

Permalink
added possibility to save response specific field's value to use it l…
Browse files Browse the repository at this point in the history
…ater
  • Loading branch information
takeit committed Feb 10, 2015
1 parent 70789d9 commit 6291375
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
8 changes: 4 additions & 4 deletions features/3_api_topics.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Feature: Testing Topics API
When I request "/topics"
Then the response is JSON

Scenario: Creating a new root topic and checking if it has been created successfully
Scenario: Creating a new root topic and subtopic, checking if topics have been created successfully
Given that I want to make a new topic
And that i have fake "topic" data:
| title | roottopic | 4 |
Expand All @@ -31,12 +31,12 @@ Feature: Testing Topics API
And the response should contain field "translations"
And field "level" in the response should be "0"
And field "title" in the response should be "roottopic"
And save "id" field under location "topic_id"

Scenario: Creating a new subtopic and checking if it has been created successfully
Given that I want to make a new subtopic
And that i have fake "topic" data:
| title | <<sentence>> | 4 |
| parent | 1 ||
| parent | (topic_id) ||

And I'm logged in as "testuser" with "testpassword" with client "1_svdg45ew371vtsdgd29fgvwe5v" and secret "h48fgsmv0due4nexjsy40jdf3sswwr"
When I submit "topic" data to "/topics"
Expand All @@ -56,7 +56,7 @@ Feature: Testing Topics API
And the response should contain field "root"
And the response should contain field "level"
And the response should contain field "translations"
And field "parent" in the response should be "1"
And field "parent" in the response should be "(topic_id)"
And field "level" in the response should be "1"

Scenario: Getting all the topics
Expand Down
2 changes: 1 addition & 1 deletion features/4_api_link_unlink_topic_to_from_article.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Feature: Testing linking/unlinking topics to/from the articles
In order to link/ublink topics through the API
In order to link/unlink topics through the API
As a service user
I want to see if the topics can be linked or unlinked to/from the articles

Expand Down
52 changes: 51 additions & 1 deletion features/bootstrap/RestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ public function collectFakeData($dataKey, \Behat\Gherkin\Node\TableNode $data)
if (strpos($value[1], '<<') !== false) {
$functionName = str_replace('>>', '', str_replace('<<', '', $value[1]));
$fakeValue = call_user_func_array(array($faker, $functionName), explode(',', $value[2]));

if ($functionName == 'file' || $functionName == 'image') {
$fakeValue = new FormUpload($fakeValue);
}
Expand All @@ -269,6 +268,15 @@ public function collectFakeData($dataKey, \Behat\Gherkin\Node\TableNode $data)
continue;
}

if (strpos($value[1], '(') !== false) {
$locationIndex = str_replace(')', '', str_replace('(', '', $value[1]));
$locationValue = $this->locations[$locationIndex];
parse_str($value[0].'='.$locationValue, $temp);
$temp = array($dataKey => $temp);
$this->collectedData = array_merge_recursive($this->collectedData, $temp);
continue;
}

$this->collectedData[$dataKey][$value[0]] = $value[1];
}
}
Expand All @@ -288,6 +296,38 @@ public function collectLocations($locationIndex)
$this->locations[$locationIndex] = $this->response->getHeader('X-Location');
}

/**
* @Then /^save "([^"]+)" field under location "([^"]*)"$/
* @param string $fieldName
* @param string $locationIndex
* @return Step|void
* @throws Exception
*/
public function saveFieldValueUnderLocation($fieldName, $locationIndex)
{
$this->response = $this->client->getLastResponse();
$fieldValue = null;
if ($this->responseIsJson) {
if ($this->associative) {
if (!(is_array($this->responseData)) || !array_key_exists($fieldName, $this->responseData)) {
throw new \Exception('Field "' . $fieldName . '" is not set!');
}

$fieldValue = $this->responseData[$fieldName];
} else {
if (!($this->responseData instanceof stdClass) || !property_exists($this->responseData, $name)) {
throw new \Exception('Field "' . $fieldName . '" is not set!');
}

$fieldValue = $this->responseData->$fieldName;
}

$this->locations[$locationIndex] = $fieldValue;
} else {
return new Step\Then('the response is JSON');
}
}

/**
* @When /^I request "([^"]*)"$/
* @param string $pageUrl
Expand Down Expand Up @@ -474,6 +514,7 @@ public function valueOfTheFieldEquals($fieldName, $fieldValue)
{
if ($this->responseIsJson) {
if (new Step\Given("the response should contain field \"{$fieldName}\"")) {
$fieldValue = $this->extractValueByGivenLocation($fieldValue) ?: $fieldValue;
if ($this->associative) {
if ($this->responseData[$fieldName] != $fieldValue) {
throw new \Exception(
Expand Down Expand Up @@ -501,6 +542,15 @@ public function valueOfTheFieldEquals($fieldName, $fieldValue)
}
}

private function extractValueByGivenLocation($fieldValue)
{
if (strpos($fieldValue, '(') !== false) {
$locationIndex = str_replace(')', '', str_replace('(', '', $fieldValue));

return $this->locations[$locationIndex];
}
}

/**
* @Then /^the response should contain "([^"]*)"$/
* @param string $str
Expand Down

0 comments on commit 6291375

Please sign in to comment.