From 1af3625979c05499b986e039de6c22058ac52d20 Mon Sep 17 00:00:00 2001 From: misantron Date: Thu, 4 Oct 2018 19:08:28 +0300 Subject: [PATCH 01/18] Relocate tests bootstrap and add phpunit configuration file --- phpunit.xml.dist | 25 +++++++++++++++++++++++++ test/bootstrap.php | 5 +++++ test/unit/bootstrap.php | 3 --- 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 phpunit.xml.dist create mode 100644 test/bootstrap.php delete mode 100644 test/unit/bootstrap.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..42e43a4 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,25 @@ + + + + + ./test/unit + + + + + lib + + src + + + + \ No newline at end of file diff --git a/test/bootstrap.php b/test/bootstrap.php new file mode 100644 index 0000000..4ff10cc --- /dev/null +++ b/test/bootstrap.php @@ -0,0 +1,5 @@ + Date: Thu, 4 Oct 2018 19:09:15 +0300 Subject: [PATCH 02/18] TravisCI configuration update --- .travis.yml | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0ba5d28..8e76fc6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,20 +1,34 @@ language: php -before_script: -- composer install --dev --no-interaction -- cd test/unit + +php: + - 5.6 + - 7.0 + +cache: + directories: + - vendor + - $HOME/.composer/cache + +install: + - travis_retry composer self-update && composer --version + - export PATH="$HOME/.composer/vendor/bin:$PATH" + - travis_retry composer install --prefer-dist --no-interaction + script: -- ../../vendor/bin/phpunit . --bootstrap bootstrap.php --filter test* --coverage-clover=coverage.xml + - vendor/bin/php-cs-fixer fix --diff --dry-run + - vendor/bin/phpcs -s --config-set ignore_warnings_on_exit 1 + - vendor/bin/phpunit --testsuite unit --coverage-clover=coverage.xml + after_success: -- bash <(curl -s https://codecov.io/bash) -php: -- 5.6 -- 7.0 + - bash <(curl -s https://codecov.io/bash) + env: global: - secure: eUN0huKA436uIkISu6V3yW01/gZHC6slBvlnprMPEhzclU2szH6qhWEXWS5CcOG6EEDBReqLZdNAwu+FC69KGFO9+6oW2GQQLSsfEViqFi/Ig2N0r4EBO4jLRebgq0GOfetPwQvJH27d8BCiloPy8rXBb5pskxSNw4B4bRyXHz4= - secure: j38xzMNmzYXR/JJdVumPmDoDVxb6FUDF497AuVPHowGh0egFW8XHWCOMeQWqWZI4Gg17pViQNIJ3xC6WBvob70AF8fsNm0+vxF2s7abXDMcbq5flLTS6ydKLgNu+na/RAkOBbTwxJAGIH/fQh8BH8iGKerCwoqf8sDErAge4NMw= - secure: h3HlxBOsNXBDrIJ0yl467ST6Q8R2TmbL7PltlPcRoHy5gAxn5UiDv5W2+6DSXrwQrTjOUunZ+O9ckcaQGQy1JNhGMwgIkJpyWAHDIHhTYGU289uUIDTHQW/soX0qHJSHSx3iMgDOIc7XnfTz6W7Nv1gYKZFedOMsZ5uBMeGyiXE= - secure: SKSl/RHFQNhGT7OUj7E0AbrQnuDhhCRI/4jC76mmzvy8EJBDgUNevAKJGtug+LVilHrlbk9fLC8rayPW6SGv0s3PowTGm8NMOc48aRBLOr7QRo/sMikJCmRuU6HWptr0NKuf2fq6lV94WDm/pDdyOSNyLga9/eaIDs/Sacp78sw= + notifications: hipchat: rooms: From 35e414c6810bd1fbdf35706c638690a37366c421 Mon Sep 17 00:00:00 2001 From: misantron Date: Thu, 4 Oct 2018 19:10:34 +0300 Subject: [PATCH 03/18] Composer dependencies update, scripts section added --- composer.json | 82 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/composer.json b/composer.json index 5c0a3af..c9680fa 100644 --- a/composer.json +++ b/composer.json @@ -1,36 +1,54 @@ { - "name": "sendgrid/php-http-client", - "description": "HTTP REST client, simplified for PHP", - "type": "library", - "version": "3.9.6", - "homepage": "http://github.com/sendgrid/php-http-client", - "keywords": ["SendGrid", "HTTP", "REST", "API", "Fluent"], - "license": "MIT", - "authors": [ - { - "name": "Matt Bernier", - "email": "dx@sendgrid.com" - }, - { - "name": "Elmer Thomas", - "email": "elmer@thinkingserious.com" - } - ], - "require": { - "php": ">=5.6" + "name": "sendgrid/php-http-client", + "description": "HTTP REST client, simplified for PHP", + "type": "library", + "version": "3.9.6", + "homepage": "http://github.com/sendgrid/php-http-client", + "keywords": [ + "SendGrid", + "HTTP", + "REST", + "API", + "Fluent" + ], + "license": "MIT", + "authors": [ + { + "name": "Matt Bernier", + "email": "dx@sendgrid.com" }, - "require-dev": { - "phpunit/phpunit": "~4.4", - "squizlabs/php_codesniffer": "~2.0" - }, - "autoload": { - "psr-4": { - "SendGrid\\": "lib/" - } - }, - "autoload-dev": { - "psr-4": { - "SendGrid\\Test\\": "test/unit/" - } + { + "name": "Elmer Thomas", + "email": "elmer@thinkingserious.com" + } + ], + "require": { + "php": ">=5.6", + "ext-json": "*", + "ext-curl": "*" + }, + "require-dev": { + "phpunit/phpunit": "~4.8", + "friendsofphp/php-cs-fixer": "~2.10", + "squizlabs/php_codesniffer": "~3.3" + }, + "autoload": { + "psr-4": { + "SendGrid\\": "lib/" + } + }, + "autoload-dev": { + "psr-4": { + "SendGrid\\Test\\": "test/unit/" } + }, + "scripts": { + "test": "phpunit --testsuite unit", + "php-cs-fix": "php-cs-fixer fix --diff --verbose --config=.php_cs", + "php-cs": "phpcs --standard=PSR1,PSR2 lib" + }, + "config": { + "optimize-autoloader": true, + "sort-packages": true + } } From 8e0a497d80bdfeb3b90d15deed1b7d62cac3647c Mon Sep 17 00:00:00 2001 From: misantron Date: Thu, 4 Oct 2018 19:10:55 +0300 Subject: [PATCH 04/18] Gitignore update --- .gitignore | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b90ffc5..bc212d7 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,9 @@ vendor/ .idea/ # Environment files -.env/*.* \ No newline at end of file +.env/*.* + +# Local configs +.php_cs +.php_cs.cache +phpunit.xml \ No newline at end of file From 2062f55d7008d2e920175e9b5b3372fa71605f84 Mon Sep 17 00:00:00 2001 From: misantron Date: Thu, 4 Oct 2018 19:11:25 +0300 Subject: [PATCH 05/18] PhpCsFixer configuration added --- .php_cs.dist | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 .php_cs.dist diff --git a/.php_cs.dist b/.php_cs.dist new file mode 100644 index 0000000..36b75c8 --- /dev/null +++ b/.php_cs.dist @@ -0,0 +1,33 @@ +in(__DIR__ . '/lib'); + +return PhpCsFixer\Config::create() + ->setRiskyAllowed(true) + ->setRules([ + '@Symfony' => true, + 'binary_operator_spaces' => array( + 'align_equals' => false, + 'align_double_arrow' => false, + ), + '@Symfony:risky' => true, + 'array_syntax' => ['syntax' => 'short'], + 'linebreak_after_opening_tag' => true, + 'mb_str_functions' => true, + 'no_php4_constructor' => true, + 'no_unreachable_default_argument_value' => true, + 'no_useless_else' => true, + 'no_useless_return' => true, + 'ordered_imports' => true, + 'php_unit_strict' => true, + 'phpdoc_order' => true, + 'semicolon_after_instruction' => true, + 'strict_comparison' => true, + 'strict_param' => true, + 'concat_space' => ['spacing' => 'one'], + 'trailing_comma_in_multiline_array' => true, + 'yoda_style' => false, + ]) + ->setFinder($finder) + ->setCacheFile(__DIR__ . '/.php_cs.cache'); From e5aa3579422920c8d8fe6c260b97cfb24473d7f5 Mon Sep 17 00:00:00 2001 From: misantron Date: Thu, 4 Oct 2018 19:12:04 +0300 Subject: [PATCH 06/18] Classes code style automatic fixes --- lib/Client.php | 104 ++++++++++++++++++++++++++--------------------- lib/Response.php | 39 +++++++++--------- 2 files changed, 77 insertions(+), 66 deletions(-) diff --git a/lib/Client.php b/lib/Client.php index ec732b9..43e5b2d 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -1,33 +1,33 @@ * @author Elmer Thomas * @copyright 2018 SendGrid * @license https://opensource.org/licenses/MIT The MIT License + * * @version GIT: - * @link http://packagist.org/packages/sendgrid/php-http-client + * + * @see http://packagist.org/packages/sendgrid/php-http-client */ namespace SendGrid; /** + * Class Client. * - * Class Client - * @package SendGrid - * @version 3.9.5 - * - * Quickly and easily access any REST or REST-like API. + * @version Release: * - * @method Response get($body = null, $query = null, $headers = null) - * @method Response post($body = null, $query = null, $headers = null) - * @method Response patch($body = null, $query = null, $headers = null) - * @method Response put($body = null, $query = null, $headers = null) - * @method Response delete($body = null, $query = null, $headers = null) + * Quickly and easily access any REST or REST-like API. * - * @method Client version($value) + * @method Response get($body = null, $query = null, $headers = null) + * @method Response post($body = null, $query = null, $headers = null) + * @method Response patch($body = null, $query = null, $headers = null) + * @method Response put($body = null, $query = null, $headers = null) + * @method Response delete($body = null, $query = null, $headers = null) + * @method Client version($value) * @method Client|Response send() * * Adding all the endpoints as a method so code completion works @@ -208,24 +208,30 @@ class Client protected $retryOnLimit; /** - * These are the supported HTTP verbs + * Supported HTTP verbs. * * @var array */ private $methods = ['get', 'post', 'patch', 'put', 'delete']; /** - * Initialize the client - * - * @param string $host the base url (e.g. https://api.sendgrid.com) - * @param array $headers global request headers - * @param string $version api version (configurable) - this is specific to the SendGrid API - * @param array $path holds the segments of the url path - * @param array $curlOptions extra options to set during curl initialization - * @param bool $retryOnLimit set default retry on limit flag - */ - public function __construct($host, $headers = null, $version = null, $path = null, $curlOptions = null, $retryOnLimit = false) - { + * Initialize the client. + * + * @param string $host the base url (e.g. https://api.sendgrid.com) + * @param array $headers global request headers + * @param string $version api version (configurable) - this is specific to the SendGrid API + * @param array $path holds the segments of the url path + * @param array $curlOptions extra options to set during curl initialization + * @param bool $retryOnLimit set default retry on limit flag + */ + public function __construct( + $host, + $headers = null, + $version = null, + $path = null, + $curlOptions = null, + $retryOnLimit = false + ) { $this->host = $host; $this->headers = $headers ?: []; $this->version = $version; @@ -277,7 +283,7 @@ public function getCurlOptions() } /** - * Set extra options to set during curl initialization + * Set extra options to set during curl initialization. * * @param array $options * @@ -291,7 +297,7 @@ public function setCurlOptions(array $options) } /** - * Set default retry on limit flag + * Set default retry on limit flag. * * @param bool $retry * @@ -305,7 +311,7 @@ public function setRetryOnLimit($retry) } /** - * Set concurrent request flag + * Set concurrent request flag. * * @param bool $isConcurrent * @@ -319,7 +325,7 @@ public function setIsConcurrentRequest($isConcurrent) } /** - * Build the final URL to be passed + * Build the final URL to be passed. * * @param array $queryParams an array of all the query parameters * @@ -331,16 +337,17 @@ private function buildUrl($queryParams = null) if (isset($queryParams)) { $path .= '?' . http_build_query($queryParams); } + return sprintf('%s%s%s', $this->host, $this->version ?: '', $path); } /** * Creates curl options for a request - * this function does not mutate any private variables + * this function does not mutate any private variables. * * @param string $method - * @param array $body - * @param array $headers + * @param array $body + * @param array $headers * * @return array */ @@ -349,9 +356,9 @@ private function createCurlOptions($method, $body = null, $headers = null) $options = [ CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, - CURLOPT_CUSTOMREQUEST => strtoupper($method), + CURLOPT_CUSTOMREQUEST => mb_strtoupper($method), CURLOPT_SSL_VERIFYPEER => true, - CURLOPT_FAILONERROR => false + CURLOPT_FAILONERROR => false, ] + $this->curlOptions; if (isset($headers)) { @@ -371,9 +378,8 @@ private function createCurlOptions($method, $body = null, $headers = null) } /** - * @param array $requestData - * e.g. ['method' => 'POST', 'url' => 'www.example.com', 'body' => 'test body', 'headers' => []] - * @param bool $retryOnLimit + * @param array $requestData (method, url, body and headers) + * @param bool $retryOnLimit * * @return array */ @@ -403,9 +409,9 @@ private function createCurlMultiHandle(array $requests) } /** - * Prepare response object + * Prepare response object. * - * @param resource $channel the curl resource + * @param resource $channel the curl resource * @param string $content * * @return Response object @@ -415,9 +421,9 @@ private function parseResponse($channel, $content) $headerSize = curl_getinfo($channel, CURLINFO_HEADER_SIZE); $statusCode = curl_getinfo($channel, CURLINFO_HTTP_CODE); - $responseBody = substr($content, $headerSize); + $responseBody = mb_substr($content, $headerSize); - $responseHeaders = substr($content, 0, $headerSize); + $responseHeaders = mb_substr($content, 0, $headerSize); $responseHeaders = explode("\n", $responseHeaders); $responseHeaders = array_map('trim', $responseHeaders); @@ -425,7 +431,7 @@ private function parseResponse($channel, $content) } /** - * Retry request + * Retry request. * * @param array $responseHeaders headers from rate limited response * @param string $method the HTTP verb @@ -439,6 +445,7 @@ private function retryRequest(array $responseHeaders, $method, $url, $body, $hea { $sleepDurations = $responseHeaders['X-Ratelimit-Reset'] - time(); sleep($sleepDurations > 0 ? $sleepDurations : 0); + return $this->makeRequest($method, $url, $body, $headers, false); } @@ -467,6 +474,7 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry if ($response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE && $retryOnLimit) { $responseHeaders = $response->headers(true); + return $this->retryRequest($responseHeaders, $method, $url, $body, $headers); } @@ -476,7 +484,7 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry } /** - * Send all saved requests at once + * Send all saved requests at once. * * @param array $requests * @@ -500,7 +508,6 @@ public function makeAllRequests(array $requests = []) $responses = []; $sleepDurations = 0; foreach ($channels as $id => $channel) { - $content = curl_multi_getcontent($channel); $response = $this->parseResponse($channel, $content); @@ -527,6 +534,7 @@ public function makeAllRequests(array $requests = []) sleep($sleepDurations > 0 ? $sleepDurations : 0); $responses = array_merge($responses, $this->makeAllRequests($retryRequests)); } + return $responses; } @@ -553,7 +561,7 @@ public function _($name = null) /** * Dynamically add method calls to the url, then call a method. - * (e.g. client.name.name.method()) + * (e.g. client.name.name.method()). * * @param string $name name of the dynamic method call or HTTP verb * @param array $args parameters passed with the method call @@ -562,10 +570,11 @@ public function _($name = null) */ public function __call($name, $args) { - $name = strtolower($name); + $name = mb_strtolower($name); if ($name === 'version') { $this->version = $args[0]; + return $this->_(); } @@ -574,7 +583,7 @@ public function __call($name, $args) return $this->makeAllRequests(); } - if (in_array($name, $this->methods, true)) { + if (\in_array($name, $this->methods, true)) { $body = isset($args[0]) ? $args[0] : null; $queryParams = isset($args[1]) ? $args[1] : null; $url = $this->buildUrl($queryParams); @@ -585,6 +594,7 @@ public function __call($name, $args) // save request to be sent later $requestData = ['method' => $name, 'url' => $url, 'body' => $body, 'headers' => $headers]; $this->savedRequests[] = $this->createSavedRequest($requestData, $retryOnLimit); + return null; } diff --git a/lib/Response.php b/lib/Response.php index f658f2c..be9c1cb 100644 --- a/lib/Response.php +++ b/lib/Response.php @@ -1,14 +1,16 @@ * @author Elmer Thomas * @copyright 2018 SendGrid * @license https://opensource.org/licenses/MIT The MIT License + * * @version GIT: - * @link http://packagist.org/packages/sendgrid/php-http-client + * + * @see http://packagist.org/packages/sendgrid/php-http-client */ namespace SendGrid; @@ -34,11 +36,11 @@ class Response protected $headers; /** - * Setup the response data + * Setup the response data. * - * @param int $statusCode the status code. - * @param string $body the response body. - * @param array $headers an array of response headers. + * @param int $statusCode the status code + * @param string $body the response body + * @param array $headers an array of response headers */ public function __construct($statusCode = 200, $body = '', array $headers = []) { @@ -48,7 +50,7 @@ public function __construct($statusCode = 200, $body = '', array $headers = []) } /** - * The status code + * The status code. * * @return int */ @@ -58,7 +60,7 @@ public function statusCode() } /** - * The response body + * The response body. * * @return string */ @@ -68,7 +70,7 @@ public function body() } /** - * The response headers + * The response headers. * * @param bool $assoc * @@ -79,24 +81,23 @@ public function headers($assoc = false) if (!$assoc) { return $this->headers; } - + return $this->prettifyHeaders($this->headers); } - + /** - * Returns response headers as associative array - * - * @param array $headers - * - * @return array - */ + * Returns response headers as associative array. + * + * @param array $headers + * + * @return array + */ private function prettifyHeaders(array $headers) { return array_reduce( array_filter($headers), function ($result, $header) { - - if (false === strpos($header, ':')) { + if (false === mb_strpos($header, ':')) { $result['Status'] = trim($header); return $result; From 50f3b72ba820d706649ac17978287ff9f2b62b01 Mon Sep 17 00:00:00 2001 From: misantron Date: Thu, 4 Oct 2018 19:25:02 +0300 Subject: [PATCH 07/18] Method description fix --- lib/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Client.php b/lib/Client.php index 43e5b2d..d0548fc 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -378,7 +378,7 @@ private function createCurlOptions($method, $body = null, $headers = null) } /** - * @param array $requestData (method, url, body and headers) + * @param array $requestData (method, url, body and headers) * @param bool $retryOnLimit * * @return array From 9bbcf558bb44b436ac754967ead167f04ce3d90f Mon Sep 17 00:00:00 2001 From: misantron Date: Tue, 9 Oct 2018 11:45:05 +0300 Subject: [PATCH 08/18] Composer ext-mbstring dependency added --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c9680fa..fd69ac4 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,8 @@ "require": { "php": ">=5.6", "ext-json": "*", - "ext-curl": "*" + "ext-curl": "*", + "ext-mbstring": "*" }, "require-dev": { "phpunit/phpunit": "~4.8", From 9004f53ac074c5c594e5e1b50d8001c2741003f8 Mon Sep 17 00:00:00 2001 From: misantron Date: Tue, 9 Oct 2018 11:46:12 +0300 Subject: [PATCH 09/18] EOF fixes added --- .gitignore | 2 +- phpunit.xml.dist | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index bc212d7..c527c3a 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,4 @@ vendor/ # Local configs .php_cs .php_cs.cache -phpunit.xml \ No newline at end of file +phpunit.xml diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 42e43a4..41a2ded 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,6 @@ - + bootstrap="./test/bootstrap.php"> ./test/unit @@ -22,4 +22,4 @@ - \ No newline at end of file + From fa85386e96557e850b7d897053e68f97aee1327c Mon Sep 17 00:00:00 2001 From: Aleksandr Ivanov Date: Fri, 10 Jul 2020 11:47:43 +0300 Subject: [PATCH 10/18] Actualize code style auto check --- .php_cs.dist | 29 ++++++++--------------------- Makefile | 2 +- composer.json | 8 +++++--- lib/Client.php | 12 ++++++------ lib/Exception/InvalidRequest.php | 7 ++----- lib/Response.php | 4 ++-- phpunit.xml.dist | 25 ------------------------- test/bootstrap.php | 5 ----- test/unit/ClientTest.php | 14 +++++++++----- 9 files changed, 33 insertions(+), 73 deletions(-) delete mode 100644 phpunit.xml.dist delete mode 100644 test/bootstrap.php diff --git a/.php_cs.dist b/.php_cs.dist index 36b75c8..c906f2e 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -6,28 +6,15 @@ $finder = PhpCsFixer\Finder::create() return PhpCsFixer\Config::create() ->setRiskyAllowed(true) ->setRules([ + '@PSR2' => true, '@Symfony' => true, - 'binary_operator_spaces' => array( - 'align_equals' => false, - 'align_double_arrow' => false, - ), - '@Symfony:risky' => true, 'array_syntax' => ['syntax' => 'short'], - 'linebreak_after_opening_tag' => true, - 'mb_str_functions' => true, - 'no_php4_constructor' => true, - 'no_unreachable_default_argument_value' => true, - 'no_useless_else' => true, - 'no_useless_return' => true, - 'ordered_imports' => true, - 'php_unit_strict' => true, - 'phpdoc_order' => true, - 'semicolon_after_instruction' => true, - 'strict_comparison' => true, - 'strict_param' => true, - 'concat_space' => ['spacing' => 'one'], - 'trailing_comma_in_multiline_array' => true, + 'cast_spaces' => ['space' => 'single'], 'yoda_style' => false, + 'concat_space' => ['spacing' => 'one'], + 'phpdoc_summary' => false, + 'combine_consecutive_unsets' => true, + 'final_internal_class' => true, + 'global_namespace_import' => ['import_classes' => false], ]) - ->setFinder($finder) - ->setCacheFile(__DIR__ . '/.php_cs.cache'); + ->setFinder($finder); diff --git a/Makefile b/Makefile index 260a6d0..8fe298f 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ clean: @rm -rf vendor composer.lock install: clean - composer install + composer install --no-suggest --no-scripts --no-progress --no-interaction test: install vendor/bin/phpunit test/unit diff --git a/composer.json b/composer.json index 146d016..5a929b3 100644 --- a/composer.json +++ b/composer.json @@ -18,11 +18,13 @@ "require": { "php": ">=5.6", "ext-curl": "*", - "ext-json": "*" + "ext-json": "*", + "ext-mbstring": "*" }, "require-dev": { - "phpunit/phpunit": "~4.4", - "squizlabs/php_codesniffer": "~2.0" + "phpunit/phpunit": "^4.8 || ^5.7", + "squizlabs/php_codesniffer": "~2.0", + "friendsofphp/php-cs-fixer": "^2.16" }, "autoload": { "psr-4": { diff --git a/lib/Client.php b/lib/Client.php index 7b8fc6f..77b092c 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -9,9 +9,7 @@ use SendGrid\Exception\InvalidRequest; /** - * * Class Client - * @package SendGrid * @version 3.9.5 * * Quickly and easily access any REST or REST-like API. @@ -76,7 +74,6 @@ * @method Client segments() * @method Client singlesends() * - * * Devices * @method Client devices() * @@ -354,7 +351,7 @@ private function createCurlOptions($method, $body = null, $headers = null) $options = [ CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, - CURLOPT_CUSTOMREQUEST => mb_strtoupper($method), + CURLOPT_CUSTOMREQUEST => strtoupper($method), CURLOPT_SSL_VERIFYPEER => true, CURLOPT_FAILONERROR => false, ] + $this->curlOptions; @@ -438,6 +435,7 @@ private function parseResponse($channel, $content) * @param array $headers original headers * * @return Response response object + * * @throws InvalidRequest */ private function retryRequest(array $responseHeaders, $method, $url, $body, $headers) @@ -459,6 +457,7 @@ private function retryRequest(array $responseHeaders, $method, $url, $body, $hea * @param bool $retryOnLimit should retry if rate limit is reach? * * @return Response object + * * @throws InvalidRequest */ public function makeRequest($method, $url, $body = null, $headers = null, $retryOnLimit = false) @@ -476,7 +475,7 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry $response = $this->parseResponse($channel, $content); - if ($response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE && $retryOnLimit) { + if ($retryOnLimit && $response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE) { $responseHeaders = $response->headers(true); return $this->retryRequest($responseHeaders, $method, $url, $body, $headers); @@ -515,7 +514,7 @@ public function makeAllRequests(array $requests = []) $content = curl_multi_getcontent($channel); $response = $this->parseResponse($channel, $content); - if ($response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE && $requests[$id]['retryOnLimit']) { + if ($requests[$id]['retryOnLimit'] && $response->statusCode() === self::TOO_MANY_REQUESTS_HTTP_CODE) { $headers = $response->headers(true); $sleepDurations = max($sleepDurations, $headers['X-Ratelimit-Reset'] - time()); $requestData = [ @@ -571,6 +570,7 @@ public function _($name = null) * @param array $args parameters passed with the method call * * @return Client|Response|Response[]|null object + * * @throws InvalidRequest */ public function __call($name, $args) diff --git a/lib/Exception/InvalidRequest.php b/lib/Exception/InvalidRequest.php index 07c8e33..21ae2ac 100644 --- a/lib/Exception/InvalidRequest.php +++ b/lib/Exception/InvalidRequest.php @@ -6,17 +6,14 @@ * Class InvalidHttpRequest * * Thrown when invalid payload was constructed, which could not reach SendGrid server. - * - * @package SendGrid\Exception */ class InvalidRequest extends \Exception { public function __construct( - $message = "", + $message = '', $code = 0, \Exception $previous = null - ) - { + ) { $message = 'Could not send request to server. ' . 'CURL error ' . $code . ': ' . $message; parent::__construct($message, $code, $previous); diff --git a/lib/Response.php b/lib/Response.php index c8a9713..376af50 100644 --- a/lib/Response.php +++ b/lib/Response.php @@ -87,8 +87,8 @@ private function prettifyHeaders(array $headers) { return array_reduce( array_filter($headers), - function ($result, $header) { - if (false === mb_strpos($header, ':')) { + static function ($result, $header) { + if (mb_strpos($header, ':') === false) { $result['Status'] = trim($header); return $result; diff --git a/phpunit.xml.dist b/phpunit.xml.dist deleted file mode 100644 index 41a2ded..0000000 --- a/phpunit.xml.dist +++ /dev/null @@ -1,25 +0,0 @@ - - - - - ./test/unit - - - - - lib - - src - - - - diff --git a/test/bootstrap.php b/test/bootstrap.php deleted file mode 100644 index 4ff10cc..0000000 --- a/test/bootstrap.php +++ /dev/null @@ -1,5 +0,0 @@ -assertSame(['Content-Type: application/json', 'Authorization: Bearer SG.XXXX'], $client->getHeaders()); $client2 = new Client('https://localhost:4010'); @@ -92,7 +95,7 @@ public function testGetVersion() $this->assertSame('/v3', $client->getVersion()); $client = new Client('https://localhost:4010'); - $this->assertSame(null, $client->getVersion()); + $this->assertNull($client->getVersion()); } public function testGetPath() @@ -123,7 +126,7 @@ public function testCurlMulti() $client->get(null, ['limit' => 100, 'offset' => 0]); // returns 3 response object - $this->assertEquals(3, count($client->send())); + $this->assertCount(3, $client->send()); } public function testCreateCurlOptionsWithMethodOnly() @@ -199,8 +202,9 @@ public function testCreateCurlOptionsWithBodyAndHeaders() public function testThrowExceptionOnInvalidCall() { - $this->setExpectedException(InvalidRequest::class); - $client = new Client('invalid://url',['User-Agent: Custom-Client 1.0']); + $this->expectException(InvalidRequest::class); + + $client = new Client('invalid://url', ['User-Agent: Custom-Client 1.0']); $client->get(); } From 7111791ff2135924a9de873c0a4ae5d2a6a65af4 Mon Sep 17 00:00:00 2001 From: Aleksandr Ivanov Date: Fri, 10 Jul 2020 12:04:35 +0300 Subject: [PATCH 11/18] Refactoring TravisCI config - try improve version matrix run --- .travis.yml | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6caa598..1d1a064 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,5 @@ language: php -before_script: - - if [ -n "$GIT_HUB_TOKEN" ]; then composer config -g github-oauth.github.com "$GIT_HUB_TOKEN"; fi; - - composer install - - if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest --prefer-stable -n; fi; - -script: - - make test - -after_success: - - bash <(curl -s https://codecov.io/bash) - php: - 5.6 - 7.0 @@ -20,8 +9,30 @@ php: - 7.4 env: - - dependencies=lowest - - dependencies=highest + matrix: + - dependencies=lowest + - dependencies=highest + global: + - composer_flags="--no-interaction --no-ansi --no-progress --no-suggest" + +before_install: + - composer clear-cache + +install: + - if [ -n "$GIT_HUB_TOKEN" ]; then composer config -g github-oauth.github.com "$GIT_HUB_TOKEN"; fi; + - if [ "$dependencies" = "highest" ]; then composer update $composer_flags; fi; + - if [ "$dependencies" = "lowest" ]; then composer update $composer_flags --prefer-lowest --prefer-stable; fi; + +#before_script: +# - if [ -n "$GIT_HUB_TOKEN" ]; then composer config -g github-oauth.github.com "$GIT_HUB_TOKEN"; fi; +# - composer install +# - if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest --prefer-stable -n; fi; + +script: + - make test + +after_success: + - bash <(curl -s https://codecov.io/bash) notifications: slack: From ca267d8041ddeae882944a9750ad52b0dcd084c2 Mon Sep 17 00:00:00 2001 From: Aleksandr Ivanov Date: Fri, 10 Jul 2020 12:09:52 +0300 Subject: [PATCH 12/18] Use own dependencies version for each build, do not reinstall it inside Make command --- .travis.yml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1d1a064..48e2d8e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,13 +23,8 @@ install: - if [ "$dependencies" = "highest" ]; then composer update $composer_flags; fi; - if [ "$dependencies" = "lowest" ]; then composer update $composer_flags --prefer-lowest --prefer-stable; fi; -#before_script: -# - if [ -n "$GIT_HUB_TOKEN" ]; then composer config -g github-oauth.github.com "$GIT_HUB_TOKEN"; fi; -# - composer install -# - if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest --prefer-stable -n; fi; - script: - - make test + - vendor/bin/phpunit test/unit after_success: - bash <(curl -s https://codecov.io/bash) From 7e4b287f3c53e107ee2e270fda4d1fcd28cb4b73 Mon Sep 17 00:00:00 2001 From: Aleksandr Ivanov Date: Fri, 10 Jul 2020 12:10:55 +0300 Subject: [PATCH 13/18] Add Composer self-update before build run --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 48e2d8e..c5f10bd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,6 +16,7 @@ env: - composer_flags="--no-interaction --no-ansi --no-progress --no-suggest" before_install: + - composer self-update - composer clear-cache install: From 968078e8d30931d198b98ad879bc33a2e65d22e7 Mon Sep 17 00:00:00 2001 From: Aleksandr Ivanov Date: Fri, 10 Jul 2020 12:49:25 +0300 Subject: [PATCH 14/18] Try to fix lowest dependencies builds run --- .travis.yml | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index c5f10bd..74640f1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,7 +22,7 @@ before_install: install: - if [ -n "$GIT_HUB_TOKEN" ]; then composer config -g github-oauth.github.com "$GIT_HUB_TOKEN"; fi; - if [ "$dependencies" = "highest" ]; then composer update $composer_flags; fi; - - if [ "$dependencies" = "lowest" ]; then composer update $composer_flags --prefer-lowest --prefer-stable; fi; + - if [ "$dependencies" = "lowest" ]; then composer update $composer_flags --prefer-lowest; fi; script: - vendor/bin/phpunit test/unit diff --git a/composer.json b/composer.json index 5a929b3..9ce52a2 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "ext-mbstring": "*" }, "require-dev": { - "phpunit/phpunit": "^4.8 || ^5.7", + "phpunit/phpunit": "^4.8 | ^5.7", "squizlabs/php_codesniffer": "~2.0", "friendsofphp/php-cs-fixer": "^2.16" }, From d4479547a8404300dd3d18b6c89b9377a4dccd6f Mon Sep 17 00:00:00 2001 From: Aleksandr Ivanov Date: Fri, 10 Jul 2020 13:39:22 +0300 Subject: [PATCH 15/18] Try to fix lowest dependencies builds run --- .travis.yml | 6 +++--- composer.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 74640f1..4fe3f1c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ env: - dependencies=lowest - dependencies=highest global: - - composer_flags="--no-interaction --no-ansi --no-progress --no-suggest" + - composer_flags="--no-interaction --no-ansi --no-progress --no-suggest --verbose" before_install: - composer self-update @@ -22,10 +22,10 @@ before_install: install: - if [ -n "$GIT_HUB_TOKEN" ]; then composer config -g github-oauth.github.com "$GIT_HUB_TOKEN"; fi; - if [ "$dependencies" = "highest" ]; then composer update $composer_flags; fi; - - if [ "$dependencies" = "lowest" ]; then composer update $composer_flags --prefer-lowest; fi; + - if [ "$dependencies" = "lowest" ]; then composer update $composer_flags --prefer-lowest --prefer-stable; fi; script: - - vendor/bin/phpunit test/unit + - ./vendor/bin/phpunit after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/composer.json b/composer.json index 9ce52a2..5a929b3 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "ext-mbstring": "*" }, "require-dev": { - "phpunit/phpunit": "^4.8 | ^5.7", + "phpunit/phpunit": "^4.8 || ^5.7", "squizlabs/php_codesniffer": "~2.0", "friendsofphp/php-cs-fixer": "^2.16" }, From d7f909864c1b9de56b6e5472e355a9f8f817df8d Mon Sep 17 00:00:00 2001 From: Aleksandr Ivanov Date: Fri, 10 Jul 2020 13:44:49 +0300 Subject: [PATCH 16/18] No need to support PHPUnit 4 --- .travis.yml | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4fe3f1c..63cbb5f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,7 +25,7 @@ install: - if [ "$dependencies" = "lowest" ]; then composer update $composer_flags --prefer-lowest --prefer-stable; fi; script: - - ./vendor/bin/phpunit + - vendor/bin/phpunit test/unit after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/composer.json b/composer.json index 5a929b3..528c56a 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "ext-mbstring": "*" }, "require-dev": { - "phpunit/phpunit": "^4.8 || ^5.7", + "phpunit/phpunit": "^5.7 || ^6.5", "squizlabs/php_codesniffer": "~2.0", "friendsofphp/php-cs-fixer": "^2.16" }, From ee55febbc53903a071656e75f29559a8331a098f Mon Sep 17 00:00:00 2001 From: Aleksandr Ivanov Date: Fri, 10 Jul 2020 13:49:49 +0300 Subject: [PATCH 17/18] Bump PHPUnit version to 5.7 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 528c56a..3330ff2 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "ext-mbstring": "*" }, "require-dev": { - "phpunit/phpunit": "^5.7 || ^6.5", + "phpunit/phpunit": "^5.7", "squizlabs/php_codesniffer": "~2.0", "friendsofphp/php-cs-fixer": "^2.16" }, From 96859ef4d3270e9cda03b5acaadba9c5f1751510 Mon Sep 17 00:00:00 2001 From: Aleksandr Ivanov Date: Fri, 10 Jul 2020 14:33:26 +0300 Subject: [PATCH 18/18] Fix to display correct PHPUnit version on lowest dependencies build run --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 3330ff2..8f79aeb 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ }, "require-dev": { "phpunit/phpunit": "^5.7", + "sebastian/version": "^1.0.6", "squizlabs/php_codesniffer": "~2.0", "friendsofphp/php-cs-fixer": "^2.16" },