Skip to content

Commit

Permalink
added methods for testing query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyWendt committed Aug 20, 2015
1 parent e01ea08 commit e8e40a8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 62 additions & 1 deletion src/ApiFeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class ApiFeatureContext extends MinkContext implements Context, SnippetAccepting

protected $guzzleConfig = [];

protected $data = [];

/**
* Initializes context.
* Every scenario gets it's own context object.
Expand Down Expand Up @@ -198,6 +200,17 @@ public function iRequest($httpMethod, $uri)
$this->makeRequest();
}

/**
* @When /^I request "(GET|PUT|POST|PATCH|DELETE) ([^"]*)" and the query needs random data from "([^"]*)"$/
*/
public function iRequestAndTheQueryNeedsRandomData($httpMethod, $resource)
{
$this->setResourceAndQuery($httpMethod, $resource);
$this->putRandomIdsInResource();
$this->putRandomDataInQuery();
$this->makeRequest();
}

/**
* @When /^I request "(GET) ([^"]*)" without the previous ending id$/
*/
Expand All @@ -211,6 +224,14 @@ public function iRequestWithoutThePreviousEndingId($httpMethod, $uri)
$this->makeRequest();
}

/**
* @Then /^I save the data$/
*/
public function iSaveTheData()
{
$this->data = $this->getScopePayload();
}

/**
* @Given /^I set the "([^"]*)" header with "([^"]*)"$/
*/
Expand Down Expand Up @@ -324,11 +345,24 @@ public function itIsEmpty()
public function pickARandomItemFromTheProperty($scope)
{
$this->maybeSetSubScope($scope);

$randomArrayKey = array_rand($this->getScopePayload());
$this->addToCurrentScope($randomArrayKey);
}

/**
* @Then /^pick the first item from the "([^"]*)" property$/
*/
public function pickTheFirstItemFromTheProperty($scope)
{
$this->maybeSetSubScope($scope);

$payload = $this->getScopePayload();
reset($payload);
$firstKey = key($payload);

$this->addToCurrentScope($firstKey);
}

/**
* @Given /^reset scope$/
*/
Expand Down Expand Up @@ -392,6 +426,19 @@ public function thePropertyEquals($property, $expectedValue)
);
}

/**
* @Given /^the "([^"]*)" property equals the random "([^"]*)"$/
*/
public function thePropertyEqualsTheRandom($property, $randomKey)
{
$payload = $this->getScopePayload();

$actual = $this->arrayGet($payload, $property);
$expected = $this->getRandomDataForKey($randomKey);

PhpUnit::assertSame($actual, $expected);
}

/**
* @Given /^the "([^"]*)" property exists$/
*/
Expand Down Expand Up @@ -779,6 +826,15 @@ protected function getIdsFromNestedArray(array $requestPayload)
return $ids;
}

protected function getRandomDataForKey($key)
{
if (!array_key_exists($key, $this->data)) {
return null;
}

return $this->data[$key];
}

/**
* Checks the response exists and returns it.
*
Expand Down Expand Up @@ -872,6 +928,11 @@ protected function maybeSetSubScope($scope)
}
}

protected function putRandomDataInQuery()
{
$this->query = $this->testingHelper->putDataInQueryString($this->query, $this->data);
}

/**
* @param null|array $ids This overrides the ids property
* @param string $idPlaceholder
Expand Down

0 comments on commit e8e40a8

Please sign in to comment.