Skip to content

Commit

Permalink
Update dependencies (#6)
Browse files Browse the repository at this point in the history
Update dependencies
  • Loading branch information
snapshotpl authored Aug 24, 2017
1 parent f55c3ed commit 3a3417f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 42 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
vendor
composer.lock
.idea
12 changes: 7 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
}
],
"require": {
"php": ">=5.4.0",
"behat/behat": "^3.0",
"guzzlehttp/guzzle": "^5.1"
"php": ">=7.0",
"behat/behat": "^3.3",
"guzzlehttp/guzzle": "^6.3",
"zendframework/zend-diactoros": "^1.4.1"
},
"autoload":{
"psr-4" : {"RstGroup\\Behat\\OAuth2\\Context\\": "src/"}
"psr-4" : {
"RstGroup\\Behat\\OAuth2\\Context\\": "src/"
}
}

}
2 changes: 1 addition & 1 deletion features/password_grant_type.feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Feature: OAuth2 Token Grant Password
| username | no |
| password | bar |
And I send a access token request
Then the response status code is 400
Then the response status code is 401
And the response has a "error" property and it is equals "invalid_grant"
And the response has a "error_description" property

Expand Down
81 changes: 46 additions & 35 deletions src/OAuth2Context.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
<?php
/**
* @licence http://opensource.org/licenses/MIT MIT
*/

declare(strict_types=1);

namespace RstGroup\Behat\OAuth2\Context;

use Behat\Gherkin\Node\TableNode;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\TableNode;
use Exception;
use GuzzleHttp\Client as GuzzleHttpClient;
use GuzzleHttp\Message\RequestInterface;
use GuzzleHttp\Message\ResponseInterface;
use GuzzleHttp\Psr7\Request;
use LogicException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Request\Serializer as RequestSerializer;
use Zend\Diactoros\Response\Serializer as ResponseSerializer;
use function GuzzleHttp\json_decode;
use function GuzzleHttp\json_encode;

/**
* OAuth2 context for Behat BDD tool.
*
* @licence http://opensource.org/licenses/MIT MIT
*/
class OAuth2Context implements SnippetAcceptingContext
{
Expand Down Expand Up @@ -94,10 +102,10 @@ public function thatIHaveAnRefreshToken()

$url = $this->parameters['token_url'];
$response = $this->getPostResponseFromUrl($url, $parameters);
$data = json_decode($response->getBody(true), true);
$data = json_decode((string) $response->getBody(), true);

if (!isset($data['refresh_token'])) {
throw new \Exception(sprintf("Error refresh token. Response: %s", $response->getBody(true)));
throw new Exception(sprintf("Error refresh token. Response: %s", (string) $response->getBody()));
}
$this->refreshToken = $data['refresh_token'];
}
Expand Down Expand Up @@ -131,16 +139,16 @@ public function iMakeAAccessTokenRequest()
$url = $this->parameters['token_url'];
$this->response = $this->getPostResponseFromUrl($url, $this->requestBody);

$contentType = (string) $this->response->getHeader('Content-type');
$contentType = $this->response->getHeaderLine('Content-type');

if ($contentType !== 'application/json') {
throw new \Exception(sprintf("Content-type must be application/json %s", $this->echoLastResponse()));
throw new Exception(sprintf("Content-type must be application/json %s", $this->echoLastResponse()));
}
$this->data = json_decode($this->response->getBody(true));
$this->data = json_decode((string) $this->response->getBody());
$this->lastErrorJson = json_last_error();

if ($this->lastErrorJson != JSON_ERROR_NONE) {
throw new \Exception(sprintf("Error parsing response JSON " . "\n\n %s", $this->echoLastResponse()));
throw new Exception(sprintf("Error parsing response JSON " . "\n\n %s", $this->echoLastResponse()));
}
}

Expand All @@ -159,7 +167,7 @@ public function iMakeAAccessTokenRequestWithGivenRefreshToken()
public function theResponseStatusCodeIs($httpStatus)
{
if ((string) $this->response->getStatusCode() !== $httpStatus) {
throw new \Exception(sprintf("HTTP code does not match %s (actual: %s)\n\n %s", $httpStatus, $this->response->getStatusCode(), $this->echoLastResponse()));
throw new Exception(sprintf("HTTP code does not match %s (actual: %s)\n\n %s", $httpStatus, $this->response->getStatusCode(), $this->echoLastResponse()));
}
}

Expand All @@ -177,8 +185,8 @@ public function theResponseHasAProperty($propertyName)

try {
return $this->getPropertyValue($propertyName);
} catch (\LogicException $e) {
throw new \Exception(sprintf("Property %s is not set!\n\n %s", $propertyName, $this->echoLastResponse()));
} catch (LogicException $e) {
throw new Exception(sprintf("Property %s is not set!\n\n %s", $propertyName, $this->echoLastResponse()));
}
}

