/** * @When /^I follow the "([^"]+)" uri template HAL link with the following parameters:$/ */ public function iFollowTheXUriTemplateLinkWithTheFollowingParameters($rel, TableNode $table) { $parameters = array(); foreach ($table->getHash() as $rows) { foreach ($rows as $key => $value) { if (preg_match('/\\[\\]$/', $key)) { $parameters[$key][] = $value; } else { $parameters[$key] = $value; } } } $parameters = array_map(function ($value) { return is_array($value) ? array_filter($value) : $value; }, $parameters); $parameters = array_combine(array_map('urlencode', array_keys($parameters)), array_values($parameters)); $uriTemplateParser = new UriTemplate(); $href = $uriTemplateParser->expand(JsonUtil::getJsonPath($this, sprintf('[\'_links\'][\'%s\'][\'href\']', $rel)), $parameters); return array(new When(sprintf('I go to "%s"', $href))); }
/** * @Then /^the JSON node ?(.*) should match:$/ */ public function theNodeXShouldMatch($path, TableNode $table) { $node = JsonUtil::getJsonPath($this, $path); $tableHash = $table->getHash(); $properties = $tableHash[0]; foreach ($properties as $propertyPath => $expectedValue) { if ($expectedValue != JsonUtil::getArrayPath($node, $propertyPath)) { throw new \Exception(sprintf('The JSON node "%s" does not contain the following object: %s.', $path, json_encode($properties))); } } }
/** * @Then /^the "([^"]+)" cookie JSON node ?(.*) should be equal to "([^"]+)"$/ */ public function theCookieJsonNodeShouldBeEqualTo($cookieName, $path, $expectedValue) { $cookie = $this->getCookie($cookieName); \PHPUnit_Framework_Assert::assertEquals($expectedValue, JsonUtil::getArrayPath(json_decode($cookie->getValue(), true), $path)); }
/** * @When /^I send a (\S+) request to the "([^"]+)" HAL link with json body:$/ */ public function iSendAXRequestToTheXHALLinkWithJsonBody($method, $rel, PyStringNode $jsonBody) { $href = JsonUtil::getJsonPath($this, sprintf('[\'_links\'][\'%s\'][\'href\']', $rel)); return array(new Then(sprintf('I send a %s request on "%s" with json body:', $method, $href), $jsonBody)); }