Skip to content

Commit 18c6083

Browse files
acrobatNyholm
authored andcommitted
Allow httplug 2.0 (KnpLabs#802)
* Allow httplug 2.0 * suppress invalid BC warnings
1 parent 5071f3e commit 18c6083

File tree

11 files changed

+109
-36
lines changed

11 files changed

+109
-36
lines changed

.github/bc-test.sh

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/sh
2+
3+
#
4+
# This file is a hack to suppress warnings from Roave BC check
5+
#
6+
7+
echo "Running..."
8+
9+
# Capture output to variable
10+
OUTPUT=$(./vendor/bin/roave-backward-compatibility-check 2>&1)
11+
echo "$OUTPUT"
12+
13+
# Remove rows we want to suppress
14+
OUTPUT=`echo "$OUTPUT" | sed '/The return type of Github\\\HttpClient\\\Plugin\\\Authentication#handleRequest() changed from no type to Http\\\Promise\\\Promise/'d`
15+
OUTPUT=`echo "$OUTPUT" | sed '/The return type of Github\\\HttpClient\\\Plugin\\\Authentication#handleRequest() changed from no type to Http\\\Promise\\\Promise/'d`
16+
OUTPUT=`echo "$OUTPUT" | sed '/The parameter $exception of Github\\\HttpClient\\\Plugin\\\History#addFailure() changed from Http\\\Client\\\Exception to a non-contravariant Psr\\\Http\\\Client\\\ClientExceptionInterface/'d`
17+
OUTPUT=`echo "$OUTPUT" | sed '/The parameter $exception of Github\\\HttpClient\\\Plugin\\\History#addFailure() changed from Http\\\Client\\\Exception to Psr\\\Http\\\Client\\\ClientExceptionInterface/'d`
18+
OUTPUT=`echo "$OUTPUT" | sed '/The return type of Github\\\HttpClient\\\Plugin\\\PathPrepend#handleRequest() changed from no type to Http\\\Promise\\\Promise/'d`
19+
OUTPUT=`echo "$OUTPUT" | sed '/The return type of Github\\\HttpClient\\\Plugin\\\GithubExceptionThrower#handleRequest() changed from no type to Http\\\Promise\\\Promise/'d`
20+
OUTPUT=`echo "$OUTPUT" | sed '/Method getMessage() was added to interface Github\\\Exception\\\ExceptionInterface/'d`
21+
OUTPUT=`echo "$OUTPUT" | sed '/Method getCode() was added to interface Github\\\Exception\\\ExceptionInterface/'d`
22+
OUTPUT=`echo "$OUTPUT" | sed '/Method getFile() was added to interface Github\\\Exception\\\ExceptionInterface/'d`
23+
OUTPUT=`echo "$OUTPUT" | sed '/Method getLine() was added to interface Github\\\Exception\\\ExceptionInterface/'d`
24+
OUTPUT=`echo "$OUTPUT" | sed '/Method getTrace() was added to interface Github\\\Exception\\\ExceptionInterface/'d`
25+
OUTPUT=`echo "$OUTPUT" | sed '/Method getPrevious() was added to interface Github\\\Exception\\\ExceptionInterface/'d`
26+
OUTPUT=`echo "$OUTPUT" | sed '/Method getTraceAsString() was added to interface Github\\\Exception\\\ExceptionInterface/'d`
27+
OUTPUT=`echo "$OUTPUT" | sed '/Method __toString() was added to interface Github\\\Exception\\\ExceptionInterface/'d`
28+
29+
# Number of rows we found with "[BC]" in them
30+
BC_BREAKS=`echo "$OUTPUT" | grep -o '\[BC\]' | wc -l | awk '{ print $1 }'`
31+
32+
# The last row of the output is "X backwards-incompatible changes detected". Find X.
33+
STATED_BREAKS=`echo "$OUTPUT" | tail -n 1 | awk -F' ' '{ print $1 }'`
34+
35+
# If
36+
# We found "[BC]" in the command output after we removed suppressed lines
37+
# OR
38+
# We have suppressed X number of BC breaks. If $STATED_BREAKS is larger than X
39+
# THEN
40+
# exit 1
41+
42+
if [ $BC_BREAKS -gt 0 ] || [ $STATED_BREAKS -gt 13 ]; then
43+
echo "EXIT 1"
44+
exit 1
45+
fi
46+
47+
# No BC breaks found
48+
echo "EXIT 0"
49+
exit 0

.travis.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ sudo: false
33

44
cache:
55
directories:
6-
- vendor
76
- $HOME/.composer/cache
87

98
env:
@@ -19,7 +18,7 @@ matrix:
1918
include:
2019
- php: 7.2
2120
name: Backward compatibillity check
22-
env: DEPENDENCIES="roave/backward-compatibility-check" TEST_COMMAND="./vendor/bin/roave-backward-compatibility-check"
21+
env: DEPENDENCIES="roave/backward-compatibility-check" TEST_COMMAND="./.github/bc-test.sh"
2322

2423
before_install:
2524
- if ! [ -z "$DEPENDENCIES" ]; then composer require --no-update ${DEPENDENCIES}; fi;

composer.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020
"php": "^7.1",
2121
"psr/http-message": "^1.0",
2222
"psr/cache": "^1.0",
23-
"php-http/httplug": "^1.1",
23+
"php-http/httplug": "^1.1 || ^2.0",
2424
"php-http/discovery": "^1.0",
2525
"php-http/client-implementation": "^1.0",
26-
"php-http/client-common": "^1.6",
26+
"php-http/client-common": "^1.6 || ^2.0",
2727
"php-http/cache-plugin": "^1.4"
2828
},
2929
"require-dev": {
3030
"phpunit/phpunit": "^7.0 || ^8.0",
31-
"php-http/guzzle6-adapter": "^1.0",
32-
"php-http/mock-client": "^1.0",
31+
"php-http/guzzle6-adapter": "^1.0 || ^2.0",
32+
"php-http/mock-client": "^1.2",
3333
"guzzlehttp/psr7": "^1.2",
3434
"cache/array-adapter": "^0.4"
3535
},

