Skip to content

Commit

Permalink
style: change formatting and readability
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianSDV committed Sep 19, 2024
1 parent 449ffea commit 3d32f6a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
19 changes: 10 additions & 9 deletions src/Hooks/HasPsShippingCostHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,17 @@ private function getDeliveryOptions(Carrier $carrier): DeliveryOptions
return new DeliveryOptions(json_decode($deliveryOptions, true));
}

//Delivery options are not in hidden input, fetch them from the database.
/** @var PsCartDeliveryOptionsRepository $cartDeliveryOptionsRepository */
/** @var \MyParcelNL\PrestaShop\Repository\PsCartDeliveryOptionsRepository $cartDeliveryOptionsRepository */
$cartDeliveryOptionsRepository = Pdk::get(PsCartDeliveryOptionsRepository::class);
$deliveryOptions = $cartDeliveryOptionsRepository->findOneBy(['cartId' => $this->context->cart->id]);

$useDbDeliveryOptions = $deliveryOptions && property_exists($deliveryOptions, 'data');
$useDbDeliveryOptions = $useDbDeliveryOptions && $deliveryOptions->getData()['carrier']['externalIdentifier'] === $carrier->externalIdentifier;

if ($useDbDeliveryOptions) {
return new DeliveryOptions($deliveryOptions->getData());
$dbDeliveryOptions = $cartDeliveryOptionsRepository->findOneBy(
['cartId' => $this->context->cart->id]
);

if (
$dbDeliveryOptions
&& $dbDeliveryOptions->getData()['carrier']['externalIdentifier'] === $carrier->externalIdentifier
) {
return new DeliveryOptions($dbDeliveryOptions->getData());
}

return new DeliveryOptions(['carrier' => $carrier]);
Expand Down
52 changes: 27 additions & 25 deletions tests/Unit/Hooks/HasPsShippingCostHooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,33 +142,35 @@ function () {
expect($result)->toBe(123.45);
});

it(
'calculates shipping costs using database',
function (CartFactory $cartFactory, array $deliveryOptions = [], float $addedCost = 0) {
$cart = $cartFactory->make();

it('calculates shipping costs using database', function (CartFactory $cartFactory, array $deliveryOptions = [], float $addedCost = 0) {
$cart = $cartFactory->make();
/** @var PsCartDeliveryOptionsRepository $cartDeliveryOptionsRepository */
$cartDeliveryOptionsRepository = Pdk::get(PsCartDeliveryOptionsRepository::class);

/** @var PsCartDeliveryOptionsRepository $cartDeliveryOptionsRepository */
$cartDeliveryOptionsRepository = Pdk::get(PsCartDeliveryOptionsRepository::class);
$deliveryOptions = new DeliveryOptions($deliveryOptions);

$deliveryOptions = new DeliveryOptions($deliveryOptions);
$cartDeliveryOptionsRepository->updateOrCreate(
[
'cartId' => $cart->id,
],
[
'data' => json_encode($deliveryOptions->toStorableArray()),
]
);

$cartDeliveryOptionsRepository->updateOrCreate(
[
'cartId' => $cart->id,
],
[
'data' => json_encode($deliveryOptions->toStorableArray()),
]
);
$instance = new ClassWithTrait();
$instance->setIdCarrier(93);
$instance->setCart($cart);

$instance = new ClassWithTrait();
$instance->setIdCarrier(93);
$instance->setCart($cart);
$baseCost = 10;
$cost = $instance->getOrderShippingCost($cart, $baseCost);

$baseCost = 10;
$cost = $instance->getOrderShippingCost($cart, $baseCost);

expect(number_format($cost, 2))->toEqual(number_format($baseCost + $addedCost, 2));
})->with([
expect(number_format($cost, 2))->toEqual(number_format($baseCost + $addedCost, 2));
}
)->with([
'standard delivery with delivery options in values' => [
function () {
$psCarrier = psFactory(PsCarrier::class)->withId(93);
Expand All @@ -186,10 +188,10 @@ function () {
return psFactory(Cart::class)->withCarrier($psCarrier);
},
'values' => [
DeliveryOptions::CARRIER => Carrier::CARRIER_POSTNL_NAME,
DeliveryOptions::DELIVERY_TYPE => DeliveryOptions::DELIVERY_TYPE_STANDARD_NAME,
DeliveryOptions::CARRIER => Carrier::CARRIER_POSTNL_NAME,
DeliveryOptions::DELIVERY_TYPE => DeliveryOptions::DELIVERY_TYPE_STANDARD_NAME,
],
'cost' => 2.95,
'cost' => 2.95,
],

'carrier with linked myparcel carrier and delivery options in values' => [
Expand Down Expand Up @@ -219,4 +221,4 @@ function () {
],
'cost' => 5.4,
],
]);
]);

0 comments on commit 3d32f6a

Please sign in to comment.