From 3d32f6acaead01c5bf47e432b7122f2cb77157fa Mon Sep 17 00:00:00 2001 From: Florian de Vries Date: Thu, 19 Sep 2024 15:33:46 +0200 Subject: [PATCH] style: change formatting and readability --- src/Hooks/HasPsShippingCostHooks.php | 19 +++---- .../Unit/Hooks/HasPsShippingCostHooksTest.php | 52 ++++++++++--------- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/Hooks/HasPsShippingCostHooks.php b/src/Hooks/HasPsShippingCostHooks.php index 69f3a0df..2af491e9 100644 --- a/src/Hooks/HasPsShippingCostHooks.php +++ b/src/Hooks/HasPsShippingCostHooks.php @@ -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]); diff --git a/tests/Unit/Hooks/HasPsShippingCostHooksTest.php b/tests/Unit/Hooks/HasPsShippingCostHooksTest.php index b3d5cd7b..f850a1be 100644 --- a/tests/Unit/Hooks/HasPsShippingCostHooksTest.php +++ b/tests/Unit/Hooks/HasPsShippingCostHooksTest.php @@ -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); @@ -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' => [ @@ -219,4 +221,4 @@ function () { ], 'cost' => 5.4, ], -]); \ No newline at end of file +]);