From 3b3a91c44910980dd6be715d366e60b5ff7218b3 Mon Sep 17 00:00:00 2001 From: hedii Date: Sat, 11 Dec 2021 10:42:53 +0100 Subject: [PATCH] Refactor for php 8 --- .github/workflows/build.yml | 4 +++- .gitignore | 1 + LICENSE.md | 10 +++++----- README.md | 11 ++++++++--- composer.json | 6 +++--- phpunit.xml => phpunit.xml.dist | 3 +++ src/ColissimoApi.php | 31 ++++++++----------------------- tests/ColissimoApiTest.php | 30 ++++++------------------------ 8 files changed, 37 insertions(+), 59 deletions(-) rename phpunit.xml => phpunit.xml.dist (77%) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d753cae..e098164 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -5,7 +5,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-versions: [ '7.3', '7.4', '8.0' ] + php-versions: [ '8.0', '8.1' ] name: Testing on PHP ${{ matrix.php-versions }} steps: - name: Checkout @@ -19,4 +19,6 @@ jobs: - name: Install dependencies run: composer install --quiet --no-ansi --no-interaction --no-scripts --no-progress - name: Run tests + env: + COLISSIMO_ID: ${{ secrets.COLISSIMO_ID }} run: composer test diff --git a/.gitignore b/.gitignore index ae08c2b..40c006c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .DS_Store composer.lock .phpunit.result.cache +/phpunit.xml diff --git a/LICENSE.md b/LICENSE.md index 3dfb5c5..336e9be 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2016 hedii +Copyright (c) Hedi Chaibi (hedii) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md index f234f8d..ba0267f 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A php package to track Colissimo (La Poste) parcels ### Requirements -- PHP 7.3, 7.4 or 8.0 +- PHP >=8.0 - Curl PHP Extension - Json PHP Extension @@ -251,9 +251,14 @@ array(4) { } ```` -### Run tests +### Testing + +Update the id in `phpunit.xml` file and run: ````bash composer test ```` -You may need to update the id in */tests/ColissimoApiTest.php* because the id is only valid for 90 days. + +## License + +colissimo-api is released under the MIT Licence. See the bundled [LICENSE](https://github.com/hedii/colissimo-api/blob/master/LICENSE.md) file for details. diff --git a/composer.json b/composer.json index 0c1d8b6..c94b133 100644 --- a/composer.json +++ b/composer.json @@ -17,13 +17,13 @@ "email": "contact@hedichaibi.com" }, "require": { - "php": "^7.3|^8.0", + "php": "^8.0", "ext-curl": "*", "ext-json": "*", - "guzzlehttp/guzzle": "^6.5|7.0.1" + "guzzlehttp/guzzle": "^7.0.1" }, "require-dev": { - "phpunit/phpunit": "^9.3.3" + "phpunit/phpunit": "^9.5.10" }, "autoload": { "psr-4": { diff --git a/phpunit.xml b/phpunit.xml.dist similarity index 77% rename from phpunit.xml rename to phpunit.xml.dist index e679df3..935c776 100644 --- a/phpunit.xml +++ b/phpunit.xml.dist @@ -7,4 +7,7 @@ ./tests/ + + + diff --git a/src/ColissimoApi.php b/src/ColissimoApi.php index e6a0c1e..bb2e976 100644 --- a/src/ColissimoApi.php +++ b/src/ColissimoApi.php @@ -2,42 +2,28 @@ namespace Hedii\ColissimoApi; -use Exception; use GuzzleHttp\Client; use GuzzleHttp\Cookie\SetCookie; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\RequestException; +use Throwable; class ColissimoApi { - /** - * The http client instance. - * - * @var \GuzzleHttp\Client - */ - private $client; + private Client $client; - /** - * ColissimoApi constructor. - * - * @param int $connectionTimeout - * @param int $timeout - * @param bool $verify - */ public function __construct(int $connectionTimeout = 10, int $timeout = 30, bool $verify = false) { $this->client = new Client([ 'connect_timeout' => $connectionTimeout, 'timeout' => $timeout, - 'verify' => $verify + 'verify' => $verify, ]); } /** * Get the colissimo status. * - * @param string $id - * @return array * @throws \Hedii\ColissimoApi\ColissimoApiException */ public function get(string $id): array @@ -49,11 +35,11 @@ public function get(string $id): array 'Accept' => 'application/json', 'referer' => "https://www.laposte.fr/outils/suivre-vos-envois?code={$id}", 'Origin' => 'https://www.laposte.fr', - 'Authorization' => "Bearer {$this->getToken($id)}" - ] + 'Authorization' => "Bearer {$this->getToken($id)}", + ], ]); - return json_decode($response->getBody()->getContents(), true); + return json_decode($response->getBody()->getContents(), associative: true); } catch (ClientException $exception) { throw new ColissimoApiException( $exception->getResponse()->getReasonPhrase(), @@ -72,7 +58,7 @@ public function get(string $id): array : $exception->getMessage() ); } - } catch (Exception $exception) { + } catch (Throwable $exception) { throw new ColissimoApiException($exception->getMessage(), 1, $exception); } } @@ -80,8 +66,7 @@ public function get(string $id): array /** * Get the JWT token. * - * @param string $id - * @return string + * @throws \GuzzleHttp\Exception\GuzzleException */ private function getToken(string $id): string { diff --git a/tests/ColissimoApiTest.php b/tests/ColissimoApiTest.php index 4bda00e..3eff6dc 100644 --- a/tests/ColissimoApiTest.php +++ b/tests/ColissimoApiTest.php @@ -8,34 +8,16 @@ class ColissimoApiTest extends TestCase { - /** - * A valid colissimo id. - * - * @var string - */ - private $id = '9L27129283299'; - - /** - * An invalid colissimo id. - * - * @var string - */ - private $invalidId = 'x9V01144112123'; - - /** - * A ColissimoApi instance. - * - * @var \Hedii\ColissimoApi\ColissimoApi - */ - private $colissimo; - - /** - * This method is called before each test. - */ + private string $invalidId = 'x9V01144112123'; + + private ColissimoApi $colissimo; + public function setUp(): void { parent::setUp(); + $this->id = getenv('COLISSIMO_ID'); + $this->colissimo = new ColissimoApi(); }