Skip to content

Commit

Permalink
Refactor for php 8
Browse files Browse the repository at this point in the history
  • Loading branch information
hedii committed Dec 11, 2021
1 parent 88a1e4f commit 3b3a91c
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 59 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.DS_Store
composer.lock
.phpunit.result.cache
/phpunit.xml
10 changes: 5 additions & 5 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
"email": "[email protected]"
},
"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": {
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml → phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
<php>
<!--<env name="COLISSIMO_ID" value="your_colissimo_id_here"/>-->
</php>
</phpunit>
31 changes: 8 additions & 23 deletions src/ColissimoApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
Expand All @@ -72,16 +58,15 @@ public function get(string $id): array
: $exception->getMessage()
);
}
} catch (Exception $exception) {
} catch (Throwable $exception) {
throw new ColissimoApiException($exception->getMessage(), 1, $exception);
}
}

/**
* Get the JWT token.
*
* @param string $id
* @return string
* @throws \GuzzleHttp\Exception\GuzzleException
*/
private function getToken(string $id): string
{
Expand Down
30 changes: 6 additions & 24 deletions tests/ColissimoApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 3b3a91c

Please sign in to comment.