From e205e0e1881014453778023f5731051b37685b2c Mon Sep 17 00:00:00 2001 From: hedii Date: Fri, 8 Jun 2018 08:24:57 +0200 Subject: [PATCH] Fix and simplify --- .gitignore | 9 +- .travis.yml | 17 +- README.md | 81 +- composer.json | 34 +- composer.lock | 1426 ----------------------------- phpunit.xml | 10 + src/ColissimoApi.php | 24 + src/ColissimoApi/ColissimoApi.php | 140 --- src/ColissimoApi/Parser.php | 137 --- src/ColissimoApiException.php | 9 + src/Parser.php | 127 +++ tests/ColissimoApiTest.php | 300 ++---- 12 files changed, 336 insertions(+), 1978 deletions(-) delete mode 100644 composer.lock create mode 100644 phpunit.xml create mode 100644 src/ColissimoApi.php delete mode 100644 src/ColissimoApi/ColissimoApi.php delete mode 100644 src/ColissimoApi/Parser.php create mode 100644 src/ColissimoApiException.php create mode 100644 src/Parser.php diff --git a/.gitignore b/.gitignore index fc28715..1a67054 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,4 @@ -# IntelliJ project files +/vendor/ .idea -*.iml -out -gen - .DS_Store -/vendor -/index.php +composer.lock diff --git a/.travis.yml b/.travis.yml index 481f3c8..07c924d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,18 +1,9 @@ language: php - php: - - 5.6 - - 7.0 - - hhvm - + - 7.1 + - 7.2 before_script: - composer self-update - - composer install - + - composer install --no-interaction script: - - vendor/bin/phpunit tests - -matrix: - allow_failures: - - hhvm - fast_finish: true \ No newline at end of file + - ./vendor/bin/phpunit diff --git a/README.md b/README.md index b434634..7c62354 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,12 @@ [![Build Status](https://travis-ci.org/hedii/colissimo-api.svg?branch=master)](https://travis-ci.org/hedii/colissimo-api) # colissimo-api -A php API to track Colissimo (La Poste) parcels +A php package to track Colissimo (La Poste) parcels + +### Requirements +- PHP >= 7.1 +- XML PHP Extension +- Curl PHP Extension ### Installation ````bash @@ -10,31 +15,71 @@ composer require hedii/colissimo-api ### Usage ````php -get('9V01144114240')->all(); -$status = $colissimoApi->get('9V01144114240')->status(); -$id = $colissimoApi->get('9V01144114240')->id(); -$destination = $colissimoApi->get('9V01144114240')->destination(); -var_dump($all, $status, $id, $destination); +$colissimo = new \Hedii\ColissimoApi\ColissimoApi(); -// show json -$colissimoApi->show('9V01144114240')->all(); -$colissimoApi->show('9V01144114240')->status(); -$colissimoApi->show('9V01144114240')->id(); -$colissimoApi->show('9V01144114240')->destination(); +try { + $result = $colissimo->get('your_colissimo_id_here'); +} catch (\Hedii\ColissimoApi\ColissimoApiException $e) { + // ... +} +```` +The result is an array of chronological status: +```` +array(5) { + [0] => + array(3) { + 'date' => + string(10) "30/05/2018" + 'label' => + string(23) "Votre colis est livré." + 'location' => + string(18) "Centre Courrier 75" + } + [1] => + array(3) { + 'date' => + string(10) "30/05/2018" + 'label' => + string(50) "Votre colis est en préparation pour la livraison." + 'location' => + string(18) "Centre Courrier 75" + } + [2] => + array(3) { + 'date' => + string(10) "30/05/2018" + 'label' => + string(52) "Votre colis est arrivé sur son site de distribution" + 'location' => + string(18) "Centre Courrier 75" + } + [3] => + array(3) { + 'date' => + string(10) "29/05/2018" + 'label' => + string(40) "Votre colis est en cours d'acheminement." + 'location' => + string(16) "Plateforme Colis" + } + [4] => + array(3) { + 'date' => + string(10) "28/05/2018" + 'label' => + string(110) "Votre colis a été déposé après l'heure limite de dépôt. Il sera expédié dès le prochain jour ouvré." + 'location' => + string(28) "Bureau de Poste Les estables" + } +} ```` ### Run tests ````bash -vendor/bin/phpunit tests +composer test ```` You may need to update the id in */tests/ColissimoApiTest.php* because the id is only valid for 90 days. diff --git a/composer.json b/composer.json index de7984b..80bbb5c 100644 --- a/composer.json +++ b/composer.json @@ -1,34 +1,48 @@ { "name": "hedii/colissimo-api", "minimum-stability": "stable", - "description": "A php API to track Colissimo (La Poste) parcels", - "keywords": ["colissimo", "api", "colissimoapi", "hedii"], + "description": "A php package to track Colissimo (La Poste) parcels", + "keywords": ["colissimo", "api", "colissimoapi", "hedii", "la poste", "laposte"], "license": "MIT", "homepage": "https://github.com/hedii/colissimo-api", "authors": [ { "name": "Hedi Chaibi", - "email": "hedi.chaibs@gmail.com" + "email": "contact@hedichaibi.com" } ], "support": { "issues": "https://github.com/hedii/colissimo-api/issues", "source": "https://github.com/hedii/colissimo-api", - "email": "hedi.chaibs@gmail.com" + "email": "contact@hedichaibi.com" }, "require": { - "php": ">=5.6", + "php": "^7.1.0", + "ext-curl": "*", "ext-xml": "*", - "symfony/dom-crawler": "^3.0", - "symfony/css-selector": "^3.0" + "guzzlehttp/guzzle": "^6.3", + "symfony/css-selector": "^3.0", + "symfony/dom-crawler": "^3.0" }, "require-dev": { - "phpunit/phpunit": "5.0.*" + "phpunit/phpunit": "^7.0" }, "autoload": { "psr-4": { - "Hedii\\": "src/", - "Hedii\\ColissimoApi\\": "src/ColissimoApi" + "Hedii\\ColissimoApi\\": "src/" } + }, + "autoload-dev": { + "psr-4": { + "Hedii\\ColissimoApi\\Tests\\": "tests/" + } + }, + "scripts": { + "test": "vendor/bin/phpunit" + }, + "config": { + "preferred-install": "dist", + "sort-packages": true, + "optimize-autoloader": true } } diff --git a/composer.lock b/composer.lock deleted file mode 100644 index 85a4d8b..0000000 --- a/composer.lock +++ /dev/null @@ -1,1426 +0,0 @@ -{ - "_readme": [ - "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", - "This file is @generated automatically" - ], - "hash": "05cd07552e60323d8508c4bbe63e69e7", - "content-hash": "ed7b362e92f90ebde6e61d5f880bbcaf", - "packages": [ - { - "name": "symfony/css-selector", - "version": "v3.1.5", - "source": { - "type": "git", - "url": "https://github.com/symfony/css-selector.git", - "reference": "ca809c64072e0fe61c1c7fb3c76cdc32265042ac" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/css-selector/zipball/ca809c64072e0fe61c1c7fb3c76cdc32265042ac", - "reference": "ca809c64072e0fe61c1c7fb3c76cdc32265042ac", - "shasum": "" - }, - "require": { - "php": ">=5.5.9" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.1-dev" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Component\\CssSelector\\": "" - }, - "exclude-from-classmap": [ - "/Tests/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Jean-François Simon", - "email": "jeanfrancois.simon@sensiolabs.com" - }, - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony CssSelector Component", - "homepage": "https://symfony.com", - "time": "2016-09-06 11:02:40" - }, - { - "name": "symfony/dom-crawler", - "version": "v3.1.5", - "source": { - "type": "git", - "url": "https://github.com/symfony/dom-crawler.git", - "reference": "bb7395e8b1db3654de82b9f35d019958276de4d7" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/dom-crawler/zipball/bb7395e8b1db3654de82b9f35d019958276de4d7", - "reference": "bb7395e8b1db3654de82b9f35d019958276de4d7", - "shasum": "" - }, - "require": { - "php": ">=5.5.9", - "symfony/polyfill-mbstring": "~1.0" - }, - "require-dev": { - "symfony/css-selector": "~2.8|~3.0" - }, - "suggest": { - "symfony/css-selector": "" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.1-dev" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Component\\DomCrawler\\": "" - }, - "exclude-from-classmap": [ - "/Tests/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony DomCrawler Component", - "homepage": "https://symfony.com", - "time": "2016-08-05 08:37:39" - }, - { - "name": "symfony/polyfill-mbstring", - "version": "v1.2.0", - "source": { - "type": "git", - "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "dff51f72b0706335131b00a7f49606168c582594" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/dff51f72b0706335131b00a7f49606168c582594", - "reference": "dff51f72b0706335131b00a7f49606168c582594", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "suggest": { - "ext-mbstring": "For best performance" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.2-dev" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Polyfill\\Mbstring\\": "" - }, - "files": [ - "bootstrap.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Nicolas Grekas", - "email": "p@tchwork.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony polyfill for the Mbstring extension", - "homepage": "https://symfony.com", - "keywords": [ - "compatibility", - "mbstring", - "polyfill", - "portable", - "shim" - ], - "time": "2016-05-18 14:26:46" - } - ], - "packages-dev": [ - { - "name": "doctrine/instantiator", - "version": "1.0.5", - "source": { - "type": "git", - "url": "https://github.com/doctrine/instantiator.git", - "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/doctrine/instantiator/zipball/8e884e78f9f0eb1329e445619e04456e64d8051d", - "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d", - "shasum": "" - }, - "require": { - "php": ">=5.3,<8.0-DEV" - }, - "require-dev": { - "athletic/athletic": "~0.1.8", - "ext-pdo": "*", - "ext-phar": "*", - "phpunit/phpunit": "~4.0", - "squizlabs/php_codesniffer": "~2.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "Doctrine\\Instantiator\\": "src/Doctrine/Instantiator/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Marco Pivetta", - "email": "ocramius@gmail.com", - "homepage": "http://ocramius.github.com/" - } - ], - "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", - "homepage": "https://github.com/doctrine/instantiator", - "keywords": [ - "constructor", - "instantiate" - ], - "time": "2015-06-14 21:17:01" - }, - { - "name": "myclabs/deep-copy", - "version": "1.5.4", - "source": { - "type": "git", - "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "ea74994a3dc7f8d2f65a06009348f2d63c81e61f" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/ea74994a3dc7f8d2f65a06009348f2d63c81e61f", - "reference": "ea74994a3dc7f8d2f65a06009348f2d63c81e61f", - "shasum": "" - }, - "require": { - "php": ">=5.4.0" - }, - "require-dev": { - "doctrine/collections": "1.*", - "phpunit/phpunit": "~4.1" - }, - "type": "library", - "autoload": { - "psr-4": { - "DeepCopy\\": "src/DeepCopy/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "description": "Create deep copies (clones) of your objects", - "homepage": "https://github.com/myclabs/DeepCopy", - "keywords": [ - "clone", - "copy", - "duplicate", - "object", - "object graph" - ], - "time": "2016-09-16 13:37:59" - }, - { - "name": "phpdocumentor/reflection-common", - "version": "1.0", - "source": { - "type": "git", - "url": "https://github.com/phpDocumentor/ReflectionCommon.git", - "reference": "144c307535e82c8fdcaacbcfc1d6d8eeb896687c" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/144c307535e82c8fdcaacbcfc1d6d8eeb896687c", - "reference": "144c307535e82c8fdcaacbcfc1d6d8eeb896687c", - "shasum": "" - }, - "require": { - "php": ">=5.5" - }, - "require-dev": { - "phpunit/phpunit": "^4.6" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "phpDocumentor\\Reflection\\": [ - "src" - ] - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Jaap van Otterdijk", - "email": "opensource@ijaap.nl" - } - ], - "description": "Common reflection classes used by phpdocumentor to reflect the code structure", - "homepage": "http://www.phpdoc.org", - "keywords": [ - "FQSEN", - "phpDocumentor", - "phpdoc", - "reflection", - "static analysis" - ], - "time": "2015-12-27 11:43:31" - }, - { - "name": "phpdocumentor/reflection-docblock", - "version": "3.1.1", - "source": { - "type": "git", - "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "8331b5efe816ae05461b7ca1e721c01b46bafb3e" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/8331b5efe816ae05461b7ca1e721c01b46bafb3e", - "reference": "8331b5efe816ae05461b7ca1e721c01b46bafb3e", - "shasum": "" - }, - "require": { - "php": ">=5.5", - "phpdocumentor/reflection-common": "^1.0@dev", - "phpdocumentor/type-resolver": "^0.2.0", - "webmozart/assert": "^1.0" - }, - "require-dev": { - "mockery/mockery": "^0.9.4", - "phpunit/phpunit": "^4.4" - }, - "type": "library", - "autoload": { - "psr-4": { - "phpDocumentor\\Reflection\\": [ - "src/" - ] - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Mike van Riel", - "email": "me@mikevanriel.com" - } - ], - "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", - "time": "2016-09-30 07:12:33" - }, - { - "name": "phpdocumentor/type-resolver", - "version": "0.2", - "source": { - "type": "git", - "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "b39c7a5b194f9ed7bd0dd345c751007a41862443" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/b39c7a5b194f9ed7bd0dd345c751007a41862443", - "reference": "b39c7a5b194f9ed7bd0dd345c751007a41862443", - "shasum": "" - }, - "require": { - "php": ">=5.5", - "phpdocumentor/reflection-common": "^1.0" - }, - "require-dev": { - "mockery/mockery": "^0.9.4", - "phpunit/phpunit": "^5.2||^4.8.24" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "phpDocumentor\\Reflection\\": [ - "src/" - ] - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Mike van Riel", - "email": "me@mikevanriel.com" - } - ], - "time": "2016-06-10 07:14:17" - }, - { - "name": "phpspec/prophecy", - "version": "v1.6.1", - "source": { - "type": "git", - "url": "https://github.com/phpspec/prophecy.git", - "reference": "58a8137754bc24b25740d4281399a4a3596058e0" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy/zipball/58a8137754bc24b25740d4281399a4a3596058e0", - "reference": "58a8137754bc24b25740d4281399a4a3596058e0", - "shasum": "" - }, - "require": { - "doctrine/instantiator": "^1.0.2", - "php": "^5.3|^7.0", - "phpdocumentor/reflection-docblock": "^2.0|^3.0.2", - "sebastian/comparator": "^1.1", - "sebastian/recursion-context": "^1.0" - }, - "require-dev": { - "phpspec/phpspec": "^2.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.6.x-dev" - } - }, - "autoload": { - "psr-0": { - "Prophecy\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Konstantin Kudryashov", - "email": "ever.zet@gmail.com", - "homepage": "http://everzet.com" - }, - { - "name": "Marcello Duarte", - "email": "marcello.duarte@gmail.com" - } - ], - "description": "Highly opinionated mocking framework for PHP 5.3+", - "homepage": "https://github.com/phpspec/prophecy", - "keywords": [ - "Double", - "Dummy", - "fake", - "mock", - "spy", - "stub" - ], - "time": "2016-06-07 08:13:47" - }, - { - "name": "phpunit/php-code-coverage", - "version": "3.3.3", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "44cd8e3930e431658d1a5de7d282d5cb37837fd5" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/44cd8e3930e431658d1a5de7d282d5cb37837fd5", - "reference": "44cd8e3930e431658d1a5de7d282d5cb37837fd5", - "shasum": "" - }, - "require": { - "php": "^5.6 || ^7.0", - "phpunit/php-file-iterator": "~1.3", - "phpunit/php-text-template": "~1.2", - "phpunit/php-token-stream": "^1.4.2", - "sebastian/code-unit-reverse-lookup": "~1.0", - "sebastian/environment": "^1.3.2", - "sebastian/version": "~1.0|~2.0" - }, - "require-dev": { - "ext-xdebug": ">=2.1.4", - "phpunit/phpunit": "~5" - }, - "suggest": { - "ext-dom": "*", - "ext-xdebug": ">=2.4.0", - "ext-xmlwriter": "*" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.3.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", - "homepage": "https://github.com/sebastianbergmann/php-code-coverage", - "keywords": [ - "coverage", - "testing", - "xunit" - ], - "time": "2016-05-27 16:24:29" - }, - { - "name": "phpunit/php-file-iterator", - "version": "1.4.1", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-file-iterator.git", - "reference": "6150bf2c35d3fc379e50c7602b75caceaa39dbf0" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/6150bf2c35d3fc379e50c7602b75caceaa39dbf0", - "reference": "6150bf2c35d3fc379e50c7602b75caceaa39dbf0", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.4.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "FilterIterator implementation that filters files based on a list of suffixes.", - "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", - "keywords": [ - "filesystem", - "iterator" - ], - "time": "2015-06-21 13:08:43" - }, - { - "name": "phpunit/php-text-template", - "version": "1.2.1", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-text-template.git", - "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/31f8b717e51d9a2afca6c9f046f5d69fc27c8686", - "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "type": "library", - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", - "role": "lead" - } - ], - "description": "Simple template engine.", - "homepage": "https://github.com/sebastianbergmann/php-text-template/", - "keywords": [ - "template" - ], - "time": "2015-06-21 13:50:34" - }, - { - "name": "phpunit/php-timer", - "version": "1.0.8", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-timer.git", - "reference": "38e9124049cf1a164f1e4537caf19c99bf1eb260" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/38e9124049cf1a164f1e4537caf19c99bf1eb260", - "reference": "38e9124049cf1a164f1e4537caf19c99bf1eb260", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "require-dev": { - "phpunit/phpunit": "~4|~5" - }, - "type": "library", - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "Utility class for timing", - "homepage": "https://github.com/sebastianbergmann/php-timer/", - "keywords": [ - "timer" - ], - "time": "2016-05-12 18:03:57" - }, - { - "name": "phpunit/php-token-stream", - "version": "1.4.8", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-token-stream.git", - "reference": "3144ae21711fb6cac0b1ab4cbe63b75ce3d4e8da" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/3144ae21711fb6cac0b1ab4cbe63b75ce3d4e8da", - "reference": "3144ae21711fb6cac0b1ab4cbe63b75ce3d4e8da", - "shasum": "" - }, - "require": { - "ext-tokenizer": "*", - "php": ">=5.3.3" - }, - "require-dev": { - "phpunit/phpunit": "~4.2" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.4-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Wrapper around PHP's tokenizer extension.", - "homepage": "https://github.com/sebastianbergmann/php-token-stream/", - "keywords": [ - "tokenizer" - ], - "time": "2015-09-15 10:49:45" - }, - { - "name": "phpunit/phpunit", - "version": "5.0.10", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "9104a4e2f6a3ebdc4eb036624949a1a2849373dd" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/9104a4e2f6a3ebdc4eb036624949a1a2849373dd", - "reference": "9104a4e2f6a3ebdc4eb036624949a1a2849373dd", - "shasum": "" - }, - "require": { - "ext-dom": "*", - "ext-json": "*", - "ext-pcre": "*", - "ext-reflection": "*", - "ext-spl": "*", - "myclabs/deep-copy": "~1.3", - "php": ">=5.6", - "phpspec/prophecy": "^1.3.1", - "phpunit/php-code-coverage": "~3.0", - "phpunit/php-file-iterator": "~1.4", - "phpunit/php-text-template": "~1.2", - "phpunit/php-timer": ">=1.0.6", - "phpunit/phpunit-mock-objects": ">=3.0", - "sebastian/comparator": "~1.1", - "sebastian/diff": "~1.2", - "sebastian/environment": "~1.3", - "sebastian/exporter": "~1.2", - "sebastian/global-state": "~1.0", - "sebastian/resource-operations": "~1.0", - "sebastian/version": "~1.0", - "symfony/yaml": "~2.1|~3.0" - }, - "suggest": { - "phpunit/php-invoker": "~1.1" - }, - "bin": [ - "phpunit" - ], - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "5.0.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", - "role": "lead" - } - ], - "description": "The PHP Unit Testing framework.", - "homepage": "https://phpunit.de/", - "keywords": [ - "phpunit", - "testing", - "xunit" - ], - "time": "2015-11-30 08:33:35" - }, - { - "name": "phpunit/phpunit-mock-objects", - "version": "3.1.3", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", - "reference": "151c96874bff6fe61a25039df60e776613a61489" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/151c96874bff6fe61a25039df60e776613a61489", - "reference": "151c96874bff6fe61a25039df60e776613a61489", - "shasum": "" - }, - "require": { - "doctrine/instantiator": "^1.0.2", - "php": ">=5.6", - "phpunit/php-text-template": "~1.2", - "sebastian/exporter": "~1.2" - }, - "require-dev": { - "phpunit/phpunit": "~5" - }, - "suggest": { - "ext-soap": "*" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.1.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "Mock Object library for PHPUnit", - "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", - "keywords": [ - "mock", - "xunit" - ], - "time": "2016-04-20 14:39:26" - }, - { - "name": "sebastian/code-unit-reverse-lookup", - "version": "1.0.0", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/code-unit-reverse-lookup.git", - "reference": "c36f5e7cfce482fde5bf8d10d41a53591e0198fe" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/code-unit-reverse-lookup/zipball/c36f5e7cfce482fde5bf8d10d41a53591e0198fe", - "reference": "c36f5e7cfce482fde5bf8d10d41a53591e0198fe", - "shasum": "" - }, - "require": { - "php": ">=5.6" - }, - "require-dev": { - "phpunit/phpunit": "~5" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Looks up which function or method a line of code belongs to", - "homepage": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/", - "time": "2016-02-13 06:45:14" - }, - { - "name": "sebastian/comparator", - "version": "1.2.0", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/comparator.git", - "reference": "937efb279bd37a375bcadf584dec0726f84dbf22" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/937efb279bd37a375bcadf584dec0726f84dbf22", - "reference": "937efb279bd37a375bcadf584dec0726f84dbf22", - "shasum": "" - }, - "require": { - "php": ">=5.3.3", - "sebastian/diff": "~1.2", - "sebastian/exporter": "~1.2" - }, - "require-dev": { - "phpunit/phpunit": "~4.4" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.2.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Jeff Welch", - "email": "whatthejeff@gmail.com" - }, - { - "name": "Volker Dusch", - "email": "github@wallbash.com" - }, - { - "name": "Bernhard Schussek", - "email": "bschussek@2bepublished.at" - }, - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Provides the functionality to compare PHP values for equality", - "homepage": "http://www.github.com/sebastianbergmann/comparator", - "keywords": [ - "comparator", - "compare", - "equality" - ], - "time": "2015-07-26 15:48:44" - }, - { - "name": "sebastian/diff", - "version": "1.4.1", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/diff.git", - "reference": "13edfd8706462032c2f52b4b862974dd46b71c9e" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/13edfd8706462032c2f52b4b862974dd46b71c9e", - "reference": "13edfd8706462032c2f52b4b862974dd46b71c9e", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "require-dev": { - "phpunit/phpunit": "~4.8" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.4-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Kore Nordmann", - "email": "mail@kore-nordmann.de" - }, - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Diff implementation", - "homepage": "https://github.com/sebastianbergmann/diff", - "keywords": [ - "diff" - ], - "time": "2015-12-08 07:14:41" - }, - { - "name": "sebastian/environment", - "version": "1.3.8", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/environment.git", - "reference": "be2c607e43ce4c89ecd60e75c6a85c126e754aea" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/be2c607e43ce4c89ecd60e75c6a85c126e754aea", - "reference": "be2c607e43ce4c89ecd60e75c6a85c126e754aea", - "shasum": "" - }, - "require": { - "php": "^5.3.3 || ^7.0" - }, - "require-dev": { - "phpunit/phpunit": "^4.8 || ^5.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.3.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Provides functionality to handle HHVM/PHP environments", - "homepage": "http://www.github.com/sebastianbergmann/environment", - "keywords": [ - "Xdebug", - "environment", - "hhvm" - ], - "time": "2016-08-18 05:49:44" - }, - { - "name": "sebastian/exporter", - "version": "1.2.2", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/exporter.git", - "reference": "42c4c2eec485ee3e159ec9884f95b431287edde4" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/42c4c2eec485ee3e159ec9884f95b431287edde4", - "reference": "42c4c2eec485ee3e159ec9884f95b431287edde4", - "shasum": "" - }, - "require": { - "php": ">=5.3.3", - "sebastian/recursion-context": "~1.0" - }, - "require-dev": { - "ext-mbstring": "*", - "phpunit/phpunit": "~4.4" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.3.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Jeff Welch", - "email": "whatthejeff@gmail.com" - }, - { - "name": "Volker Dusch", - "email": "github@wallbash.com" - }, - { - "name": "Bernhard Schussek", - "email": "bschussek@2bepublished.at" - }, - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - }, - { - "name": "Adam Harvey", - "email": "aharvey@php.net" - } - ], - "description": "Provides the functionality to export PHP variables for visualization", - "homepage": "http://www.github.com/sebastianbergmann/exporter", - "keywords": [ - "export", - "exporter" - ], - "time": "2016-06-17 09:04:28" - }, - { - "name": "sebastian/global-state", - "version": "1.1.1", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/global-state.git", - "reference": "bc37d50fea7d017d3d340f230811c9f1d7280af4" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/bc37d50fea7d017d3d340f230811c9f1d7280af4", - "reference": "bc37d50fea7d017d3d340f230811c9f1d7280af4", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "require-dev": { - "phpunit/phpunit": "~4.2" - }, - "suggest": { - "ext-uopz": "*" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Snapshotting of global state", - "homepage": "http://www.github.com/sebastianbergmann/global-state", - "keywords": [ - "global state" - ], - "time": "2015-10-12 03:26:01" - }, - { - "name": "sebastian/recursion-context", - "version": "1.0.2", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/recursion-context.git", - "reference": "913401df809e99e4f47b27cdd781f4a258d58791" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/913401df809e99e4f47b27cdd781f4a258d58791", - "reference": "913401df809e99e4f47b27cdd781f4a258d58791", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "require-dev": { - "phpunit/phpunit": "~4.4" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Jeff Welch", - "email": "whatthejeff@gmail.com" - }, - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - }, - { - "name": "Adam Harvey", - "email": "aharvey@php.net" - } - ], - "description": "Provides functionality to recursively process PHP variables", - "homepage": "http://www.github.com/sebastianbergmann/recursion-context", - "time": "2015-11-11 19:50:13" - }, - { - "name": "sebastian/resource-operations", - "version": "1.0.0", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/resource-operations.git", - "reference": "ce990bb21759f94aeafd30209e8cfcdfa8bc3f52" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/ce990bb21759f94aeafd30209e8cfcdfa8bc3f52", - "reference": "ce990bb21759f94aeafd30209e8cfcdfa8bc3f52", - "shasum": "" - }, - "require": { - "php": ">=5.6.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Provides a list of PHP built-in functions that operate on resources", - "homepage": "https://www.github.com/sebastianbergmann/resource-operations", - "time": "2015-07-28 20:34:47" - }, - { - "name": "sebastian/version", - "version": "1.0.6", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/version.git", - "reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/58b3a85e7999757d6ad81c787a1fbf5ff6c628c6", - "reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6", - "shasum": "" - }, - "type": "library", - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", - "role": "lead" - } - ], - "description": "Library that helps with managing the version number of Git-hosted PHP projects", - "homepage": "https://github.com/sebastianbergmann/version", - "time": "2015-06-21 13:59:46" - }, - { - "name": "symfony/yaml", - "version": "v3.1.5", - "source": { - "type": "git", - "url": "https://github.com/symfony/yaml.git", - "reference": "368b9738d4033c8b93454cb0dbd45d305135a6d3" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/368b9738d4033c8b93454cb0dbd45d305135a6d3", - "reference": "368b9738d4033c8b93454cb0dbd45d305135a6d3", - "shasum": "" - }, - "require": { - "php": ">=5.5.9" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.1-dev" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Component\\Yaml\\": "" - }, - "exclude-from-classmap": [ - "/Tests/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony Yaml Component", - "homepage": "https://symfony.com", - "time": "2016-09-25 08:27:07" - }, - { - "name": "webmozart/assert", - "version": "1.1.0", - "source": { - "type": "git", - "url": "https://github.com/webmozart/assert.git", - "reference": "bb2d123231c095735130cc8f6d31385a44c7b308" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/webmozart/assert/zipball/bb2d123231c095735130cc8f6d31385a44c7b308", - "reference": "bb2d123231c095735130cc8f6d31385a44c7b308", - "shasum": "" - }, - "require": { - "php": "^5.3.3|^7.0" - }, - "require-dev": { - "phpunit/phpunit": "^4.6", - "sebastian/version": "^1.0.1" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.2-dev" - } - }, - "autoload": { - "psr-4": { - "Webmozart\\Assert\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Bernhard Schussek", - "email": "bschussek@gmail.com" - } - ], - "description": "Assertions to validate method input/output with nice error messages.", - "keywords": [ - "assert", - "check", - "validate" - ], - "time": "2016-08-09 15:02:57" - } - ], - "aliases": [], - "minimum-stability": "stable", - "stability-flags": [], - "prefer-stable": false, - "prefer-lowest": false, - "platform": { - "php": ">=5.6" - }, - "platform-dev": [] -} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..e679df3 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,10 @@ + + + + + ./tests/ + + + diff --git a/src/ColissimoApi.php b/src/ColissimoApi.php new file mode 100644 index 0000000..54a4454 --- /dev/null +++ b/src/ColissimoApi.php @@ -0,0 +1,24 @@ +run(); + + if (empty($data)) { + throw new ColissimoApiException("Cannot find status for colissimo id `{$id}`"); + } + + return $data; + } +} diff --git a/src/ColissimoApi/ColissimoApi.php b/src/ColissimoApi/ColissimoApi.php deleted file mode 100644 index 32785a6..0000000 --- a/src/ColissimoApi/ColissimoApi.php +++ /dev/null @@ -1,140 +0,0 @@ -data = $parser->parseAll(); - - if (empty($this->data)) { - $this->error = 'Invalid Colissimo id provided'; - } - - return $this; - } - - /** - * @param string $id - * - * @return $this - */ - public function show($id) - { - $this->show = true; - $parser = new Parser($id); - $this->data = $parser->parseAll(); - - if (empty($this->data)) { - $this->error = 'Invalid Colissimo id provided'; - } - - return $this; - } - - /** - * @return bool|mixed|string|void - */ - public function all() - { - if ($this->show) { - echo $this->asJson($this->respondWith($this->data)); - - return $this->asJson($this->respondWith($this->data)); - } - - return $this->respondWith($this->data); - } - - /** - * @return bool|mixed|string|void - */ - public function status() - { - if ($this->show) { - echo $this->asJson($this->respondWith($this->data['status'])); - - return $this->asJson($this->respondWith($this->data['status'])); - } - - return $this->respondWith($this->data['status']); - } - - /** - * @return bool|mixed|string|void - */ - public function destination() - { - if ($this->show) { - echo $this->asJson($this->respondWith($this->data['destination'])); - - return $this->asJson($this->respondWith($this->data['destination'])); - } - - return $this->respondWith($this->data['destination']); - } - - /** - * @return bool|mixed|string|void - */ - public function id() - { - if ($this->show) { - echo $this->asJson($this->respondWith($this->data['id'])); - - return $this->asJson($this->respondWith($this->data['id'])); - } - - return $this->respondWith($this->data['id']); - } - - /** - * @param mixed|string|array $value - * - * @return mixed|string|void - */ - public function asJson($value) - { - return json_encode($value, JSON_UNESCAPED_UNICODE); - } - - /** - * @param mixed|string|bool|array $content - * - * @return mixed|string|bool|array - */ - private function respondWith($content) - { - if ($this->error) { - if ($this->show) { - return $this->error; - } - - return false; - } - - return $content; - } -} \ No newline at end of file diff --git a/src/ColissimoApi/Parser.php b/src/ColissimoApi/Parser.php deleted file mode 100644 index 5118a6c..0000000 --- a/src/ColissimoApi/Parser.php +++ /dev/null @@ -1,137 +0,0 @@ -id = $id; - $this->crawler = new Crawler(); - } - - /** - * @return array|bool - */ - public function parseAll() - { - $this->crawler->addHtmlContent($this->getHtml($this->id), 'ISO-8859-1'); - - $nodeValues = $this->crawler->filter('table.dataArray tbody tr td')->each(function (Crawler $node) { - return [ - $node->attr('headers') => trim($node->text()) - ]; - }); - - if ($nodeValues) { - $rows = array_chunk($nodeValues, 3); - - foreach ($rows as $key => $value) { - $this->dates[$key] = $value[0]; - $this->labels[$key] = $value[1]; - $this->sites[$key] = $value[2]; - } - - foreach ($this->dates as $key => $value) { - $this->data['status'][$key] = [ - 'date' => $this->dates[$key]['Date'], - 'label' => $this->labels[$key]['Libelle'], - 'location' => $this->sites[$key]['site'] - ]; - } - - $this->data['id'] = $this->id; - $this->data['destination'] = $this->parseDestination(); - - return $this->data; - } - - return false; - } - - /** - * @param string $id - * - * @return string - */ - private function getHtml($id) - { - return file_get_contents($this->baseUrl . $id); - } - - /** - * @return mixed - */ - private function parseDestination() - { - return preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u', '', - $this->getStringAfter($this->crawler->filter('.suivreForm .head')->text(), 'à destination de :')); - } - - /** - * @param string $haystack - * @param string $delimiter - * - * @return mixed|string|bool - */ - private function getStringAfter($haystack, $delimiter) - { - if ( ! empty($haystack) && ! empty($delimiter)) { - if (strpos($haystack, $delimiter) !== false) { - $filter = explode($delimiter, $haystack); - - if (isset($filter[1])) { - return $filter[1]; - } - - return false; - } - - return false; - } - - return false; - } -} \ No newline at end of file diff --git a/src/ColissimoApiException.php b/src/ColissimoApiException.php new file mode 100644 index 0000000..72c77f4 --- /dev/null +++ b/src/ColissimoApiException.php @@ -0,0 +1,9 @@ +id = $id; + $this->crawler = new Crawler(); + } + + /** + * Run the parser. + * + * @return array + * @throws \Hedii\ColissimoApi\ColissimoApiException + */ + public function run(): array + { + $html = $this->getHtml($this->id); + + if (! $html) { + return []; + } + + $this->crawler->addHtmlContent($html); + + $nodeValues = $this->crawler + ->filter('table tbody tr td') + ->each(function (Crawler $node) { + return trim($node->text()); + }); + + if ($nodeValues) { + foreach (array_chunk($nodeValues, 3) as $row) { + $this->data[] = [ + 'date' => $row[0], + 'label' => $row[1], + 'location' => $row[2] + ]; + } + + return $this->data; + } + + return []; + } + + /** + * Get the html content of the colissimo url response. + * + * @param string $id + * @return null|string + * @throws \Hedii\ColissimoApi\ColissimoApiException + */ + private function getHtml(string $id): ?string + { + try { + $response = $this->client()->get("{$this->baseUrl}/{$id}"); + + return $response->getBody()->getContents() ?: null; + } catch (Exception $exception) { + throw new ColissimoApiException( + "Cannot get the colissimo url `{{$this->baseUrl}/{$id}}`. {$exception->getMessage()}. See stack trace.", + $exception->getCode(), + $exception + ); + } + } + + /** + * Get an http client instance. + * + * @return \GuzzleHttp\Client + */ + private function client(): Client + { + return new Client([ + 'connect_timeout' => 10, + 'timeout' => 30, + 'verify' => false, + 'headers' => [ + 'X-Requested-With' => 'XMLHttpRequest', + 'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36', + 'Referer' => 'https://www.laposte.fr/particulier/outils/suivre-vos-envois' + ] + ]); + } +} diff --git a/tests/ColissimoApiTest.php b/tests/ColissimoApiTest.php index e84df05..0fede44 100644 --- a/tests/ColissimoApiTest.php +++ b/tests/ColissimoApiTest.php @@ -1,234 +1,80 @@ get($this->id)->all(); - - $this->assertTrue(is_array($array)); - } - - public function testGetAllArrayHasCorrectKeys() - { - $colissimoApi = new ColissimoApi(); - $array = $colissimoApi->get($this->id)->all(); - - $this->assertArrayHasKey('status', $array, 'status key is missing'); - $this->assertArrayHasKey('id', $array, 'id key is missing'); - $this->assertArrayHasKey('destination', $array, 'destination key is missing'); - } - - public function testGetAllArrayHasCorrectValues() - { - $colissimoApi = new ColissimoApi(); - $array = $colissimoApi->get($this->id)->all(); - - $this->assertTrue(!empty($array['status']), 'status is empty'); - $this->assertTrue(!empty($array['id']), 'id value is empty'); - $this->assertTrue(!empty($array['destination']), 'id value is empty'); - } - - public function testGetAllArrayHasStatusArrayThatContainsCorrectKeys() - { - $colissimoApi = new ColissimoApi(); - $array = $colissimoApi->get($this->id)->all(); - - $this->assertArrayHasKey('date', $array['status'][0], 'date key is missing'); - $this->assertArrayHasKey('label', $array['status'][0], 'label key is missing'); - $this->assertArrayHasKey('location', $array['status'][0], 'location key is missing'); - } - - public function testGetAllArrayHasStatusArrayThatContainsCorrectValues() - { - $colissimoApi = new ColissimoApi(); - $array = $colissimoApi->get($this->id)->all(); - - $this->assertTrue(!empty($array['status'][0]['date']), 'status date value is empty'); - $this->assertTrue(!empty($array['status'][0]['label']), 'status label value is empty'); - } - - public function testGetStatusReturnsAnArray() - { - $colissimoApi = new ColissimoApi(); - $array = $colissimoApi->get($this->id)->status(); - - $this->assertTrue(is_array($array)); - } - - public function testGetStatusArrayHasArrayThatContainsCorrectKeys() - { - $colissimoApi = new ColissimoApi(); - $array = $colissimoApi->get($this->id)->status(); - - $this->assertArrayHasKey('date', $array[0], 'date key is missing'); - $this->assertArrayHasKey('label', $array[0], 'label key is missing'); - $this->assertArrayHasKey('location', $array[0], 'location key is missing'); - } - - public function testGetStatusArrayHasArrayThatContainsCorrectValues() - { - $colissimoApi = new ColissimoApi(); - $array = $colissimoApi->get($this->id)->status(); - - $this->assertTrue(!empty($array[0]['date']), 'date value is empty'); - $this->assertTrue(!empty($array[0]['label']), 'label value is empty'); - } - - public function testGetIdReturnsAString() - { - $colissimoApi = new ColissimoApi(); - $id = $colissimoApi->get($this->id)->id(); - - $this->assertTrue(!empty($id), 'id is empty'); - $this->assertTrue(is_string($id), 'id is not a string'); - } - - public function testGetDestinationReturnsAString() - { - $colissimoApi = new ColissimoApi(); - $destination = $colissimoApi->get($this->id)->destination(); - - $this->assertTrue(!empty($destination), 'destination is empty'); - $this->assertTrue(is_string($destination), 'destination is not a string'); - } - - public function testShowAllReturnsValidJson() - { - $colissimoApi = new ColissimoApi(); - $json = $colissimoApi->show($this->id)->all(); - - $this->assertJson($json); - } - - public function testShowAllMatchesGetAll() - { - $colissimoApi = new ColissimoApi(); - $get = $colissimoApi->get($this->id)->all(); - $json = $colissimoApi->show($this->id)->all(); - - $this->assertJsonStringEqualsJsonString(json_encode($get), $json); - } - - public function testShowStatusReturnsValidJson() - { - $colissimoApi = new ColissimoApi(); - $json = $colissimoApi->show($this->id)->status(); - - $this->assertJson($json); - } - - public function testShowStatusMatchesGetStatus() - { - $colissimoApi = new ColissimoApi(); - $get = $colissimoApi->get($this->id)->status(); - $json = $colissimoApi->show($this->id)->status(); - - $this->assertJsonStringEqualsJsonString(json_encode($get), $json); - } - - public function testShowIdReturnsValidJson() - { - $colissimoApi = new ColissimoApi(); - $json = $colissimoApi->show($this->id)->id(); - - $this->assertJson($json); - } - - public function testShowIdMatchesGetId() - { - $colissimoApi = new ColissimoApi(); - $get = $colissimoApi->get($this->id)->id(); - $json = $colissimoApi->show($this->id)->id(); - - $this->assertJsonStringEqualsJsonString(json_encode($get), $json); - } - - public function testShowDestinationReturnsValidJson() - { - $colissimoApi = new ColissimoApi(); - $json = $colissimoApi->show($this->id)->destination(); - - $this->assertJson($json); - } - - public function testShowDestinationMatchesGetDestination() - { - $colissimoApi = new ColissimoApi(); - $get = $colissimoApi->get($this->id)->destination(); - $json = $colissimoApi->show($this->id)->destination(); - - $this->assertJsonStringEqualsJsonString(json_encode($get), $json); - } - - public function testGetAllWithInvalidIdReturnsFalse() - { - $colissimoApi = new ColissimoApi(); - $result = $colissimoApi->get($this->wrongId)->destination(); - - $this->assertEquals(false, $result); - } - - public function testGetStatusWithInvalidIdReturnsFalse() - { - $colissimoApi = new ColissimoApi(); - $result = $colissimoApi->get($this->wrongId)->status(); - - $this->assertEquals(false, $result); - } - - public function testGetIdWithInvalidIdReturnsFalse() - { - $colissimoApi = new ColissimoApi(); - $result = $colissimoApi->get($this->wrongId)->id(); - - $this->assertEquals(false, $result); - } - - public function testGetDestinationWithInvalidIdReturnsFalse() - { - $colissimoApi = new ColissimoApi(); - $result = $colissimoApi->get($this->wrongId)->destination(); - - $this->assertEquals(false, $result); - } - - public function testShowAllWithInvalidIdReturnsErrorMessage() - { - $this->expectOutputString(json_encode('Invalid Colissimo id provided')); - - $colissimoApi = new ColissimoApi(); - $colissimoApi->show($this->wrongId)->all(); - } - - public function testShowStatusWithInvalidIdReturnsErrorMessage() - { - $this->expectOutputString(json_encode('Invalid Colissimo id provided')); - - $colissimoApi = new ColissimoApi(); - $colissimoApi->show($this->wrongId)->status(); - } - - public function testShowIdWithInvalidIdReturnsErrorMessage() - { - $this->expectOutputString(json_encode('Invalid Colissimo id provided')); - - $colissimoApi = new ColissimoApi(); - $colissimoApi->show($this->wrongId)->id(); - } - - public function testShowDestinationWithInvalidIdReturnsErrorMessage() - { - $this->expectOutputString(json_encode('Invalid Colissimo id provided')); - - $colissimoApi = new ColissimoApi(); - $colissimoApi->show($this->wrongId)->destination(); + /** + * A valid colissimo id. + * + * @var string + */ + private $id = '8N04104266553'; + + /** + * 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. + */ + public function setUp(): void + { + parent::setUp(); + + $this->colissimo = new ColissimoApi(); + } + + /** @test */ + public function it_should_fail_on_wrong_id(): void + { + $this->expectException(ColissimoApiException::class); + $this->expectExceptionMessage("Cannot find status for colissimo id `{$this->invalidId}`"); + + $this->colissimo->get($this->invalidId); + } + + /** @test */ + public function it_should_have_an_all_method(): void + { + $result = $this->colissimo->get($this->id); + + $this->assertSame([ + [ + 'date' => '30/05/2018', + 'label' => 'Votre colis est livré.', + 'location' => 'Centre Courrier 75' + ], [ + 'date' => '30/05/2018', + 'label' => 'Votre colis est en préparation pour la livraison.', + 'location' => 'Centre Courrier 75' + ], [ + 'date' => '30/05/2018', + 'label' => 'Votre colis est arrivé sur son site de distribution', + 'location' => 'Centre Courrier 75' + ], [ + 'date' => '29/05/2018', + 'label' => 'Votre colis est en cours d\'acheminement.', + 'location' => 'Plateforme Colis' + ], [ + 'date' => '28/05/2018', + 'label' => 'Votre colis a été déposé après l\'heure limite de dépôt. Il sera expédié dès le prochain jour ouvré.', + 'location' => 'Bureau de Poste Les estables' + ] + ], $result); } }