diff --git a/.gitignore b/.gitignore index 77aae3d..df26bab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,10 @@ /.idea /composer.lock /vendor +/build +/test-reports +!.editorconfig +!.gitattributes +!.gitignore +!.gitkeep +!.php-cs-fixer.dist.php diff --git a/composer.json b/composer.json index 3ccfb79..08feea0 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ } ], "require": { - "guzzlehttp/guzzle": "6.*" + "guzzlehttp/guzzle": "^6.0 || ^7.0" }, "require-dev": { "phpunit/phpunit": "~5.7" diff --git a/src/Lamia/HttpClient/HttpClient.php b/src/Lamia/HttpClient/HttpClient.php index 4d4db51..7a309ce 100644 --- a/src/Lamia/HttpClient/HttpClient.php +++ b/src/Lamia/HttpClient/HttpClient.php @@ -15,7 +15,7 @@ class HttpClient implements HttpClientInterface */ protected $client; - protected $headers = array(); + protected $headers = []; private $lastResponse; private $lastRequest; @@ -29,7 +29,7 @@ class HttpClient implements HttpClientInterface * @param array $options * @param ClientInterface $client */ - public function __construct($baseUrl, array $options = array(), ClientInterface $client = null) + public function __construct($baseUrl, array $options = [], ClientInterface $client = null) { $this->options = array_merge($this->options, $options); $this->options['base_uri'] = $baseUrl; @@ -66,7 +66,7 @@ public function setHeaders(array $headers) public function clearHeaders() { $this->headers = array( - 'User-Agent' => sprintf('%s', $this->options['user_agent']), + 'User-Agent' => \sprintf('%s', $this->options['user_agent']), ); } @@ -74,7 +74,7 @@ public function clearHeaders() /** * {@inheritDoc} */ - public function get($path, array $parameters = array(), array $headers = array()) + public function get($path, array $parameters = [], array $headers = []) { $path .= '?'. http_build_query($parameters); return $this->request($path, null, 'GET', $headers); @@ -83,7 +83,7 @@ public function get($path, array $parameters = array(), array $headers = array() /** * {@inheritDoc} */ - public function post($path, $body = null, array $headers = array()) + public function post($path, $body = null, array $headers = []) { if (!isset($headers['Content-Type'])) { $headers['Content-Type'] = 'application/x-www-form-urlencoded'; @@ -95,7 +95,7 @@ public function post($path, $body = null, array $headers = array()) /** * {@inheritDoc} */ - public function patch($path, $body = null, array $headers = array()) + public function patch($path, $body = null, array $headers = []) { if (!isset($headers['Content-Type'])) { $headers['Content-Type'] = 'application/x-www-form-urlencoded'; @@ -107,7 +107,7 @@ public function patch($path, $body = null, array $headers = array()) /** * {@inheritDoc} */ - public function delete($path, $body = null, array $headers = array()) + public function delete($path, $body = null, array $headers = []) { return $this->request($path, $body, 'DELETE', $headers); } @@ -115,7 +115,7 @@ public function delete($path, $body = null, array $headers = array()) /** * {@inheritDoc} */ - public function put($path, $body, array $headers = array()) + public function put($path, $body, array $headers = []) { if (!isset($headers['Content-Type'])) { $headers['Content-Type'] = 'application/x-www-form-urlencoded'; @@ -130,8 +130,8 @@ public function request( $path, $body = null, $httpMethod = 'GET', - array $headers = array(), - array $options = array() + array $headers = [], + array $options = [] ) { $request = $this->createRequest($httpMethod, $path, $body, $headers); try { @@ -166,13 +166,13 @@ public function getLastResponse() * @param string $httpMethod * @param string $path * - * @return mixed|\Psr\Http\Message\ResponseInterface + * @return Request */ protected function createRequest( $httpMethod, $path, $body = null, - array $headers = array() + array $headers = [] ) { return new Request( $httpMethod, diff --git a/src/Lamia/HttpClient/HttpClientInterface.php b/src/Lamia/HttpClient/HttpClientInterface.php index 97cc18d..aab7f46 100644 --- a/src/Lamia/HttpClient/HttpClientInterface.php +++ b/src/Lamia/HttpClient/HttpClientInterface.php @@ -2,8 +2,11 @@ namespace Lamia\HttpClient; -use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Exception\GuzzleException; +use Lamia\HttpClient\Exception\ErrorException; use Lamia\HttpClient\Exception\InvalidArgumentException; +use Lamia\HttpClient\Exception\RuntimeException; +use Psr\Http\Message\ResponseInterface; interface HttpClientInterface { @@ -14,9 +17,9 @@ interface HttpClientInterface * @param array $parameters GET Parameters * @param array $headers Reconfigure the request headers for this call only * - * @return Response + * @return ResponseInterface */ - public function get($path, array $parameters = array(), array $headers = array()); + public function get($path, array $parameters = [], array $headers = []); /** * Send a POST request * @@ -24,9 +27,9 @@ public function get($path, array $parameters = array(), array $headers = array() * @param mixed $body Request body * @param array $headers Reconfigure the request headers for this call only * - * @return Response + * @return ResponseInterface */ - public function post($path, $body = null, array $headers = array()); + public function post($path, $body = null, array $headers = []); /** * Send a PATCH request * @@ -35,9 +38,9 @@ public function post($path, $body = null, array $headers = array()); * @param array $headers Reconfigure the request headers for this call only * * @internal param array $parameters Request body - * @return Response + * @return ResponseInterface */ - public function patch($path, $body = null, array $headers = array()); + public function patch($path, $body = null, array $headers = []); /** * Send a PUT request * @@ -45,9 +48,9 @@ public function patch($path, $body = null, array $headers = array()); * @param mixed $body Request body * @param array $headers Reconfigure the request headers for this call only * - * @return Response + * @return ResponseInterface */ - public function put($path, $body, array $headers = array()); + public function put($path, $body, array $headers = []); /** * Send a DELETE request * @@ -55,9 +58,9 @@ public function put($path, $body, array $headers = array()); * @param mixed $body Request body * @param array $headers Reconfigure the request headers for this call only * - * @return Response + * @return ResponseInterface */ - public function delete($path, $body = null, array $headers = array()); + public function delete($path, $body = null, array $headers = []); /** * Send a request to the server, receive a response, * decode the response and returns an associative array @@ -67,23 +70,28 @@ public function delete($path, $body = null, array $headers = array()); * @param string $httpMethod HTTP method to use * @param array $headers Request headers * - * @return Response + * @return ResponseInterface + * + * @throws ErrorException + * @throws RuntimeException + * @throws GuzzleException */ - public function request($path, $body, $httpMethod = 'GET', array $headers = array()); + public function request($path, $body, $httpMethod = 'GET', array $headers = []); /** * Change an option value. * - * @param string $name The option name - * @param mixed $value The value + * @param string $name The option name + * @param mixed $value The value * * @throws InvalidArgumentException + * * @return void */ public function setOption($name, $value); /** * Set HTTP headers * - * @param array $headers + * @param array $headers * @return void */ public function setHeaders(array $headers); diff --git a/tests/Lamia/HttpClient/HttpClientTest.php b/tests/Lamia/HttpClient/HttpClientTest.php index 28cf124..ad2edb0 100644 --- a/tests/Lamia/HttpClient/HttpClientTest.php +++ b/tests/Lamia/HttpClient/HttpClientTest.php @@ -1,4 +1,5 @@ get('posts', array('userId' => 2))->getBody()->getContents()); + $result = json_decode($newInstance->get('posts', ['userId' => 2])->getBody()->getContents()); $this->assertEquals(2, $result[1]->userId); } public function testDummyPostRequest() { $newInstance = new HttpClient(self::TEST_URL); - $result = json_decode($newInstance->post('posts', json_encode(array('title' => 'test', 'body' => 'blabla', 'userId' => 1)))->getBody()->getContents()); + $result = json_decode($newInstance->post('posts', json_encode(['title' => 'test', 'body' => 'blabla', 'userId' => 1]))->getBody()->getContents()); $this->assertNotEmpty($result->id); } @@ -38,7 +39,7 @@ public function testDummyPostRequest() public function testDummyPutRequest() { $newInstance = new HttpClient(self::TEST_URL); - $result = json_decode($newInstance->put('posts/1', json_encode(array('id' => 1, 'title' => 'test', 'body' => 'blabla', 'userId' => 1)))->getBody()->getContents()); + $result = json_decode($newInstance->put('posts/1', json_encode(['id' => 1, 'title' => 'test', 'body' => 'blabla', 'userId' => 1]))->getBody()->getContents()); $this->assertEquals(1, $result->id); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php deleted file mode 100644 index ec0a61f..0000000 --- a/tests/bootstrap.php +++ /dev/null @@ -1,21 +0,0 @@ -add('Lamia\HttpClient', __DIR__); - -return $loader;