Skip to content

Commit

Permalink
Merge pull request #149 from MyParcelCOM/develop
Browse files Browse the repository at this point in the history
🚀
  • Loading branch information
M4tini authored Mar 25, 2021
2 parents 590b4bf + 6aa596d commit fc29cb1
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 36 deletions.
22 changes: 22 additions & 0 deletions src/Resources/Interfaces/ServiceRateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ public function setPrice($price);
*/
public function getPrice();

/**
* @param int $amount
* @return $this
*/
public function setFuelSurchargeAmount($amount);

/**
* @return null|int
*/
public function getFuelSurchargeAmount();

/**
* @param string $currency
* @return $this
*/
public function setFuelSurchargeCurrency($currency);

/**
* @return string
*/
public function getFuelSurchargeCurrency();

/**
* @param ServiceInterface $service
* @return $this
Expand Down
7 changes: 7 additions & 0 deletions src/Resources/ResourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ protected function serviceRateFactory(array &$properties)
unset($properties['attributes']['price']);
}

if (isset($properties['attributes']['fuel_surcharge']['amount'])) {
$serviceRate->setFuelSurchargeAmount($properties['attributes']['fuel_surcharge']['amount']);
$serviceRate->setFuelSurchargeCurrency($properties['attributes']['fuel_surcharge']['currency']);

unset($properties['attributes']['fuel_surcharge']);
}

if (isset($properties['relationships']['service_options'])) {
$serviceOptions = $properties['relationships']['service_options']['data'];

Expand Down
55 changes: 48 additions & 7 deletions src/Resources/ServiceRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ServiceRate implements ServiceRateInterface
use JsonSerializable;

const ATTRIBUTE_PRICE = 'price';
const ATTRIBUTE_FUEL_SURCHARGE = 'fuel_surcharge';
const ATTRIBUTE_CURRENCY = 'currency';
const ATTRIBUTE_AMOUNT = 'amount';
const ATTRIBUTE_WEIGHT_MIN = 'weight_min';
Expand All @@ -34,16 +35,20 @@ class ServiceRate implements ServiceRateInterface
private $type = ResourceInterface::TYPE_SERVICE_RATE;

private $attributes = [
self::ATTRIBUTE_PRICE => [
self::ATTRIBUTE_PRICE => [
self::ATTRIBUTE_CURRENCY => null,
self::ATTRIBUTE_AMOUNT => null,
],
self::ATTRIBUTE_WEIGHT_MIN => null,
self::ATTRIBUTE_WEIGHT_MAX => null,
self::ATTRIBUTE_WIDTH_MAX => null,
self::ATTRIBUTE_LENGTH_MAX => null,
self::ATTRIBUTE_HEIGHT_MAX => null,
self::ATTRIBUTE_VOLUME_MAX => null,
self::ATTRIBUTE_FUEL_SURCHARGE => [
self::ATTRIBUTE_CURRENCY => null,
self::ATTRIBUTE_AMOUNT => null,
],
self::ATTRIBUTE_WEIGHT_MIN => null,
self::ATTRIBUTE_WEIGHT_MAX => null,
self::ATTRIBUTE_WIDTH_MAX => null,
self::ATTRIBUTE_LENGTH_MAX => null,
self::ATTRIBUTE_HEIGHT_MAX => null,
self::ATTRIBUTE_VOLUME_MAX => null,
];

private $relationships = [
Expand Down Expand Up @@ -238,6 +243,42 @@ public function getPrice()
return $this->attributes[self::ATTRIBUTE_PRICE][self::ATTRIBUTE_AMOUNT];
}

/**
* {@inheritdoc}
*/
public function setFuelSurchargeAmount($amount)
{
$this->attributes[self::ATTRIBUTE_FUEL_SURCHARGE][self::ATTRIBUTE_AMOUNT] = $amount;

return $this;
}

/**
* {@inheritdoc}
*/
public function getFuelSurchargeAmount()
{
return $this->attributes[self::ATTRIBUTE_FUEL_SURCHARGE][self::ATTRIBUTE_AMOUNT];
}

/**
* {@inheritdoc}
*/
public function setFuelSurchargeCurrency($currency)
{
$this->attributes[self::ATTRIBUTE_FUEL_SURCHARGE][self::ATTRIBUTE_CURRENCY] = $currency;

return $this;
}

/**
* {@inheritdoc}
*/
public function getFuelSurchargeCurrency()
{
return $this->attributes[self::ATTRIBUTE_FUEL_SURCHARGE][self::ATTRIBUTE_CURRENCY];
}

