From b6d294e208926e3f6ec68a3ae654527434e82016 Mon Sep 17 00:00:00 2001 From: Dave Nash Date: Mon, 22 Jun 2015 12:45:56 +0100 Subject: [PATCH] Added array checking from JSON * Created new step definition * Fixed failing Travis Behat test --- config/service.json | 16 ++++-- features/repo.feature | 24 +++++++-- .../Context/GuzzleContextSpec.php | 20 ++++++++ .../GuzzleExtension/Context/GuzzleContext.php | 50 ++++++++++++++++++- .../Context/RawGuzzleContext.php | 2 +- 5 files changed, 104 insertions(+), 8 deletions(-) diff --git a/config/service.json b/config/service.json index db2b972..b660dc7 100644 --- a/config/service.json +++ b/config/service.json @@ -1,14 +1,24 @@ { "name": "Travis API", "operations": { + "GetConfig": { + "httpMethod": "GET", + "uri": "config", + "summary": "Gets the config" + }, "GetReposBuilds": { "httpMethod": "GET", - "uri": "repos/{slug}/builds", + "uri": "repos/{profile}/{repo}/builds", "summary": "Gets the last build for repo", "parameters": { - "slug": { + "profile": { + "location": "uri", + "description": "Profile name from GitHub", + "required": true + }, + "repo": { "location": "uri", - "description": "Repo slug from GitHub", + "description": "Repo name from GitHub", "required": true }, "number": { diff --git a/features/repo.feature b/features/repo.feature index c727ae9..e04034b 100644 --- a/features/repo.feature +++ b/features/repo.feature @@ -1,11 +1,29 @@ Feature: Repo As a user - I would like to see build details for a repo + I would like to see the config and build details for a repo + + Scenario: Getting the config + When I call "GetConfig" + Then I get a successful response + And the response contains the following values from JSON: + """ + { + "config": { + "host": "travis-ci.org", + "github": { + "scopes": [ + "read:org" + ] + } + } + } + """ Scenario: Getting the first build When I call "GetReposBuilds" with the following values: - | slug | teaandcode/behat-guzzle-extension | - | number | 1 | + | profile | teaandcode | + | repo | behat-guzzle-extension | + | number | 1 | Then I get a successful response And the response contains 1 resource with the following data: | id | repository_id | number | state | diff --git a/spec/Behat/GuzzleExtension/Context/GuzzleContextSpec.php b/spec/Behat/GuzzleExtension/Context/GuzzleContextSpec.php index 64a73a2..68c255e 100644 --- a/spec/Behat/GuzzleExtension/Context/GuzzleContextSpec.php +++ b/spec/Behat/GuzzleExtension/Context/GuzzleContextSpec.php @@ -151,6 +151,26 @@ public function it_has_i_call_command_with_value_from_json() $this->theResponseContainsTheFollowingValue($table); } + public function it_has_i_call_command_with_value_from_json_and_response_contains_value_from_json() + { + $client = $this->getMockedClient( + new Response( + 200, + array( + 'Content-Type' => 'application/json' + ), + '{"foo":"bar","fu":[{"id":4},{"id":6}]}' + ) + ); + + $string = new PyStringNode(array('{"test":"foo"}'), 1); + $table = new PyStringNode(array('{"foo":"bar","fu":[{"id":4}]}'), 1); + + $this->setGuzzleClient($client); + $this->iCallCommandWithValueFromJSON('Mock', $string); + $this->theResponseContainsTheFollowingValueFromJSON($table); + } + public function it_has_i_call_command_but_response_contains_a_wrong_value() { $client = $this->getMockedClient( diff --git a/src/Behat/GuzzleExtension/Context/GuzzleContext.php b/src/Behat/GuzzleExtension/Context/GuzzleContext.php index c84db1c..b42b798 100644 --- a/src/Behat/GuzzleExtension/Context/GuzzleContext.php +++ b/src/Behat/GuzzleExtension/Context/GuzzleContext.php @@ -298,6 +298,31 @@ public function theResponseContainsTheFollowingValue(TableNode $table) $this->compareValues($item, $data); } + /** + * Check response contains specified values from JSON + * + * Example: The the response contains the following values from JSON: + * """ + * """ + * Example: And the response contains the following value from JSON: + * """ + * """ + * + * @param PyStringNode $string Values specified in feature as JSON + * + * @Then the response contains the following value(s) from JSON: + */ + public function theResponseContainsTheFollowingValueFromJSON( + PyStringNode $string + ) { + $data = json_decode($string, true); + $item = $this->getGuzzleResult(); + + $data = $this->addStoredValuesToArray($data); + + $this->compareValues($item, $data); + } + /** * * Example: Then the response contains 2 resources with the following data: @@ -317,7 +342,7 @@ public function theResponseContainsResourceWithTheFollowingData( $count, TableNode $table ) { - $list = $this->getGuzzleResult(); + $list = $this->getGuzzleResult(); $length = count($list); if ($length != $count) { @@ -402,4 +427,27 @@ protected function addStoredValues($string) return $string; } + + /** + * Adds stored values to array + * + * @param array $array Array containing stored field markers + * + * @access protected + * @return array + */ + protected function addStoredValuesToArray($array) + { + foreach ($array as $field => $value) { + if (is_array($value)) { + $value = $this->addStoredValuesToArray($value); + } else { + $value = $this->addStoredValues($value); + } + + $array[$field] = $value; + } + + return $array; + } } diff --git a/src/Behat/GuzzleExtension/Context/RawGuzzleContext.php b/src/Behat/GuzzleExtension/Context/RawGuzzleContext.php index ebfda6b..30988ec 100644 --- a/src/Behat/GuzzleExtension/Context/RawGuzzleContext.php +++ b/src/Behat/GuzzleExtension/Context/RawGuzzleContext.php @@ -38,7 +38,7 @@ class RawGuzzleContext implements GuzzleAwareContext /** * @var string */ - const GUZZLE_EXTENSION_VERSION = '0.3.5'; + const GUZZLE_EXTENSION_VERSION = '0.3.6'; /** * @var Client