From cf7bc65f1ab8745e6d0bef9ce014593a4ddc988d Mon Sep 17 00:00:00 2001 From: mandan2 Date: Tue, 26 Sep 2023 13:45:46 +0300 Subject: [PATCH] renamed some services and added additional conditions for carrier retrieve --- .../CouldNotProvideCarrierDeliveryPrice.php | 62 ------- .../Provider/CarrierDeliveryPriceProvider.php | 122 -------------- .../CarrierDeliveryPriceProviderTest.php | 159 ------------------ 3 files changed, 343 deletions(-) delete mode 100644 subscription/Exception/CouldNotProvideCarrierDeliveryPrice.php delete mode 100644 subscription/Provider/CarrierDeliveryPriceProvider.php delete mode 100644 tests/Integration/Subscription/Provider/CarrierDeliveryPriceProviderTest.php diff --git a/subscription/Exception/CouldNotProvideCarrierDeliveryPrice.php b/subscription/Exception/CouldNotProvideCarrierDeliveryPrice.php deleted file mode 100644 index c6f20a9e4..000000000 --- a/subscription/Exception/CouldNotProvideCarrierDeliveryPrice.php +++ /dev/null @@ -1,62 +0,0 @@ -configuration = $configuration; - $this->carrierRepository = $carrierRepository; - $this->addressRepository = $addressRepository; - $this->customerRepository = $customerRepository; - $this->cartRepository = $cartRepository; - $this->countryRepository = $countryRepository; - } - - /** - * @throws CouldNotProvideCarrierDeliveryPrice - */ - public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, array $subscriptionProduct): float - { - $subscriptionCarrierId = (int) $this->configuration->get(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID); - - /** @var \Carrier|null $carrier */ - $carrier = $this->carrierRepository->findOneBy([ - 'id_carrier' => $subscriptionCarrierId, - ]); - - if (!$carrier) { - throw CouldNotProvideCarrierDeliveryPrice::failedToFindSelectedCarrierForSubscriptionOrder(); - } - - /** @var \Cart|null $cart */ - $cart = $this->cartRepository->findOneBy([ - 'id_cart' => $cartId, - ]); - - if (!$cart) { - throw CouldNotProvideCarrierDeliveryPrice::failedToFindOrderCart(); - } - - /** @var \Customer|null $customer */ - $customer = $this->customerRepository->findOneBy([ - 'id_customer' => $customerId, - ]); - - if (!$customer) { - throw CouldNotProvideCarrierDeliveryPrice::failedToFindOrderCustomer(); - } - - $getAvailableOrderCarriers = $this->carrierRepository->getCarriersForOrder( - $this->addressRepository->getZoneById($addressDeliveryId), - $customer->getGroups(), - $cart - ); - - if (!in_array($subscriptionCarrierId, array_column($getAvailableOrderCarriers, 'id_carrier'), false)) { - throw CouldNotProvideCarrierDeliveryPrice::failedToApplySelectedCarrierForSubscriptionOrder(); - } - - /** @var \Address|bool $address */ - $address = $this->addressRepository->findOneBy([ - 'id_address' => $addressDeliveryId, - ]); - - if (!$address) { - throw CouldNotProvideCarrierDeliveryPrice::failedToFindOrderDeliveryAddress(); - } - - /** @var \Country|bool $country */ - $country = $this->countryRepository->findOneBy([ - 'id_country' => $address->id_country, - ]); - - if (!$country) { - throw CouldNotProvideCarrierDeliveryPrice::failedToFindOrderDeliveryCountry(); - } - - /** @var float|bool $deliveryPrice */ - $deliveryPrice = $cart->getPackageShippingCost( - $subscriptionCarrierId, - true, - $country, - [$subscriptionProduct], - $this->addressRepository->getZoneById($addressDeliveryId) - ); - - if (is_bool($deliveryPrice) && !$deliveryPrice) { - throw CouldNotProvideCarrierDeliveryPrice::failedToGetSelectedCarrierPriceForSubscriptionOrder(); - } - - return (float) $deliveryPrice; - } -} diff --git a/tests/Integration/Subscription/Provider/CarrierDeliveryPriceProviderTest.php b/tests/Integration/Subscription/Provider/CarrierDeliveryPriceProviderTest.php deleted file mode 100644 index bf8e5416c..000000000 --- a/tests/Integration/Subscription/Provider/CarrierDeliveryPriceProviderTest.php +++ /dev/null @@ -1,159 +0,0 @@ -getService(ConfigurationAdapter::class); - - $this->subscriptionOrderCarrierId = $configuration->get(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID); - - parent::setUp(); - } - - public function tearDown(): void - { - /** @var ConfigurationAdapter $configuration */ - $configuration = $this->getService(ConfigurationAdapter::class); - - $configuration->updateValue(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID, $this->subscriptionOrderCarrierId); - - parent::tearDown(); - } - - public function testItSuccessfullyProvidesCarrierDeliveryPrice(): void - { - $address = AddressFactory::create(); - $carrier = CarrierFactory::create([ - 'price' => 999.00, - ]); - $cart = CartFactory::create([ - 'id_carrier' => $carrier->id, - ]); - - /** @var ConfigurationAdapter $configuration */ - $configuration = $this->getService(ConfigurationAdapter::class); - - $configuration->updateValue(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID, $carrier->id); - - $targetProduct = ProductFactory::create([ - 'quantity' => 10, - ]); - $product1 = ProductFactory::create([ - 'quantity' => 10, - ]); - $product2 = ProductFactory::create([ - 'quantity' => 10, - ]); - - $cart->updateQty(2, $targetProduct->id); - - $targetProductArray = $cart->getProducts()[0]; - - $cart->updateQty(2, $product1->id); - $cart->updateQty(3, $product2->id); - - /** @var CarrierDeliveryPriceProvider $carrierDeliveryPriceProvider */ - $carrierDeliveryPriceProvider = $this->getService(CarrierDeliveryPriceProvider::class); - - $result = $carrierDeliveryPriceProvider->getPrice( - $address->id, - $cart->id, - $cart->id_customer, - $targetProductArray - ); - - $this->assertEquals(999.00, $result); - - $this->removeFactories([ - $carrier, - $address, - $cart, - $targetProduct, - $product1, - $product2, - ]); - } - - public function testItUnsuccessfullyProvidesCarrierDeliveryPriceCarrierIsOutOfZone(): void - { - $address = AddressFactory::create(); - $carrier = CarrierFactory::create([ - 'price' => 999.00, - 'id_zones_to_delete' => [ - $address::getZoneById($address->id), - ], - ]); - $cart = CartFactory::create([ - 'id_carrier' => $carrier->id, - ]); - - /** @var ConfigurationAdapter $configuration */ - $configuration = $this->getService(ConfigurationAdapter::class); - - $configuration->updateValue(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID, $carrier->id); - - $targetProduct = ProductFactory::create([ - 'quantity' => 10, - ]); - $product1 = ProductFactory::create([ - 'quantity' => 10, - ]); - $product2 = ProductFactory::create([ - 'quantity' => 10, - ]); - - $cart->updateQty(2, $targetProduct->id); - - $targetProductArray = $cart->getProducts()[0]; - - $cart->updateQty(2, $product1->id); - $cart->updateQty(3, $product2->id); - - $this->expectException(CouldNotProvideCarrierDeliveryPrice::class); - $this->expectExceptionCode(ExceptionCode::ORDER_FAILED_TO_APPLY_SELECTED_CARRIER_FOR_SUBSCRIPTION_ORDER); - - /** @var CarrierDeliveryPriceProvider $carrierDeliveryPriceProvider */ - $carrierDeliveryPriceProvider = $this->getService(CarrierDeliveryPriceProvider::class); - - $carrierDeliveryPriceProvider->getPrice( - $address->id, - $cart->id, - $cart->id_customer, - $targetProductArray - ); - - $this->removeFactories([ - $carrier, - $address, - $cart, - $targetProduct, - $product1, - $product2, - ]); - } - - /** - * @param \ObjectModel[] $objects - */ - private function removeFactories(array $objects): void - { - foreach ($objects as $object) { - $object->delete(); - } - } -}