diff --git a/app/Shipments/Shipment.php b/app/Shipments/Shipment.php index 11d938b..ff7f74e 100644 --- a/app/Shipments/Shipment.php +++ b/app/Shipments/Shipment.php @@ -62,6 +62,9 @@ class Shipment /** @var bool */ protected $trackTraceEnabled = true; + /** @var string */ + protected $myparcelcomShipmentId; + /** * @return string */ @@ -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; + } } diff --git a/app/Shipments/ShipmentMapper.php b/app/Shipments/ShipmentMapper.php index 9922aad..b84ac80 100644 --- a/app/Shipments/ShipmentMapper.php +++ b/app/Shipments/ShipmentMapper.php @@ -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()) @@ -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'])) { @@ -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']); diff --git a/app/Shipments/ShipmentTransformer.php b/app/Shipments/ShipmentTransformer.php index b4e50bf..ce6a772 100644 --- a/app/Shipments/ShipmentTransformer.php +++ b/app/Shipments/ShipmentTransformer.php @@ -33,35 +33,35 @@ 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(), @@ -69,7 +69,7 @@ public function getAttributes($shipment): array 'data' => $file->getData(), ]; }, $shipment->getFiles()), - 'items' => array_map(function (ShipmentItem $item) { + 'items' => array_map(function (ShipmentItem $item) { return [ 'sku' => $item->getSku(), 'description' => $item->getDescription(), @@ -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(), ]); } diff --git a/tests/Stubs/shipment-request.stub b/tests/Stubs/shipment-request.stub index cd7315c..ead1785 100644 --- a/tests/Stubs/shipment-request.stub +++ b/tests/Stubs/shipment-request.stub @@ -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", diff --git a/tests/Unit/Shipments/ShipmentMapperTest.php b/tests/Unit/Shipments/ShipmentMapperTest.php index bbae16a..9cf9a96 100644 --- a/tests/Unit/Shipments/ShipmentMapperTest.php +++ b/tests/Unit/Shipments/ShipmentMapperTest.php @@ -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; }); diff --git a/tests/Unit/Shipments/ShipmentTest.php b/tests/Unit/Shipments/ShipmentTest.php index 3996e3b..9585cc1 100644 --- a/tests/Unit/Shipments/ShipmentTest.php +++ b/tests/Unit/Shipments/ShipmentTest.php @@ -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()); + } } diff --git a/tests/Unit/Shipments/ShipmentTransformerTest.php b/tests/Unit/Shipments/ShipmentTransformerTest.php index 096c881..492c930 100644 --- a/tests/Unit/Shipments/ShipmentTransformerTest.php +++ b/tests/Unit/Shipments/ShipmentTransformerTest.php @@ -109,6 +109,7 @@ public function setUp() 'getFiles' => [], 'getCustoms' => $customs, 'getItems' => [$shipmentItem], + 'getMyparcelcomShipmentId' => 'bbacd0c7-9ec5-42df-9870-443b8e1a7155', ]); $this->minimalShipment = Mockery::mock(Shipment::class, [ @@ -129,6 +130,7 @@ public function setUp() 'getFiles' => [], 'getCustoms' => null, 'getItems' => [], + 'getMyparcelcomShipmentId' => 'bbacd0c7-9ec5-42df-9870-443b8e1a7155', ]); } @@ -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, @@ -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, @@ -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, @@ -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', @@ -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', @@ -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)); } @@ -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', ], @@ -442,6 +446,7 @@ public function testTransformMinimalShipment() 'code' => 'nl300', 'name' => 'noname', ], + 'myparcelcom_shipment_id' => 'bbacd0c7-9ec5-42df-9870-443b8e1a7155', ], ], $this->shipmentTransformer->transform($this->minimalShipment)