From 3229ffd1e78fc8cb09302b93c53afb7267cae37f Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Mon, 13 Jul 2020 17:50:07 +0100 Subject: [PATCH 1/3] Use hrtime over microtime --- composer.json | 3 ++- src/LoggerPlugin.php | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 9c69086..45b464f 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ "php": "^5.4 || ^7.0", "psr/log": "^1.0", "php-http/client-common": "^1.9 || ^2.0", - "php-http/message": "^1.0" + "php-http/message": "^1.0", + "symfony/polyfill-php73": "^1.17" }, "require-dev": { "phpspec/phpspec": "^2.5", diff --git a/src/LoggerPlugin.php b/src/LoggerPlugin.php index c822cbc..7bcb0e5 100644 --- a/src/LoggerPlugin.php +++ b/src/LoggerPlugin.php @@ -31,11 +31,11 @@ public function __construct(LoggerInterface $logger, Formatter $formatter = null protected function doHandleRequest(RequestInterface $request, callable $next, callable $first) { - $start = microtime(true); + $start = hrtime(true)/1E6; $this->logger->info(sprintf("Sending request:\n%s", $this->formatter->formatRequest($request)), ['request' => $request]); return $next($request)->then(function (ResponseInterface $response) use ($request, $start) { - $milliseconds = (int) round((microtime(true) - $start) * 1000); + $milliseconds = (int) round(hrtime(true)/1E6 - $start); $this->logger->info( sprintf("Received response:\n%s\n\nfor request:\n%s", $this->formatter->formatResponse($response), $this->formatter->formatRequest($request)), [ @@ -47,7 +47,7 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca return $response; }, function (Exception $exception) use ($request, $start) { - $milliseconds = (int) round((microtime(true) - $start) * 1000); + $milliseconds = (int) round((hrtime(true)/1E6 - $start)); if ($exception instanceof Exception\HttpException) { $this->logger->error( sprintf("Error:\n%s\nwith response:\n%s\n\nwhen sending request:\n%s", $exception->getMessage(), $this->formatter->formatResponse($exception->getResponse()), $this->formatter->formatRequest($request)), From 4310844399780a1b44b1230db281242991401b78 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Mon, 13 Jul 2020 17:50:22 +0100 Subject: [PATCH 2/3] Support PHP 7.1-8.0 --- .gitattributes | 3 -- .github/workflows/tests.yml | 91 +++++++++++++++++++++++++++++++++++++ .travis.yml | 42 ----------------- composer.json | 11 ++--- phpspec.yml.ci | 2 +- spec/LoggerPluginSpec.php | 8 ++-- 6 files changed, 101 insertions(+), 56 deletions(-) create mode 100644 .github/workflows/tests.yml delete mode 100644 .travis.yml diff --git a/.gitattributes b/.gitattributes index 7ae0617..cd0ff7b 100644 --- a/.gitattributes +++ b/.gitattributes @@ -5,9 +5,6 @@ .php_cs export-ignore .scrutinizer.yml export-ignore .styleci.yml export-ignore -.travis.yml export-ignore phpspec.yml.ci export-ignore phpspec.yml.dist export-ignore -phpunit.xml.dist export-ignore spec/ export-ignore -tests/ export-ignore diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..e4d0050 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,91 @@ +name: tests + +on: + push: + pull_request: + +jobs: + latest: + name: PHP ${{ matrix.php }} Latest + runs-on: ubuntu-latest + strategy: + matrix: + php: ['7.1', '7.2', '7.3', '7.4', '8.0'] + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + tools: composer:v2 + coverage: none + + - name: Install PHP 7 dependencies + run: composer update --prefer-dist --no-interaction --no-progress + if: "matrix.php != '8.0'" + + - name: Install PHP 8 dependencies + run: | + composer require "phpdocumentor/reflection-docblock:^5.2@dev" --no-interaction --no-update + composer update --prefer-dist --prefer-stable --no-interaction --no-progress --ignore-platform-req=php + if: "matrix.php == '8.0'" + + - name: Execute tests + run: composer test + + lowest: + name: PHP ${{ matrix.php }} Lowest + runs-on: ubuntu-latest + strategy: + matrix: + php: ['7.1', '7.2', '7.3', '7.4'] + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + tools: composer:v2 + coverage: none + + - name: Install dependencies + run: | + composer require "sebastian/comparator:^3.0.2" --no-interaction --no-update + composer update --prefer-dist --prefer-stable --prefer-lowest --no-interaction --no-progress + + - name: Execute tests + run: composer test + + coverage: + name: Code Coverage + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 7.4 + tools: composer:v2 + coverage: xdebug + + - name: Install dependencies + run: | + composer require "friends-of-phpspec/phpspec-code-coverage:^4.3.2" --no-interaction --no-update + composer update --prefer-dist --no-interaction --no-progress + + - name: Execute tests + run: composer test-ci + + - name: Upload coverage + run: | + wget https://scrutinizer-ci.com/ocular.phar + php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 2d2dc53..0000000 --- a/.travis.yml +++ /dev/null @@ -1,42 +0,0 @@ -language: php - -sudo: false - -cache: - directories: - - $HOME/.composer/cache/files - -php: - - 5.4 - - 5.5 - - 5.6 - - 7.0 - - 7.1 - - 7.2 - - 7.3 - -env: - global: - - TEST_COMMAND="composer test" - -branches: - except: - - /^analysis-.*$/ - -matrix: - fast_finish: true - include: - - php: 5.4 - env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci" - - php: 7.1 - env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" - -install: - - composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction - -script: - - $TEST_COMMAND - -after_success: - - if [[ "$COVERAGE" = true ]]; then wget https://scrutinizer-ci.com/ocular.phar; fi - - if [[ "$COVERAGE" = true ]]; then php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml; fi diff --git a/composer.json b/composer.json index 45b464f..d2d0c67 100644 --- a/composer.json +++ b/composer.json @@ -11,15 +11,14 @@ } ], "require": { - "php": "^5.4 || ^7.0", + "php": "^7.1 || ^7.0", "psr/log": "^1.0", "php-http/client-common": "^1.9 || ^2.0", "php-http/message": "^1.0", "symfony/polyfill-php73": "^1.17" }, "require-dev": { - "phpspec/phpspec": "^2.5", - "henrikbjorn/phpspec-code-coverage" : "^1.0" + "phpspec/phpspec": "^5.1 || ^6.0" }, "autoload": { "psr-4": { @@ -32,9 +31,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.1-dev" + "dev-master": "1.2-dev" } - }, - "prefer-stable": true, - "minimum-stability": "dev" + } } diff --git a/phpspec.yml.ci b/phpspec.yml.ci index 979146e..087f808 100644 --- a/phpspec.yml.ci +++ b/phpspec.yml.ci @@ -4,7 +4,7 @@ suites: psr4_prefix: Http\Client\Common\Plugin formatter.name: pretty extensions: - - PhpSpec\Extension\CodeCoverageExtension + FriendsOfPhpSpec\PhpSpec\CodeCoverage\CodeCoverageExtension: ~ code_coverage: format: clover output: build/coverage.xml diff --git a/spec/LoggerPluginSpec.php b/spec/LoggerPluginSpec.php index 07642ab..ad8ab9a 100644 --- a/spec/LoggerPluginSpec.php +++ b/spec/LoggerPluginSpec.php @@ -2,6 +2,8 @@ namespace spec\Http\Client\Common\Plugin; +use Http\Client\Common\Plugin; +use Http\Client\Common\Plugin\LoggerPlugin; use Http\Client\Exception\HttpException; use Http\Client\Exception\NetworkException; use Http\Promise\FulfilledPromise; @@ -23,12 +25,12 @@ function let(LoggerInterface $logger, Formatter $formatter) function it_is_initializable() { - $this->shouldHaveType('Http\Client\Common\Plugin\LoggerPlugin'); + $this->shouldHaveType(LoggerPlugin::class); } function it_is_a_plugin() { - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } function it_logs_request_and_response( @@ -106,7 +108,7 @@ function(array $context) use ($request, $response, $exception) { && $context['response'] === $response->getWrappedObject() && $context['exception'] === $exception && is_int($context['milliseconds']) - ; + ; } ) )->shouldBeCalled(); From 1de6dce2801bd07f6476ade8d4de41c69bca1d94 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Mon, 13 Jul 2020 18:16:54 +0100 Subject: [PATCH 3/3] Fixed CS --- src/LoggerPlugin.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/LoggerPlugin.php b/src/LoggerPlugin.php index 7bcb0e5..6a6859a 100644 --- a/src/LoggerPlugin.php +++ b/src/LoggerPlugin.php @@ -31,11 +31,11 @@ public function __construct(LoggerInterface $logger, Formatter $formatter = null protected function doHandleRequest(RequestInterface $request, callable $next, callable $first) { - $start = hrtime(true)/1E6; + $start = hrtime(true) / 1E6; $this->logger->info(sprintf("Sending request:\n%s", $this->formatter->formatRequest($request)), ['request' => $request]); return $next($request)->then(function (ResponseInterface $response) use ($request, $start) { - $milliseconds = (int) round(hrtime(true)/1E6 - $start); + $milliseconds = (int) round(hrtime(true) / 1E6 - $start); $this->logger->info( sprintf("Received response:\n%s\n\nfor request:\n%s", $this->formatter->formatResponse($response), $this->formatter->formatRequest($request)), [ @@ -47,7 +47,7 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca return $response; }, function (Exception $exception) use ($request, $start) { - $milliseconds = (int) round((hrtime(true)/1E6 - $start)); + $milliseconds = (int) round((hrtime(true) / 1E6 - $start)); if ($exception instanceof Exception\HttpException) { $this->logger->error( sprintf("Error:\n%s\nwith response:\n%s\n\nwhen sending request:\n%s", $exception->getMessage(), $this->formatter->formatResponse($exception->getResponse()), $this->formatter->formatRequest($request)),