Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit"
"test": "vendor/bin/phpunit --testdox"
}
}
5 changes: 5 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,6 +49,9 @@ class Client
/** @var \Mvdnbrk\DhlParcel\Endpoints\TrackTrace */
public $tracktrace;

/** @var Capabilities */
public $capabilities;

public function __construct()
{
$this->httpClient = new HttpClient([
Expand All @@ -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);
}

/**
Expand Down
75 changes: 75 additions & 0 deletions src/Endpoints/Capabilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Mvdnbrk\DhlParcel\Endpoints;

use Mvdnbrk\DhlParcel\Resources\Capability as CapabilityResource;
use Mvdnbrk\DhlParcel\Resources\Dimensions;
use Mvdnbrk\DhlParcel\Resources\Option;
use Mvdnbrk\DhlParcel\Resources\ParcelType;
use Mvdnbrk\DhlParcel\Resources\Product;
use Tightenco\Collect\Support\Collection;

class Capabilities extends BaseEndpoint
{
public function get(string $senderType, array $filters = []): Collection
{
$queryString = $this->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;
}
}
33 changes: 33 additions & 0 deletions src/Resources/Capability.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Mvdnbrk\DhlParcel\Resources;

use Tightenco\Collect\Support\Collection;

class Capability extends BaseResource
{
/** @var int */
public $rank;

/** @var string */
public $fromCountryCode;

/** @var string */
public $toCountryCode;

/** @var Product */
public $product;

/** @var ParcelType */
public $parcelType;

/** @var \Tightenco\Collect\Support\Collection */
public $options;

public function __construct(array $attributes = [])
{
$this->options = new Collection;

parent::__construct($attributes);
}
}
15 changes: 15 additions & 0 deletions src/Resources/Dimensions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Mvdnbrk\DhlParcel\Resources;

class Dimensions extends BaseResource
{
/** @var int */
public $maxLengthCm;

/** @var int */
public $maxWidthCm;

/** @var int */
public $maxHeightCm;
}
21 changes: 21 additions & 0 deletions src/Resources/Option.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Mvdnbrk\DhlParcel\Resources;

class Option extends BaseResource
{
/** @var string */
public $key;

/** @var string */
public $description;

/** @var int */
public $rank;

/** @var int */
public $code;

/** @var string */
public $optionType;
}
33 changes: 33 additions & 0 deletions src/Resources/ParcelType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Mvdnbrk\DhlParcel\Resources;

class ParcelType extends BaseResource
{
/** @var string */
public $key;

/** @var string */
public $category;

/** @var int */
public $minWeightKg;

/** @var int */
public $maxWeightKg;

/** @var int */
public $defaultWeightKg;

/** @var Dimensions */
public $dimensions;

/** @var int */
public $minWeightGrams;

/** @var int */
public $maxWeightGrams;

/** @var int */
public $defaultWeightGrams;
}
30 changes: 30 additions & 0 deletions src/Resources/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Mvdnbrk\DhlParcel\Resources;

class Product extends BaseResource
{
/** @var string */
public $key;

/** @var string */
public $label;

/** @var string */
public $code;

/** @var string */
public $menuCode;

/** @var bool */
public $businessProduct;

/** @var bool */
public $monoColloProduct;

/** @var string */
public $softwareCharacteristic;

/** @var bool */
public $returnProduct;
}
78 changes: 78 additions & 0 deletions tests/Feature/Endpoints/CapabilitiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Mvdnbrk\DhlParcel\Tests\Feature\Endpoints;

use Mvdnbrk\DhlParcel\Exceptions\DhlParcelException;
use Mvdnbrk\DhlParcel\Resources\Capability;
use Mvdnbrk\DhlParcel\Resources\Option;
use Mvdnbrk\DhlParcel\Resources\ParcelType;
use Mvdnbrk\DhlParcel\Resources\Product;
use Mvdnbrk\DhlParcel\Tests\TestCase;

class CapabilitiesTest extends TestCase
{
/** @test */
public function get_capabilities()
{
$capabilities = $this->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);
}
}