Expand All @@ -204,7 +212,7 @@ public function theResponseHasAPropertyAndItsTypeIs($propertyName, $typeString)
break;
}
default:
throw new \Exception(sprintf("Property %s is not of the correct type: %s!\n\n %s", $propertyName, $typeString, $this->echoLastResponse()));
throw new Exception(sprintf("Property %s is not of the correct type: %s!\n\n %s", $propertyName, $typeString, $this->echoLastResponse()));
}
}

Expand All @@ -218,15 +226,18 @@ public function theResponseHasAPropertyAndItIsEquals($propertyName, $propertyVal
if ($value == $propertyValue) {
return;
}
throw new \Exception(sprintf("Given %s value is not %s\n\n %s", $propertyName, $propertyValue, $this->echoLastResponse()));
throw new Exception(sprintf("Given %s value is not %s\n\n %s", $propertyName, $propertyValue, $this->echoLastResponse()));
}

/**
* @Then echo last response
*/
public function echoLastResponse()
{
$this->printDebug(sprintf("Request:\n %s \n\n Response:\n %s", $this->request, $this->response));
$request = RequestSerializer::toString($this->request);
$response = ResponseSerializer::toString($this->response);

$this->printDebug(sprintf("Request:\n %s \n\n Response:\n %s", $request, $response));
}

/**
Expand All @@ -240,21 +251,21 @@ public function theResponseHasTheOAuth2Format()
];

foreach ($expectedHeaders as $name => $value) {
if ($this->response->getHeader($name) != $value) {
throw new \Exception(sprintf("Header %s is should be %s, %s given", $name, $value, $this->response->getHeader($name)));
$responseHeaderValue = $this->response->getHeaderLine($name);
if ($responseHeaderValue != $value) {
throw new Exception(sprintf("Header %s is should be %s, %s given", $name, $value, $responseHeaderValue));
}
}
}

/**
* Get POST response from URL without ssl verify and exception propagation
*
* @param string $url POST URL
* @param array $body body array
*/
protected function getPostResponseFromUrl($url, $body)
protected function getPostResponseFromUrl(string $url, array $body): ResponseInterface
{
$this->request = $this->client->createRequest('POST', $url, ['body' => $body, 'verify' => false, 'exceptions' => false]);
$bodyAsQuery = http_build_query($body);
$headers = [
'Content-Type' => 'application/x-www-form-urlencoded',
];
$this->request = new Request('POST', $url, $headers, $bodyAsQuery);

return $this->client->send($this->request);
}

Expand All @@ -263,7 +274,7 @@ protected function getPostResponseFromUrl($url, $body)
*
* @param string $propertyName property name
*/
protected function getPropertyValue($propertyName)
protected function getPropertyValue(string $propertyName)
{
return $this->getValue($propertyName, $this->data);
}
Expand All @@ -274,10 +285,10 @@ protected function getPropertyValue($propertyName)
* @param string $propertyName property name
* @param mixed $data data as array or object
*/
protected function getValue($propertyName, $data)
protected function getValue(string $propertyName, $data)
{
if (empty($data)) {
throw new \Exception(sprintf("Response was not set %s", var_export($data, true)));
throw new Exception(sprintf("Response was not set %s", var_export($data, true)));
}
if (is_array($data) && array_key_exists($propertyName, $data)) {
$data = $data[$propertyName];
Expand All @@ -287,7 +298,7 @@ protected function getValue($propertyName, $data)
$data = $data->$propertyName;
return $data;
}
throw new \LogicException(sprintf("'%s' is not set", $propertyName));
throw new LogicException(sprintf("'%s' is not set", $propertyName));
}

/**
Expand All @@ -296,7 +307,7 @@ protected function getValue($propertyName, $data)
* @param string $name header name
* @param string $value value for header name
*/
protected function setHeader($name, $value)
protected function setHeader(string $name, string $value)
{
$this->headers[$name] = $value;
}
Expand All @@ -313,7 +324,7 @@ protected function setHeaders(array $headers)
}
}

protected function getGuzzleParameters()
protected function getGuzzleParameters(): array
{
return isset($this->parameters[self::GUZZLE_PARAMETERS]) && is_array($this->parameters[self::GUZZLE_PARAMETERS]) ? $this->parameters[self::GUZZLE_PARAMETERS] : [];
}
Expand All @@ -323,7 +334,7 @@ protected function getGuzzleParameters()
*
* @param string $string debug string
*/
public function printDebug($string)
public function printDebug(string $string)
{
echo sprintf("\n\033[36m| %s\033[0m\n\n", strtr($string, ["\n" => "\n| "]));
}
Expand Down

0 comments on commit 3a3417f

Please sign in to comment.