Skip to content

Commit

Permalink
✨ Added myparcelcom_shipment_id to shipment resource.
Browse files Browse the repository at this point in the history
  • Loading branch information
NickVries committed May 10, 2019
1 parent 0e8675d commit b578549
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 33 deletions.
22 changes: 22 additions & 0 deletions app/Shipments/Shipment.php
Expand Up @@ -62,6 +62,9 @@ class Shipment
/** @var bool */
protected $trackTraceEnabled = true;

/** @var string */
protected $myparcelcomShipmentId;

/**
* @return string
*/
Expand Down Expand Up @@ -410,4 +413,23 @@ public function isTrackTraceEnabled(): bool
{
return $this->trackTraceEnabled;
}

/**
* @return string
*/
public function getMyparcelcomShipmentId(): string
{
return $this->myparcelcomShipmentId;
}

/**
* @param string $myparcelcomShipmentId
* @return $this
*/
public function setMyparcelcomShipmentId(string $myparcelcomShipmentId): self
{
$this->myparcelcomShipmentId = $myparcelcomShipmentId;

return $this;
}
}
17 changes: 11 additions & 6 deletions app/Shipments/ShipmentMapper.php
Expand Up @@ -27,6 +27,11 @@ public function map($data, $shipment): Shipment
{
$attributes = $data['attributes'];

// Map myparcelcom shipment id
if (isset($attributes['myparcelcom_shipment_id'])) {
$shipment->setMyparcelcomShipmentId($attributes['myparcelcom_shipment_id']);
}

// Map addresses.
$shipment->setRecipientAddress(
$this->mapAddress($attributes['recipient_address'], new Address())
Expand Down Expand Up @@ -54,19 +59,19 @@ public function map($data, $shipment): Shipment
$shipment->setPhysicalProperties(new PhysicalProperties());
}
if (isset($attributes['physical_properties']['weight'])) {
$shipment->getPhysicalProperties()->setWeight((int)$attributes['physical_properties']['weight']);
$shipment->getPhysicalProperties()->setWeight((int) $attributes['physical_properties']['weight']);
}
if (isset($attributes['physical_properties']['width'])) {
$shipment->getPhysicalProperties()->setWidth((int)$attributes['physical_properties']['width']);
$shipment->getPhysicalProperties()->setWidth((int) $attributes['physical_properties']['width']);
}
if (isset($attributes['physical_properties']['height'])) {
$shipment->getPhysicalProperties()->setHeight((int)$attributes['physical_properties']['height']);
$shipment->getPhysicalProperties()->setHeight((int) $attributes['physical_properties']['height']);
}
if (isset($attributes['physical_properties']['length'])) {
$shipment->getPhysicalProperties()->setLength((int)$attributes['physical_properties']['length']);
$shipment->getPhysicalProperties()->setLength((int) $attributes['physical_properties']['length']);
}
if (isset($attributes['physical_properties']['volume'])) {
$shipment->getPhysicalProperties()->setVolume((float)$attributes['physical_properties']['volume']);
$shipment->getPhysicalProperties()->setVolume((float) $attributes['physical_properties']['volume']);
}

if (isset($attributes['options'])) {
Expand Down Expand Up @@ -141,7 +146,7 @@ protected function mapOptions(array $options, Shipment $shipment): self
protected function mapAddress(array $data, Address $address): Address
{
if (isset($data['street_1'])) {
$address->setStreet1((string)$data['street_1']);
$address->setStreet1((string) $data['street_1']);
}
if (isset($data['street_2'])) {
$address->setStreet2($data['street_2']);
Expand Down
29 changes: 15 additions & 14 deletions app/Shipments/ShipmentTransformer.php
Expand Up @@ -33,43 +33,43 @@ public function getAttributes($shipment): array
$this->validateModel($shipment);

return array_filter([
'recipient_address' => $this->transformAddress($shipment->getRecipientAddress()),
'sender_address' => $this->transformAddress($shipment->getSenderAddress()),
'return_address' => $this->transformAddress($shipment->getReturnAddress()),
'pickup_location' => $shipment->getPickupLocationCode() === null ? null : [
'recipient_address' => $this->transformAddress($shipment->getRecipientAddress()),
'sender_address' => $this->transformAddress($shipment->getSenderAddress()),
'return_address' => $this->transformAddress($shipment->getReturnAddress()),
'pickup_location' => $shipment->getPickupLocationCode() === null ? null : [
'code' => $shipment->getPickupLocationCode(),
'address' => $this->transformAddress($shipment->getPickupLocationAddress()),
],
'description' => $shipment->getDescription(),
'barcode' => $shipment->getBarcode(),
'tracking_code' => $shipment->getTrackingCode(),
'tracking_url' => $shipment->getTrackingUrl(),
'service' => [
'description' => $shipment->getDescription(),
'barcode' => $shipment->getBarcode(),
'tracking_code' => $shipment->getTrackingCode(),
'tracking_url' => $shipment->getTrackingUrl(),
'service' => [
'code' => $shipment->getService()->getCode(),
'name' => $shipment->getService()->getName(),
],
'options' => array_map(function (Option $option) {
'options' => array_map(function (Option $option) {
return [
'code' => $option->getCode(),
'name' => $option->getName(),
];
}, $shipment->getOptions()),
'physical_properties' => $shipment->getPhysicalProperties() === null ? null : [
'physical_properties' => $shipment->getPhysicalProperties() === null ? null : [
'height' => $shipment->getPhysicalProperties()->getHeight(),
'width' => $shipment->getPhysicalProperties()->getWidth(),
'length' => $shipment->getPhysicalProperties()->getLength(),
'volume' => $shipment->getPhysicalProperties()->getVolume(),
'weight' => $shipment->getPhysicalProperties()->getWeight(),
],
'files' => array_map(function (File $file) {
'files' => array_map(function (File $file) {
return [
'resource_type' => $file->getType(),
'mime_type' => $file->getMimeType(),
'extension' => $file->getExtension(),
'data' => $file->getData(),
];
}, $shipment->getFiles()),
'items' => array_map(function (ShipmentItem $item) {
'items' => array_map(function (ShipmentItem $item) {
return [
'sku' => $item->getSku(),
'description' => $item->getDescription(),
Expand All @@ -82,12 +82,13 @@ public function getAttributes($shipment): array
],
];
}, $shipment->getItems()),
'customs' => $shipment->getCustoms() === null ? null : [
'customs' => $shipment->getCustoms() === null ? null : [
'content_type' => $shipment->getCustoms()->getContentType(),
'invoice_number' => $shipment->getCustoms()->getInvoiceNumber(),
'non_delivery' => $shipment->getCustoms()->getNonDelivery(),
'incoterm' => $shipment->getCustoms()->getIncoterm(),
],
'myparcelcom_shipment_id' => $shipment->getMyparcelcomShipmentId(),
]);
}

Expand Down
1 change: 1 addition & 0 deletions tests/Stubs/shipment-request.stub
Expand Up @@ -2,6 +2,7 @@
"data": {
"type": "shipments",
"attributes": {
"myparcelcom_shipment_id": "bbacd0c7-9ec5-42df-9870-443b8e1a7155",
"recipient_address": {
"street_1": "Some road",
"street_2": "Room 3",
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/Shipments/ShipmentMapperTest.php
Expand Up @@ -171,6 +171,12 @@ public function testMap()
$this->assertNotNull($item->getOriginCountryCode());
});

return $shipment;
})
->shouldReceive('setMyparcelcomShipmentId')
->andReturnUsing(function (string $id) use ($shipment) {
$this->assertEquals('bbacd0c7-9ec5-42df-9870-443b8e1a7155', $id);

return $shipment;
});

Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/Shipments/ShipmentTest.php
Expand Up @@ -164,4 +164,14 @@ public function testTrackTraceEnabled()
$this->assertTrue($shipment->isTrackTraceEnabled());
$this->assertFalse($shipment->setTrackTraceEnabled(false)->isTrackTraceEnabled());
}

/** @test */
public function testItSetsMyparcelcomShipmentId()
{
$shipment = new Shipment();

$myparcelcomShipmentId = 'bbacd0c7-9ec5-42df-9870-443b8e1a7155';

$this->assertEquals($myparcelcomShipmentId, $shipment->setMyparcelcomShipmentId($myparcelcomShipmentId)->getMyparcelcomShipmentId());
}
}
31 changes: 18 additions & 13 deletions tests/Unit/Shipments/ShipmentTransformerTest.php
Expand Up @@ -109,6 +109,7 @@ public function setUp()
'getFiles' => [],
'getCustoms' => $customs,
'getItems' => [$shipmentItem],
'getMyparcelcomShipmentId' => 'bbacd0c7-9ec5-42df-9870-443b8e1a7155',
]);

$this->minimalShipment = Mockery::mock(Shipment::class, [
Expand All @@ -129,6 +130,7 @@ public function setUp()
'getFiles' => [],
'getCustoms' => null,
'getItems' => [],
'getMyparcelcomShipmentId' => 'bbacd0c7-9ec5-42df-9870-443b8e1a7155',
]);
}

Expand Down Expand Up @@ -162,7 +164,7 @@ public function testGetType()
public function testGetAttributes()
{
$this->assertEquals([
'recipient_address' => [
'recipient_address' => [
'street_1' => 'First Street',
'street_2' => 'Second Street',
'street_number' => 69,
Expand All @@ -177,7 +179,7 @@ public function testGetAttributes()
'email' => 'john@expertsexchange.com',
'phone_number' => '1337-9001',
],
'sender_address' => [
'sender_address' => [
'street_1' => 'First Street',
'street_2' => 'Second Street',
'street_number' => 69,
Expand All @@ -192,7 +194,7 @@ public function testGetAttributes()
'email' => 'john@expertsexchange.com',
'phone_number' => '1337-9001',
],
'return_address' => [
'return_address' => [
'street_1' => 'First Street',
'street_2' => 'Second Street',
'street_number' => 69,
Expand All @@ -207,7 +209,7 @@ public function testGetAttributes()
'email' => 'john@expertsexchange.com',
'phone_number' => '1337-9001',
],
'pickup_location' => [
'pickup_location' => [
'code' => 'aaaa',
'address' => [
'street_1' => 'First Street',
Expand All @@ -225,28 +227,28 @@ public function testGetAttributes()
'phone_number' => '1337-9001',
],
],
'description' => 'descending ription',
'barcode' => '3SBARCODE',
'tracking_code' => 'TR4CK1NGC0D3',
'tracking_url' => 'https://track.me/TR4CK1NGC0D3',
'service' => [
'description' => 'descending ription',
'barcode' => '3SBARCODE',
'tracking_code' => 'TR4CK1NGC0D3',
'tracking_url' => 'https://track.me/TR4CK1NGC0D3',
'service' => [
'code' => 'nl300',
'name' => 'noname',
],
'physical_properties' => [
'physical_properties' => [
'height' => 1,
'width' => 2,
'length' => 3,
'volume' => 4,
'weight' => 5,
],
'options' => [
'options' => [
[
'name' => 'plx name me',
'code' => 'somecode',
],
],
'items' => [
'items' => [
[
'sku' => '01284ASD',
'description' => 'priceless Ming vase from some dynasty',
Expand All @@ -259,12 +261,13 @@ public function testGetAttributes()
],
],
],
'customs' => [
'customs' => [
'content_type' => Customs::CONTENT_TYPE_DOCUMENTS,
'invoice_number' => 'V01C3',
'incoterm' => Customs::INCOTERM_DELIVERED_AT_PLACE,
'non_delivery' => Customs::NON_DELIVERY_ABANDON,
],
'myparcelcom_shipment_id' => 'bbacd0c7-9ec5-42df-9870-443b8e1a7155',
], $this->shipmentTransformer->getAttributes($this->shipment));
}

Expand Down Expand Up @@ -377,6 +380,7 @@ public function testTransform()
'incoterm' => Customs::INCOTERM_DELIVERED_AT_PLACE,
'non_delivery' => Customs::NON_DELIVERY_ABANDON,
],
'myparcelcom_shipment_id' => 'bbacd0c7-9ec5-42df-9870-443b8e1a7155',
'tracking_code' => 'TR4CK1NGC0D3',
'tracking_url' => 'https://track.me/TR4CK1NGC0D3',
],
Expand Down Expand Up @@ -442,6 +446,7 @@ public function testTransformMinimalShipment()
'code' => 'nl300',
'name' => 'noname',
],
'myparcelcom_shipment_id' => 'bbacd0c7-9ec5-42df-9870-443b8e1a7155',
],
],
$this->shipmentTransformer->transform($this->minimalShipment)
Expand Down

0 comments on commit b578549

Please sign in to comment.