From 18c6083a22501e36b205a0a229413a80e42e3b65 Mon Sep 17 00:00:00 2001 From: Jeroen Thora Date: Sun, 3 Nov 2019 12:48:04 +0100 Subject: [PATCH] Allow httplug 2.0 (#802) * Allow httplug 2.0 * suppress invalid BC warnings --- .github/bc-test.sh | 49 +++++++++++++++++++ .travis.yml | 3 +- composer.json | 8 +-- .../HttpClient/Plugin/Authentication.php | 4 +- .../Plugin/GithubExceptionThrower.php | 4 +- lib/Github/HttpClient/Plugin/History.php | 7 +-- lib/Github/HttpClient/Plugin/HistoryTrait.php | 32 ++++++++++++ lib/Github/HttpClient/Plugin/PathPrepend.php | 4 +- test/Github/Tests/Api/AbstractApiTest.php | 30 ++++-------- .../Plugin/GithubExceptionThrowerTest.php | 2 +- .../HttpClient/Plugin/PathPrependTest.php | 2 +- 11 files changed, 109 insertions(+), 36 deletions(-) create mode 100755 .github/bc-test.sh create mode 100644 lib/Github/HttpClient/Plugin/HistoryTrait.php diff --git a/.github/bc-test.sh b/.github/bc-test.sh new file mode 100755 index 00000000000..1f66ee13ab0 --- /dev/null +++ b/.github/bc-test.sh @@ -0,0 +1,49 @@ +#!/bin/sh + +# +# This file is a hack to suppress warnings from Roave BC check +# + +echo "Running..." + +# Capture output to variable +OUTPUT=$(./vendor/bin/roave-backward-compatibility-check 2>&1) +echo "$OUTPUT" + +# Remove rows we want to suppress +OUTPUT=`echo "$OUTPUT" | sed '/The return type of Github\\\HttpClient\\\Plugin\\\Authentication#handleRequest() changed from no type to Http\\\Promise\\\Promise/'d` +OUTPUT=`echo "$OUTPUT" | sed '/The return type of Github\\\HttpClient\\\Plugin\\\Authentication#handleRequest() changed from no type to Http\\\Promise\\\Promise/'d` +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` +OUTPUT=`echo "$OUTPUT" | sed '/The parameter $exception of Github\\\HttpClient\\\Plugin\\\History#addFailure() changed from Http\\\Client\\\Exception to Psr\\\Http\\\Client\\\ClientExceptionInterface/'d` +OUTPUT=`echo "$OUTPUT" | sed '/The return type of Github\\\HttpClient\\\Plugin\\\PathPrepend#handleRequest() changed from no type to Http\\\Promise\\\Promise/'d` +OUTPUT=`echo "$OUTPUT" | sed '/The return type of Github\\\HttpClient\\\Plugin\\\GithubExceptionThrower#handleRequest() changed from no type to Http\\\Promise\\\Promise/'d` +OUTPUT=`echo "$OUTPUT" | sed '/Method getMessage() was added to interface Github\\\Exception\\\ExceptionInterface/'d` +OUTPUT=`echo "$OUTPUT" | sed '/Method getCode() was added to interface Github\\\Exception\\\ExceptionInterface/'d` +OUTPUT=`echo "$OUTPUT" | sed '/Method getFile() was added to interface Github\\\Exception\\\ExceptionInterface/'d` +OUTPUT=`echo "$OUTPUT" | sed '/Method getLine() was added to interface Github\\\Exception\\\ExceptionInterface/'d` +OUTPUT=`echo "$OUTPUT" | sed '/Method getTrace() was added to interface Github\\\Exception\\\ExceptionInterface/'d` +OUTPUT=`echo "$OUTPUT" | sed '/Method getPrevious() was added to interface Github\\\Exception\\\ExceptionInterface/'d` +OUTPUT=`echo "$OUTPUT" | sed '/Method getTraceAsString() was added to interface Github\\\Exception\\\ExceptionInterface/'d` +OUTPUT=`echo "$OUTPUT" | sed '/Method __toString() was added to interface Github\\\Exception\\\ExceptionInterface/'d` + +# Number of rows we found with "[BC]" in them +BC_BREAKS=`echo "$OUTPUT" | grep -o '\[BC\]' | wc -l | awk '{ print $1 }'` + +# The last row of the output is "X backwards-incompatible changes detected". Find X. +STATED_BREAKS=`echo "$OUTPUT" | tail -n 1 | awk -F' ' '{ print $1 }'` + +# If +# We found "[BC]" in the command output after we removed suppressed lines +# OR +# We have suppressed X number of BC breaks. If $STATED_BREAKS is larger than X +# THEN +# exit 1 + +if [ $BC_BREAKS -gt 0 ] || [ $STATED_BREAKS -gt 13 ]; then + echo "EXIT 1" + exit 1 +fi + +# No BC breaks found +echo "EXIT 0" +exit 0 diff --git a/.travis.yml b/.travis.yml index 4630e91bffd..6912ddfbfbc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,6 @@ sudo: false cache: directories: - - vendor - $HOME/.composer/cache env: @@ -19,7 +18,7 @@ matrix: include: - php: 7.2 name: Backward compatibillity check - env: DEPENDENCIES="roave/backward-compatibility-check" TEST_COMMAND="./vendor/bin/roave-backward-compatibility-check" + env: DEPENDENCIES="roave/backward-compatibility-check" TEST_COMMAND="./.github/bc-test.sh" before_install: - if ! [ -z "$DEPENDENCIES" ]; then composer require --no-update ${DEPENDENCIES}; fi; diff --git a/composer.json b/composer.json index af0e7df5ad9..10eacd31e0a 100644 --- a/composer.json +++ b/composer.json @@ -20,16 +20,16 @@ "php": "^7.1", "psr/http-message": "^1.0", "psr/cache": "^1.0", - "php-http/httplug": "^1.1", + "php-http/httplug": "^1.1 || ^2.0", "php-http/discovery": "^1.0", "php-http/client-implementation": "^1.0", - "php-http/client-common": "^1.6", + "php-http/client-common": "^1.6 || ^2.0", "php-http/cache-plugin": "^1.4" }, "require-dev": { "phpunit/phpunit": "^7.0 || ^8.0", - "php-http/guzzle6-adapter": "^1.0", - "php-http/mock-client": "^1.0", + "php-http/guzzle6-adapter": "^1.0 || ^2.0", + "php-http/mock-client": "^1.2", "guzzlehttp/psr7": "^1.2", "cache/array-adapter": "^0.4" }, diff --git a/lib/Github/HttpClient/Plugin/Authentication.php b/lib/Github/HttpClient/Plugin/Authentication.php index 920d65722c6..0ab09ab20ba 100644 --- a/lib/Github/HttpClient/Plugin/Authentication.php +++ b/lib/Github/HttpClient/Plugin/Authentication.php @@ -14,6 +14,8 @@ */ class Authentication implements Plugin { + use Plugin\VersionBridgePlugin; + private $tokenOrLogin; private $password; private $method; @@ -28,7 +30,7 @@ public function __construct($tokenOrLogin, $password, $method) /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function doHandleRequest(RequestInterface $request, callable $next, callable $first) { switch ($this->method) { case Client::AUTH_HTTP_PASSWORD: diff --git a/lib/Github/HttpClient/Plugin/GithubExceptionThrower.php b/lib/Github/HttpClient/Plugin/GithubExceptionThrower.php index a0593924b7a..933c06a5817 100644 --- a/lib/Github/HttpClient/Plugin/GithubExceptionThrower.php +++ b/lib/Github/HttpClient/Plugin/GithubExceptionThrower.php @@ -18,10 +18,12 @@ */ class GithubExceptionThrower implements Plugin { + use Plugin\VersionBridgePlugin; + /** * {@inheritdoc} */ - public function handleRequest(RequestInterface $request, callable $next, callable $first) + public function doHandleRequest(RequestInterface $request, callable $next, callable $first) { return $next($request)->then(function (ResponseInterface $response) use ($request) { if ($response->getStatusCode() < 400 || $response->getStatusCode() > 600) { diff --git a/lib/Github/HttpClient/Plugin/History.php b/lib/Github/HttpClient/Plugin/History.php index 303d81404dc..0f59bbb65f9 100644 --- a/lib/Github/HttpClient/Plugin/History.php +++ b/lib/Github/HttpClient/Plugin/History.php @@ -3,7 +3,6 @@ namespace Github\HttpClient\Plugin; use Http\Client\Common\Plugin\Journal; -use Http\Client\Exception; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -14,6 +13,8 @@ */ class History implements Journal { + use HistoryTrait; + /** * @var ResponseInterface */ @@ -31,8 +32,4 @@ public function addSuccess(RequestInterface $request, ResponseInterface $respons { $this->lastResponse = $response; } - - public function addFailure(RequestInterface $request, Exception $exception) - { - } } diff --git a/lib/Github/HttpClient/Plugin/HistoryTrait.php b/lib/Github/HttpClient/Plugin/HistoryTrait.php new file mode 100644 index 00000000000..fbc5335d318 --- /dev/null +++ b/lib/Github/HttpClient/Plugin/HistoryTrait.php @@ -0,0 +1,32 @@ +getUri()->getPath(); if (strpos($currentPath, $this->path) !== 0) { diff --git a/test/Github/Tests/Api/AbstractApiTest.php b/test/Github/Tests/Api/AbstractApiTest.php index ec8dd1e0be0..ae2fa7cd3b6 100644 --- a/test/Github/Tests/Api/AbstractApiTest.php +++ b/test/Github/Tests/Api/AbstractApiTest.php @@ -4,6 +4,7 @@ use Github\Api\AbstractApi; use GuzzleHttp\Psr7\Response; +use Http\Client\Common\HttpMethodsClientInterface; class AbstractApiTest extends TestCase { @@ -212,26 +213,15 @@ protected function getClientMock() */ protected function getHttpMethodsMock(array $methods = []) { - $methods = array_merge(['sendRequest'], $methods); - $mock = $this->getMockBuilder(\Http\Client\Common\HttpMethodsClient::class) - ->disableOriginalConstructor() - ->setMethods($methods) - ->getMock(); - $mock - ->expects($this->any()) - ->method('sendRequest'); - - return $mock; - } - - /** - * @return \Http\Client\HttpClient - */ - protected function getHttpClientMock() - { - $mock = $this->getMockBuilder(\Http\Client\HttpClient::class) - ->setMethods(['sendRequest']) - ->getMock(); + if (interface_exists(HttpMethodsClientInterface::class)) { + $mock = $this->createMock(HttpMethodsClientInterface::class); + } else { + $methods = array_merge(['sendRequest'], $methods); + $mock = $this->getMockBuilder(\Http\Client\Common\HttpMethodsClient::class) + ->disableOriginalConstructor() + ->setMethods($methods) + ->getMock(); + } $mock ->expects($this->any()) ->method('sendRequest'); diff --git a/test/Github/Tests/HttpClient/Plugin/GithubExceptionThrowerTest.php b/test/Github/Tests/HttpClient/Plugin/GithubExceptionThrowerTest.php index fd83e88c2f4..84027aa4339 100644 --- a/test/Github/Tests/HttpClient/Plugin/GithubExceptionThrowerTest.php +++ b/test/Github/Tests/HttpClient/Plugin/GithubExceptionThrowerTest.php @@ -40,7 +40,7 @@ public function testHandleRequest(ResponseInterface $response, ExceptionInterfac $this->expectExceptionMessage($exception->getMessage()); } - $plugin->handleRequest( + $plugin->doHandleRequest( $request, function (RequestInterface $request) use ($promise) { return $promise; diff --git a/test/Github/Tests/HttpClient/Plugin/PathPrependTest.php b/test/Github/Tests/HttpClient/Plugin/PathPrependTest.php index fdfe180980a..e5feda2cf3d 100644 --- a/test/Github/Tests/HttpClient/Plugin/PathPrependTest.php +++ b/test/Github/Tests/HttpClient/Plugin/PathPrependTest.php @@ -20,7 +20,7 @@ public function testPathIsPrepended($uri, $expectedPath) $plugin = new PathPrepend('/api/v3'); $newRequest = null; - $plugin->handleRequest($request, function ($request) use (&$newRequest) { + $plugin->doHandleRequest($request, function ($request) use (&$newRequest) { $newRequest = $request; }, function () { throw new \RuntimeException('Did not expect plugin to call first');