Skip to content

Commit

Permalink
Merge pull request #29 from AlexisPPLIN/fix/kg-to-grams-conversion-pr…
Browse files Browse the repository at this point in the history
…ecision

Added intval and ceil to not loose precision during weight conversion
  • Loading branch information
villermen authored Nov 21, 2023
2 parents 81dbffe + 3ef7240 commit 49893c6
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Model/ParcelItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static function fromData(array $data): self
return new self(
(string)$data['description'],
(int)$data['quantity'],
round(((float)$data['weight']) * 1000),
(int)round(((float)$data['weight']) * 1000),
(float)$data['value'],
isset($data['hs_code']) ? (string)$data['hs_code'] : null,
isset($data['origin_country']) ? (string)$data['origin_country'] : null,
Expand Down
4 changes: 2 additions & 2 deletions src/Model/ShippingMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public static function fromData(array $data): self
return new self(
(int)$data['id'],
(string)$data['name'],
(int)($data['min_weight'] * 1000),
(int)($data['max_weight'] * 1000),
(int)round($data['min_weight'] * 1000.0),
(int)round($data['max_weight'] * 1000.0),
(string)$data['carrier'],
$prices,
$data['service_point_input'] !== 'none',
Expand Down
4 changes: 2 additions & 2 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testGetShippingMethods(): void
return new Response(
200,
[],
'{"shipping_methods": [{"service_point_input": "none","max_weight": "1.000","name": "Low weight shipment","carrier": "carrier_code","countries": [{"iso_2": "BE","iso_3": "BEL","id": 1,"price": 3.50,"name": "Belgium"},{"iso_2": "NL","iso_3": "NLD","id": 2,"price": 4.20,"name": "Netherlands"}],"min_weight": "0.001","id": 1,"price": 0}]}'
'{"shipping_methods": [{"service_point_input": "none","min_weight": "0.001","max_weight": "1.001","name": "Low weight shipment","carrier": "carrier_code","countries": [{"iso_2": "BE","iso_3": "BEL","id": 1,"price": 3.50,"name": "Belgium"},{"iso_2": "NL","iso_3": "NLD","id": 2,"price": 4.20,"name": "Netherlands"}],"min_weight": "0.001","id": 1,"price": 0}]}'
);
});

Expand All @@ -78,7 +78,7 @@ public function testGetShippingMethods(): void
$this->assertCount(1, $shippingMethods);
$this->assertEquals(1, $shippingMethods[0]->getId());
$this->assertEquals(1, $shippingMethods[0]->getMinimumWeight());
$this->assertEquals(1000, $shippingMethods[0]->getMaximumWeight());
$this->assertEquals(1001, $shippingMethods[0]->getMaximumWeight());
$this->assertEquals('carrier_code', $shippingMethods[0]->getCarrier());
$this->assertEquals(['BE' => 350, 'NL' => 420], $shippingMethods[0]->getPrices());
$this->assertEquals(420, $shippingMethods[0]->getPriceForCountry('NL'));
Expand Down

0 comments on commit 49893c6

Please sign in to comment.