lib/Github/HttpClient/Plugin/Authentication.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
class Authentication implements Plugin
1616
{
17+
use Plugin\VersionBridgePlugin;
18+
1719
private $tokenOrLogin;
1820
private $password;
1921
private $method;
@@ -28,7 +30,7 @@ public function __construct($tokenOrLogin, $password, $method)
2830
/**
2931
* {@inheritdoc}
3032
*/
31-
public function handleRequest(RequestInterface $request, callable $next, callable $first)
33+
public function doHandleRequest(RequestInterface $request, callable $next, callable $first)
3234
{
3335
switch ($this->method) {
3436
case Client::AUTH_HTTP_PASSWORD:

lib/Github/HttpClient/Plugin/GithubExceptionThrower.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
*/
1919
class GithubExceptionThrower implements Plugin
2020
{
21+
use Plugin\VersionBridgePlugin;
22+
2123
/**
2224
* {@inheritdoc}
2325
*/
24-
public function handleRequest(RequestInterface $request, callable $next, callable $first)
26+
public function doHandleRequest(RequestInterface $request, callable $next, callable $first)
2527
{
2628
return $next($request)->then(function (ResponseInterface $response) use ($request) {
2729
if ($response->getStatusCode() < 400 || $response->getStatusCode() > 600) {

lib/Github/HttpClient/Plugin/History.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Github\HttpClient\Plugin;
44

55
use Http\Client\Common\Plugin\Journal;
6-
use Http\Client\Exception;
76
use Psr\Http\Message\RequestInterface;
87
use Psr\Http\Message\ResponseInterface;
98

@@ -14,6 +13,8 @@
1413
*/
1514
class History implements Journal
1615
{
16+
use HistoryTrait;
17+
1718
/**
1819
* @var ResponseInterface
1920
*/
@@ -31,8 +32,4 @@ public function addSuccess(RequestInterface $request, ResponseInterface $respons
3132
{
3233
$this->lastResponse = $response;
3334
}
34-
35-
public function addFailure(RequestInterface $request, Exception $exception)
36-
{
37-
}
3835
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Github\HttpClient\Plugin;
4+
5+
use Http\Client\Exception;
6+
use Psr\Http\Client\ClientExceptionInterface;
7+
use Psr\Http\Message\RequestInterface;
8+
9+
/*
10+
* Below is a some code to make the History plugin compatible with both 1.x and 2.x of php-client/client-common
11+
*/
12+
if (\interface_exists(\Http\Client\Common\HttpMethodsClientInterface::class)) {
13+
/**
14+
* @internal code for php-http/client-common:2.x
15+
*/
16+
trait HistoryTrait
17+
{
18+
public function addFailure(RequestInterface $request, ClientExceptionInterface $exception)
19+
{
20+
}
21+
}
22+
} else {
23+
/**
24+
* @internal code for php-http/client-common:1.x
25+
*/
26+
trait HistoryTrait
27+
{
28+
public function addFailure(RequestInterface $request, Exception $exception)
29+
{
30+
}
31+
}
32+
}

lib/Github/HttpClient/Plugin/PathPrepend.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
class PathPrepend implements Plugin
1414
{
15+
use Plugin\VersionBridgePlugin;
16+
1517
private $path;
1618

1719
/**
@@ -25,7 +27,7 @@ public function __construct($path)
2527
/**
2628
* {@inheritdoc}
2729
*/
28-
public function handleRequest(RequestInterface $request, callable $next, callable $first)
30+
public function doHandleRequest(RequestInterface $request, callable $next, callable $first)
2931
{
3032
$currentPath = $request->getUri()->getPath();
3133
if (strpos($currentPath, $this->path) !== 0) {

test/Github/Tests/Api/AbstractApiTest.php

+10-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Github\Api\AbstractApi;
66
use GuzzleHttp\Psr7\Response;
7+
use Http\Client\Common\HttpMethodsClientInterface;
78

89
class AbstractApiTest extends TestCase
910
{
@@ -212,26 +213,15 @@ protected function getClientMock()
212213
*/
213214
protected function getHttpMethodsMock(array $methods = [])
214215
{
215-
$methods = array_merge(['sendRequest'], $methods);
216-
$mock = $this->getMockBuilder(\Http\Client\Common\HttpMethodsClient::class)
217-
->disableOriginalConstructor()
218-
->setMethods($methods)
219-
->getMock();
220-
$mock
221-
->expects($this->any())
222-
->method('sendRequest');
223-
224-
return $mock;
225-
}
226-
227-
/**
228-
* @return \Http\Client\HttpClient
229-
*/
230-
protected function getHttpClientMock()
231-
{
232-
$mock = $this->getMockBuilder(\Http\Client\HttpClient::class)
233-
->setMethods(['sendRequest'])
234-
->getMock();
216+
if (interface_exists(HttpMethodsClientInterface::class)) {
217+
$mock = $this->createMock(HttpMethodsClientInterface::class);
218+
} else {
219+
$methods = array_merge(['sendRequest'], $methods);
220+
$mock = $this->getMockBuilder(\Http\Client\Common\HttpMethodsClient::class)
221+
->disableOriginalConstructor()
222+
->setMethods($methods)
223+
->getMock();
224+
}
235225
$mock
236226
->expects($this->any())
237227
->method('sendRequest');

test/Github/Tests/HttpClient/Plugin/GithubExceptionThrowerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testHandleRequest(ResponseInterface $response, ExceptionInterfac
4040
$this->expectExceptionMessage($exception->getMessage());
4141
}
4242

43-
$plugin->handleRequest(
43+
$plugin->doHandleRequest(
4444
$request,
4545
function (RequestInterface $request) use ($promise) {
4646
return $promise;

test/Github/Tests/HttpClient/Plugin/PathPrependTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function testPathIsPrepended($uri, $expectedPath)
2020
$plugin = new PathPrepend('/api/v3');
2121

2222
$newRequest = null;
23-
$plugin->handleRequest($request, function ($request) use (&$newRequest) {
23+
$plugin->doHandleRequest($request, function ($request) use (&$newRequest) {
2424
$newRequest = $request;
2525
}, function () {
2626
throw new \RuntimeException('Did not expect plugin to call first');

0 commit comments

Comments
 (0)