Skip to content

Commit d550762

Browse files
committed
Guzzle 6 is now fully supported and tested
1 parent 759fd8f commit d550762

22 files changed

+573
-177
lines changed

guzzle_environments/4/phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<testsuites>
55
<testsuite name="All Tests">
66
<directory>../../tests</directory>
7+
<exclude>../../tests/OAuth2MiddlewareTest.php</exclude>
78
</testsuite>
89
</testsuites>
910
<filter>

guzzle_environments/5/phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<testsuites>
55
<testsuite name="All Tests">
66
<directory>../../tests</directory>
7+
<exclude>../../tests/OAuth2SubscriberTest.php</exclude>
78
</testsuite>
89
</testsuites>
910
<filter>

guzzle_environments/6/phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<testsuites>
55
<testsuite name="All Tests">
66
<directory>../../tests</directory>
7+
<exclude>../../tests/OAuth2SubscriberTest.php</exclude>
78
</testsuite>
89
</testsuites>
910
<filter>

src/OAuth2Middleware.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public function __invoke(callable $handler)
3030
$request = $this->signRequest($request);
3131

3232
return $handler($request, $options)->then(
33-
$this->onFulfilled($request, $options),
34-
$this->onRejected($request, $options)
33+
$this->onFulfilled($request, $options, $handler),
34+
$this->onRejected($request, $options, $handler)
3535
);
3636
};
3737
}
@@ -44,15 +44,18 @@ public function __invoke(callable $handler)
4444
*
4545
* @param ErrorEvent $event Event received
4646
*/
47-
private function onFulfilled(RequestInterface $request, array $options)
47+
private function onFulfilled(RequestInterface $request, array $options, $handler)
4848
{
49-
return function ($response) use ($request, $options) {
49+
return function ($response) use ($request, $options, $handler) {
5050
// Only deal with Unauthorized response.
5151
if ($response && $response->getStatusCode() != 401) {
5252
return $response;
5353
}
5454

5555
// If we already retried once, give up.
56+
// This is extremely unlikely in Guzzle 6+ since we're using promises
57+
// to check the response - looping should be impossible, but I'm leaving
58+
// the code here in case something interferes with the Middleware
5659
if ($request->hasHeader('X-Guzzle-Retry')) {
5760
return $response;
5861
}
@@ -66,11 +69,11 @@ private function onFulfilled(RequestInterface $request, array $options)
6669
$request = $request->withHeader('X-Guzzle-Retry', '1');
6770
$request = $this->signRequest($request);
6871

69-
return $this($request, $options);
72+
return $handler($request, $options);
7073
};
7174
}
7275

73-
private function onRejected(RequestInterface $request, array $options)
76+
private function onRejected(RequestInterface $request, array $options, $handler)
7477
{
7578
return function ($reason) use ($request, $options) {
7679
return \GuzzleHttp\Promise\rejection_for($reason);

src/Signer/AccessToken/BasicAuth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class BasicAuth implements SignerInterface
88
{
99
public function sign($request, $accessToken)
1010
{
11-
if (Helper::guzzleIs('~', 6)) {
11+
if (Helper::guzzleIs('>=', 6)) {
1212
return $request->withHeader('Authorization', 'Bearer ' . $accessToken);
1313
}
1414

src/Signer/AccessToken/QueryString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __construct($fieldName = 'access_token')
1616
public function sign($request, $accessToken)
1717
{
1818
if (Helper::guzzleIs('~', 6)) {
19-
$uri = Psr\Http\Message\UriInterface\Uri::withQueryValue(
19+
$uri = \GuzzleHttp\Psr7\Uri::withQueryValue(
2020
$request->getUri(),
2121
$this->fieldName,
2222
$accessToken

src/Signer/ClientCredentials/PostFormData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function __construct($clientIdField = 'client_id', $clientSecretField = '
1818

1919
public function sign($request, $clientId, $clientSecret)
2020
{
21-
if (Helper::guzzleIs('~', 6)) {
21+
if (Helper::guzzleIs('>=', 6)) {
2222
if ($request->getHeaderLine('Content-Type') != 'application/x-www-form-urlencoded') {
2323
throw new \RuntimeException('Unable to set fields in request body');
2424
}

tests/BaseTestCase.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,76 @@
22

33
namespace kamermans\OAuth2\Tests;
44

5+
use kamermans\OAuth2\Utils\Helper;
6+
use GuzzleHttp\Post\PostBody;
7+
use GuzzleHttp\Message\Request;
8+
use GuzzleHttp\Psr7\Request as Psr7Request;
9+
510
class BaseTestCase extends \PHPUnit_Framework_TestCase
611
{
712

13+
protected function createRequest($method, $uri, $options=[])
14+
{
15+
return Helper::guzzleIs('>=', 6)?
16+
new Psr7Request($method, $uri, $options):
17+
new Request($method, $uri, ['headers' => $options]);
18+
}
19+
20+
protected function getHeader($request, $header)
21+
{
22+
return Helper::guzzleIs('>=', 6)?
23+
$request->getHeaderLine($header):
24+
$request->getHeader($header);
25+
}
26+
27+
protected function getQueryStringValue($request, $field)
28+
{
29+
if (Helper::guzzleIs('<', 6)) {
30+
return $request->getQuery()->get($field);
31+
}
32+
33+
$query_string = $request->getUri()->getQuery();
34+
35+
$values = $this->parseQueryString($query_string);
36+
37+
return array_key_exists($field, $values)? $values[$field]: null;
38+
39+
}
40+
41+
protected function getFormPostBodyValue($request, $field)
42+
{
43+
if (Helper::guzzleIs('<', 6)) {
44+
return $request->getBody()->getField($field);
45+
}
46+
47+
$query_string = (string)$request->getBody();
48+
49+
$values = $this->parseQueryString($query_string);
50+
51+
return array_key_exists($field, $values)? $values[$field]: null;
52+
53+
}
54+
55+
protected function parseQueryString($query_string)
56+
{
57+
$values = [];
58+
foreach (explode('&', $query_string) as $component) {
59+
list($key, $value) = explode('=', $component);
60+
$values[rawurldecode($key)] = rawurldecode($value);
61+
}
62+
63+
return $values;
64+
}
65+
66+
protected function setPostBody($request, array $data=[])
67+
{
68+
if (Helper::guzzleIs('>=', 6)) {
69+
return $request->withBody(\GuzzleHttp\Psr7\stream_for(http_build_query($data, '', '&')));
70+
}
71+
72+
$request->setBody(new PostBody($data));
73+
74+
return $request;
75+
}
76+
877
}

tests/GrantType/AuthorizationCodeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use kamermans\OAuth2\GrantType\AuthorizationCode;
1818
use kamermans\OAuth2\Tests\BaseTestCase;
1919

20-
class AuthorizationCodeTest extends \kamermans\OAuth2\Tests\BaseTestCase
20+
class AuthorizationCodeTest extends BaseTestCase
2121
{
2222
public function testConstruct()
2323
{

tests/GrantType/ClientCredentialsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use kamermans\OAuth2\Utils\Helper;
1717
use kamermans\OAuth2\GrantType\ClientCredentials;
1818

19-
class ClientCredentialsTest extends \kamermans\OAuth2\Tests\BaseTestCase
19+
class ClientCredentialsTest extends BaseTestCase
2020
{
2121
public function testConstruct()
2222
{

tests/GrantType/PasswordCredentialsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use kamermans\OAuth2\Utils\Helper;
1717
use kamermans\OAuth2\GrantType\PasswordCredentials;
1818

19-
class PasswordCredentialsTest extends \kamermans\OAuth2\Tests\BaseTestCase
19+
class PasswordCredentialsTest extends BaseTestCase
2020
{
2121
public function testConstruct()
2222
{

tests/GrantType/RefreshTokenTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use kamermans\OAuth2\Utils\Helper;
1717
use kamermans\OAuth2\GrantType\RefreshToken;
1818

19-
class RefreshTokenTest extends \kamermans\OAuth2\Tests\BaseTestCase
19+
class RefreshTokenTest extends BaseTestCase
2020
{
2121
public function testConstruct()
2222
{

tests/GrantType/Specific/GithubApplicationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use kamermans\OAuth2\Utils\Helper;
1818
use kamermans\OAuth2\GrantType\Specific\GithubApplication;
1919

20-
class GithubApplicationTest extends \kamermans\OAuth2\Tests\BaseTestCase
20+
class GithubApplicationTest extends BaseTestCase
2121
{
2222
public function testConstruct()
2323
{

0 commit comments

Comments
 (0)