From 4052b950cd2d91cdf856dfb130fe6a8f9d3f6d5b Mon Sep 17 00:00:00 2001 From: Willem Turkstra Date: Wed, 10 May 2023 11:42:03 +0200 Subject: [PATCH 1/4] Added capabilities endpoint --- composer.json | 2 +- src/Client.php | 5 ++ src/Endpoints/Capabilities.php | 76 +++++++++++++++++++ src/Resources/Capability.php | 34 +++++++++ src/Resources/Dimensions.php | 17 +++++ src/Resources/Option.php | 12 +++ src/Resources/ParcelType.php | 33 ++++++++ src/Resources/Product.php | 30 ++++++++ tests/Feature/Endpoints/CapabilitiesTest.php | 79 ++++++++++++++++++++ 9 files changed, 287 insertions(+), 1 deletion(-) create mode 100755 src/Endpoints/Capabilities.php create mode 100644 src/Resources/Capability.php create mode 100644 src/Resources/Dimensions.php create mode 100644 src/Resources/Option.php create mode 100644 src/Resources/ParcelType.php create mode 100644 src/Resources/Product.php create mode 100644 tests/Feature/Endpoints/CapabilitiesTest.php diff --git a/composer.json b/composer.json index 8d88af1..989d781 100755 --- a/composer.json +++ b/composer.json @@ -48,6 +48,6 @@ } }, "scripts": { - "test": "vendor/bin/phpunit" + "test": "vendor/bin/phpunit --testdox" } } diff --git a/src/Client.php b/src/Client.php index 6ae293a..127b534 100755 --- a/src/Client.php +++ b/src/Client.php @@ -8,6 +8,7 @@ use GuzzleHttp\Psr7\Request; use GuzzleHttp\RequestOptions; use Mvdnbrk\DhlParcel\Endpoints\Authentication; +use Mvdnbrk\DhlParcel\Endpoints\Capabilities; use Mvdnbrk\DhlParcel\Endpoints\Labels; use Mvdnbrk\DhlParcel\Endpoints\ServicePoints; use Mvdnbrk\DhlParcel\Endpoints\Shipments; @@ -48,6 +49,9 @@ class Client /** @var \Mvdnbrk\DhlParcel\Endpoints\TrackTrace */ public $tracktrace; + /** @var Capabilities */ + public $capabilities; + public function __construct() { $this->httpClient = new HttpClient([ @@ -64,6 +68,7 @@ public function initializeEndpoints(): void $this->servicePoints = new ServicePoints($this); $this->shipments = new Shipments($this); $this->tracktrace = new TrackTrace($this); + $this->capabilities = new Capabilities($this); } /** diff --git a/src/Endpoints/Capabilities.php b/src/Endpoints/Capabilities.php new file mode 100755 index 0000000..be07128 --- /dev/null +++ b/src/Endpoints/Capabilities.php @@ -0,0 +1,76 @@ +buildQueryString($filters); + + $response = $this->performApiCall( + 'GET', + 'capabilities/'.$senderType.$queryString + ); + + $capabilities = new Collection(); + + collect($response)->each(function ($capability) use ($capabilities) { + $capabilityResource = new CapabilityResource([ + 'rank' => $capability->rank, + 'fromCountryCode' => $capability->fromCountryCode, + 'toCountryCode' => $capability->toCountryCode, + ]); + + $capabilityResource->product = new Product([ + "key" => $capability->product->key, + "label" => $capability->product->label, + "code" => $capability->product->code, + "menuCode" => $capability->product->menuCode, + "businessProduct" => $capability->product->businessProduct, + "monoColloProduct" => $capability->product->monoColloProduct, + "softwareCharacteristic" => $capability->product->softwareCharacteristic, + "returnProduct" => $capability->product->returnProduct, + ]); + + $capabilityResource->parcelType = new ParcelType([ + "key" => $capability->parcelType->key, + "category" => $capability->parcelType->category, + "minWeightKg" => $capability->parcelType->minWeightKg, + "maxWeightKg" => $capability->parcelType->maxWeightKg, + "defaultWeightKg" => $capability->parcelType->defaultWeightKg, + "minWeightGrams" => $capability->parcelType->minWeightGrams, + "maxWeightGrams" => $capability->parcelType->maxWeightGrams, + "defaultWeightGrams" => $capability->parcelType->defaultWeightGrams, + ]); + + $capabilityResource->parcelType->dimensions = new Dimensions([ + "maxLengthCm" => $capability->parcelType->dimensions->maxLengthCm, + "maxWidthCm" => $capability->parcelType->dimensions->maxWidthCm, + "maxHeightCm" => $capability->parcelType->dimensions->maxHeightCm, + ]); + + collect($capability->options)->each(function ($option) use ($capabilityResource) { + $capabilityResource->options->add(new Option([ + "key" => $option->key, + "description" => $option->description, + "rank" => $option->rank, + "code" => $option->code, + "optionType" => $option->optionType, + ])); + }); + + $capabilities->add($capabilityResource); + }); + + return $capabilities; + } +} diff --git a/src/Resources/Capability.php b/src/Resources/Capability.php new file mode 100644 index 0000000..ab2b5a8 --- /dev/null +++ b/src/Resources/Capability.php @@ -0,0 +1,34 @@ +options = new Collection; + + parent::__construct($attributes); + } +} diff --git a/src/Resources/Dimensions.php b/src/Resources/Dimensions.php new file mode 100644 index 0000000..e83c882 --- /dev/null +++ b/src/Resources/Dimensions.php @@ -0,0 +1,17 @@ +client->capabilities->get('business'); + + $this->assertGreaterThan(0, $capabilities->count()); + $this->assertInstanceOf(Capability::class, $capabilities->get(0)); + $this->assertInstanceOf(ParcelType::class, $capabilities->get(0)->parcelType); + $this->assertInstanceOf(Product::class, $capabilities->get(0)->product); + $this->assertInstanceOf(Option::class, $capabilities->get(0)->options->get(0)); + + $this->assertNotNull($capabilities->get(0)->rank); + $this->assertNotNull($capabilities->get(0)->fromCountryCode); + $this->assertNotNull($capabilities->get(0)->toCountryCode); + $this->assertNotNull($capabilities->get(0)->product); + $this->assertNotNull($capabilities->get(0)->parcelType); + $this->assertNotNull($capabilities->get(0)->options); + + $this->assertNotNull($capabilities->get(0)->parcelType->key); + $this->assertNotNull($capabilities->get(0)->parcelType->category); + $this->assertNotNull($capabilities->get(0)->parcelType->minWeightKg); + $this->assertNotNull($capabilities->get(0)->parcelType->maxWeightKg); + $this->assertNotNull($capabilities->get(0)->parcelType->defaultWeightKg); + $this->assertNotNull($capabilities->get(0)->parcelType->dimensions); + $this->assertNotNull($capabilities->get(0)->parcelType->minWeightGrams); + $this->assertNotNull($capabilities->get(0)->parcelType->maxWeightGrams); + $this->assertNotNull($capabilities->get(0)->parcelType->defaultWeightGrams); + + $this->assertNotNull($capabilities->get(0)->product->key); + $this->assertNotNull($capabilities->get(0)->product->label); + $this->assertNotNull($capabilities->get(0)->product->code); + $this->assertNotNull($capabilities->get(0)->product->menuCode); + $this->assertNotNull($capabilities->get(0)->product->businessProduct); + $this->assertNotNull($capabilities->get(0)->product->monoColloProduct); + $this->assertNotNull($capabilities->get(0)->product->softwareCharacteristic); + $this->assertNotNull($capabilities->get(0)->product->returnProduct); + + $this->assertNotNull($capabilities->get(0)->options->get(0)->key); + $this->assertNotNull($capabilities->get(0)->options->get(0)->description); + $this->assertNotNull($capabilities->get(0)->options->get(0)->rank); + $this->assertNotNull($capabilities->get(0)->options->get(0)->code); + $this->assertNotNull($capabilities->get(0)->options->get(0)->optionType); + } + + public function invalid_sender_type() + { + $this->expectException(DhlParcelException::class); + + $this->client->capabilities->get('somethingnonexisting'); + } + + public function test_specific_to_and_from_country() + { + $capabilities = $this->client->capabilities->get('business', [ + 'fromCountry' => 'NL', + 'toCountry' => 'BE', + ]); + + /** @var Capability $capability */ + $capability = $capabilities->get(0); + + $this->assertEquals('NL', $capability->fromCountryCode); + $this->assertEquals('BE', $capability->toCountryCode); + } +} From 4e8281bdcafdca067c92076d374eb8a529bed819 Mon Sep 17 00:00:00 2001 From: Willem Turkstra Date: Wed, 10 May 2023 11:46:47 +0200 Subject: [PATCH 2/4] Added phpdoc --- src/Resources/Option.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Resources/Option.php b/src/Resources/Option.php index 3f6e3ec..de1551f 100644 --- a/src/Resources/Option.php +++ b/src/Resources/Option.php @@ -4,9 +4,18 @@ class Option extends BaseResource { + /** @var string */ public $key; + + /** @var string */ public $description; + + /** @var int */ public $rank; + + /** @var int */ public $code; + + /** @var string */ public $optionType; } From bd939f2a439d12b19357e7240976f76429ec07f4 Mon Sep 17 00:00:00 2001 From: Willem Turkstra Date: Wed, 10 May 2023 11:53:48 +0200 Subject: [PATCH 3/4] Apply codestyle --- src/Endpoints/Capabilities.php | 49 ++++++++++---------- src/Resources/Dimensions.php | 2 - src/Resources/Product.php | 6 +-- tests/Feature/Endpoints/CapabilitiesTest.php | 1 - 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/Endpoints/Capabilities.php b/src/Endpoints/Capabilities.php index be07128..83ad0c3 100755 --- a/src/Endpoints/Capabilities.php +++ b/src/Endpoints/Capabilities.php @@ -7,7 +7,6 @@ use Mvdnbrk\DhlParcel\Resources\Option; use Mvdnbrk\DhlParcel\Resources\ParcelType; use Mvdnbrk\DhlParcel\Resources\Product; -use Mvdnbrk\DhlParcel\Resources\ShipmentPiece; use Tightenco\Collect\Support\Collection; class Capabilities extends BaseEndpoint @@ -31,40 +30,40 @@ public function get(string $senderType, array $filters = []): Collection ]); $capabilityResource->product = new Product([ - "key" => $capability->product->key, - "label" => $capability->product->label, - "code" => $capability->product->code, - "menuCode" => $capability->product->menuCode, - "businessProduct" => $capability->product->businessProduct, - "monoColloProduct" => $capability->product->monoColloProduct, - "softwareCharacteristic" => $capability->product->softwareCharacteristic, - "returnProduct" => $capability->product->returnProduct, + 'key' => $capability->product->key, + 'label' => $capability->product->label, + 'code' => $capability->product->code, + 'menuCode' => $capability->product->menuCode, + 'businessProduct' => $capability->product->businessProduct, + 'monoColloProduct' => $capability->product->monoColloProduct, + 'softwareCharacteristic' => $capability->product->softwareCharacteristic, + 'returnProduct' => $capability->product->returnProduct, ]); $capabilityResource->parcelType = new ParcelType([ - "key" => $capability->parcelType->key, - "category" => $capability->parcelType->category, - "minWeightKg" => $capability->parcelType->minWeightKg, - "maxWeightKg" => $capability->parcelType->maxWeightKg, - "defaultWeightKg" => $capability->parcelType->defaultWeightKg, - "minWeightGrams" => $capability->parcelType->minWeightGrams, - "maxWeightGrams" => $capability->parcelType->maxWeightGrams, - "defaultWeightGrams" => $capability->parcelType->defaultWeightGrams, + 'key' => $capability->parcelType->key, + 'category' => $capability->parcelType->category, + 'minWeightKg' => $capability->parcelType->minWeightKg, + 'maxWeightKg' => $capability->parcelType->maxWeightKg, + 'defaultWeightKg' => $capability->parcelType->defaultWeightKg, + 'minWeightGrams' => $capability->parcelType->minWeightGrams, + 'maxWeightGrams' => $capability->parcelType->maxWeightGrams, + 'defaultWeightGrams' => $capability->parcelType->defaultWeightGrams, ]); $capabilityResource->parcelType->dimensions = new Dimensions([ - "maxLengthCm" => $capability->parcelType->dimensions->maxLengthCm, - "maxWidthCm" => $capability->parcelType->dimensions->maxWidthCm, - "maxHeightCm" => $capability->parcelType->dimensions->maxHeightCm, + 'maxLengthCm' => $capability->parcelType->dimensions->maxLengthCm, + 'maxWidthCm' => $capability->parcelType->dimensions->maxWidthCm, + 'maxHeightCm' => $capability->parcelType->dimensions->maxHeightCm, ]); collect($capability->options)->each(function ($option) use ($capabilityResource) { $capabilityResource->options->add(new Option([ - "key" => $option->key, - "description" => $option->description, - "rank" => $option->rank, - "code" => $option->code, - "optionType" => $option->optionType, + 'key' => $option->key, + 'description' => $option->description, + 'rank' => $option->rank, + 'code' => $option->code, + 'optionType' => $option->optionType, ])); }); diff --git a/src/Resources/Dimensions.php b/src/Resources/Dimensions.php index e83c882..f530ef5 100644 --- a/src/Resources/Dimensions.php +++ b/src/Resources/Dimensions.php @@ -12,6 +12,4 @@ class Dimensions extends BaseResource /** @var int */ public $maxHeightCm; - - } diff --git a/src/Resources/Product.php b/src/Resources/Product.php index 5388997..0f9eaa1 100644 --- a/src/Resources/Product.php +++ b/src/Resources/Product.php @@ -16,15 +16,15 @@ class Product extends BaseResource /** @var string */ public $menuCode; - /** @var boolean */ + /** @var bool */ public $businessProduct; - /** @var boolean */ + /** @var bool */ public $monoColloProduct; /** @var string */ public $softwareCharacteristic; - /** @var boolean */ + /** @var bool */ public $returnProduct; } diff --git a/tests/Feature/Endpoints/CapabilitiesTest.php b/tests/Feature/Endpoints/CapabilitiesTest.php index cf6a414..b311e36 100644 --- a/tests/Feature/Endpoints/CapabilitiesTest.php +++ b/tests/Feature/Endpoints/CapabilitiesTest.php @@ -4,7 +4,6 @@ use Mvdnbrk\DhlParcel\Exceptions\DhlParcelException; use Mvdnbrk\DhlParcel\Resources\Capability; -use Mvdnbrk\DhlParcel\Resources\Dimensions; use Mvdnbrk\DhlParcel\Resources\Option; use Mvdnbrk\DhlParcel\Resources\ParcelType; use Mvdnbrk\DhlParcel\Resources\Product; From 44774224fe16943ec95084e6ceabb5c8b65be423 Mon Sep 17 00:00:00 2001 From: Willem Turkstra Date: Wed, 10 May 2023 11:54:57 +0200 Subject: [PATCH 4/4] Apply codestyle --- src/Resources/Capability.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Resources/Capability.php b/src/Resources/Capability.php index ab2b5a8..42ef22c 100644 --- a/src/Resources/Capability.php +++ b/src/Resources/Capability.php @@ -24,7 +24,6 @@ class Capability extends BaseResource /** @var \Tightenco\Collect\Support\Collection */ public $options; - public function __construct(array $attributes = []) { $this->options = new Collection;