/**
* {@inheritdoc}
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Shipments/PriceCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ public function calculate(ShipmentInterface $shipment, ServiceRateInterface $ser
}

$serviceRatePrice = $serviceRate->getPrice();
$fuelSurchargePrice = $serviceRate->getFuelSurchargeAmount() ?: 0;
$serviceOptionPrice = $this->calculateOptionsPrice($shipment, $serviceRate);

if ($serviceRatePrice === null || $serviceOptionPrice === null) {
return null;
}

return $serviceOptionPrice + $serviceRatePrice;
return $serviceOptionPrice + $fuelSurchargePrice + $serviceRatePrice;
}

/**
Expand Down
18 changes: 11 additions & 7 deletions tests/Feature/ResourceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,20 @@ public function testItCreatesAServiceRate()
'type' => 'service-rates',
'id' => 'service-rate-id',
'attributes' => [
'weight_min' => 2000,
'weight_max' => 5000,
'width_max' => 100,
'height_max' => 200,
'volume_max' => 6,
'length_max' => 300,
'price' => [
'weight_min' => 2000,
'weight_max' => 5000,
'width_max' => 100,
'height_max' => 200,
'volume_max' => 6,
'length_max' => 300,
'price' => [
'amount' => 800,
'currency' => 'GBP',
],
'fuel_surcharge' => [
'amount' => 19,
'currency' => 'GBP',
],
],
'relationships' => [
'service' => [
Expand Down
13 changes: 9 additions & 4 deletions tests/Traits/MocksContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ trait MocksContract
{
/**
* @param ServiceOptionInterface[] $serviceOptions
* @param int $price
* @param int $weightMin
* @param int $weightMax
* @param int|null $price
* @param int|null $weightMin
* @param int|null $weightMax
* @param int|null $fuelSurcharge
* @return ServiceRateInterface
*/
protected function getMockedServiceRate(
array $serviceOptions = [],
$price = null,
$weightMin = null,
$weightMax = null
$weightMax = null,
$fuelSurcharge = null
) {
$mock = $this->getMockBuilder(ServiceRateInterface::class)
->disableOriginalConstructor()
Expand All @@ -33,6 +35,9 @@ protected function getMockedServiceRate(
$mock
->method('getPrice')
->willReturn($price);
$mock
->method('getFuelSurchargeAmount')
->willReturn($fuelSurcharge);
$mock
->method('getServiceOptions')
->willReturn($serviceOptions);
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/PriceCalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ public function testItCalculatesTheTotalPriceOfAShipment()
$this->assertEquals(6100, $priceCalculator->calculate($shipment));
}

/** @test */
public function testItCalculatesTheTotalPriceIncludingFuelSurcharge()
{
$serviceOptionMocks = [
$this->getMockedServiceOption('service-option-id-uno', 250),
$this->getMockedServiceOption('service-option-id-dos', 850),
];
$serviceRateMock = $this->getMockedServiceRate($serviceOptionMocks, 5000, 0, 5000, 19);
$serviceMock = $this->getMockedService([$serviceRateMock]);
$shipment = $this->getMockedShipment(1337, $serviceMock, $serviceOptionMocks);

$priceCalculator = new PriceCalculator();
$this->assertEquals(6119, $priceCalculator->calculate($shipment));
}

/** @test */
public function testItCalculatesTheOptionsPriceForAShipment()
{
Expand Down
41 changes: 24 additions & 17 deletions tests/Unit/ServiceRateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,17 @@ public function testItSetsAddsAndGetsServiceOptionRelationships()
public function attributeDataProvider()
{
return [
'id' => ['setId', 'service-rate-id', 'getId'],
'type' => ['setType', 'service-rates', 'getType'],
'weight_min' => ['setWeightMin', 123, 'getWeightMin'],
'weight_max' => ['setWeightMax', 456, 'getWeightMax'],
'length_max' => ['setLengthMax', 789, 'getLengthMax'],
'height_max' => ['setHeightMax', 987, 'getHeightMax'],
'width_max' => ['setWidthMax', 654, 'getWidthMax'],
'volume_max' => ['setVolumeMax', 321, 'getVolumeMax'],
'currency' => ['setCurrency', 'GBP', 'getCurrency'],
'price' => ['setPrice', 500, 'getPrice'],
'id' => ['setId', 'service-rate-id', 'getId'],
'type' => ['setType', 'service-rates', 'getType'],
'weight_min' => ['setWeightMin', 123, 'getWeightMin'],
'weight_max' => ['setWeightMax', 456, 'getWeightMax'],
'length_max' => ['setLengthMax', 789, 'getLengthMax'],
'height_max' => ['setHeightMax', 987, 'getHeightMax'],
'width_max' => ['setWidthMax', 654, 'getWidthMax'],
'volume_max' => ['setVolumeMax', 321, 'getVolumeMax'],
'currency' => ['setCurrency', 'GBP', 'getCurrency'],
'price' => ['setPrice', 500, 'getPrice'],
'fuel_surcharge' => ['setFuelSurchargeAmount', 19, 'getFuelSurchargeAmount'],
];
}

Expand Down Expand Up @@ -151,6 +152,8 @@ public function testJsonSerialize()
->setVolumeMax(321)
->setCurrency('GBP')
->setPrice(741)
->setFuelSurchargeAmount(19)
->setFuelSurchargeCurrency('GBP')
->setService($serviceMock)
->setContract($contractMock)
->setServiceOptions([
Expand All @@ -163,16 +166,20 @@ public function testJsonSerialize()
'id' => 'service-rate-id',
'type' => 'service-rates',
'attributes' => [
'price' => [
'price' => [
'amount' => 741,
'currency' => 'GBP',
],
'weight_min' => 123,
'weight_max' => 456,
'length_max' => 789,
'width_max' => 987,
'height_max' => 654,
'volume_max' => 321,
'fuel_surcharge' => [
'amount' => 19,
'currency' => 'GBP',
],
'weight_min' => 123,
'weight_max' => 456,
'length_max' => 789,
'width_max' => 987,
'height_max' => 654,
'volume_max' => 321,
],
'relationships' => [
'service' => [
Expand Down

0 comments on commit fc29cb1

Please sign in to comment.