diff --git a/controllers/front/ajax.php b/controllers/front/ajax.php index 6c6ff8376..42a85cd38 100644 --- a/controllers/front/ajax.php +++ b/controllers/front/ajax.php @@ -16,7 +16,6 @@ use Mollie\Exception\FailedToProvidePaymentFeeException; use Mollie\Provider\PaymentFeeProviderInterface; use Mollie\Repository\CurrencyRepositoryInterface; -use Mollie\Subscription\Exception\ProductValidationException; use Mollie\Subscription\Exception\SubscriptionProductValidationException; use Mollie\Subscription\Validator\CanProductBeAddedToCartValidator; use Mollie\Utility\NumberUtility; @@ -192,12 +191,9 @@ private function validateProduct(): void try { $cartValidation->validate((int) $product['id_product_attribute']); - } catch (ProductValidationException $e) { - $productCanBeAdded = false; - $message = $this->module->l('Product cannot be added because you have subscription product in your cart', self::FILE_NAME); } catch (SubscriptionProductValidationException $e) { $productCanBeAdded = false; - $message = $this->module->l('Subscription product cannot be added if you have other products in your cart', self::FILE_NAME); + $message = $this->module->l('Please note: Only one subscription product can be added to the cart at a time.', self::FILE_NAME); } $this->ajaxRender( diff --git a/controllers/front/subscriptionWebhook.php b/controllers/front/subscriptionWebhook.php index e01c99c98..0fe91601f 100644 --- a/controllers/front/subscriptionWebhook.php +++ b/controllers/front/subscriptionWebhook.php @@ -64,7 +64,7 @@ protected function executeWebhook() try { $recurringOrderHandler->handle($transactionId); - } catch (Exception $exception) { + } catch (\Throwable $exception) { $errorHandler->handle($exception, null, false); $this->respond('failed', HttpStatusCode::HTTP_BAD_REQUEST); diff --git a/controllers/front/webhook.php b/controllers/front/webhook.php index 6b2e86d01..699298883 100644 --- a/controllers/front/webhook.php +++ b/controllers/front/webhook.php @@ -14,7 +14,6 @@ use Mollie\Config\Config; use Mollie\Controller\AbstractMollieController; use Mollie\Errors\Http\HttpStatusCode; -use Mollie\Exception\TransactionException; use Mollie\Handler\ErrorHandler\ErrorHandler; use Mollie\Service\TransactionService; use Mollie\Utility\TransactionUtility; @@ -73,6 +72,9 @@ protected function executeWebhook() /** @var TransactionService $transactionService */ $transactionService = $this->module->getService(TransactionService::class); + /** @var ErrorHandler $errorHandler */ + $errorHandler = $this->module->getService(ErrorHandler::class); + $transactionId = Tools::getValue('id'); if (!$transactionId) { $this->respond('failed', HttpStatusCode::HTTP_UNPROCESSABLE_ENTITY, 'Missing transaction id'); @@ -95,9 +97,7 @@ protected function executeWebhook() $cartId = $metaData->cart_id ?? 0; $this->setContext($cartId); $payment = $transactionService->processTransaction($transaction); - } catch (TransactionException $e) { - /** @var ErrorHandler $errorHandler */ - $errorHandler = $this->module->getService(ErrorHandler::class); + } catch (\Throwable $e) { $errorHandler->handle($e, $e->getCode(), false); $this->respond('failed', $e->getCode(), $e->getMessage()); } diff --git a/mollie.php b/mollie.php index 58e6542db..0b419b8d2 100755 --- a/mollie.php +++ b/mollie.php @@ -26,7 +26,6 @@ use Mollie\Repository\PaymentMethodRepositoryInterface; use Mollie\Service\ExceptionService; use Mollie\ServiceProvider\LeagueServiceContainerProvider; -use Mollie\Subscription\Exception\ProductValidationException; use Mollie\Subscription\Exception\SubscriptionProductValidationException; use Mollie\Subscription\Handler\CustomerAddressUpdateHandler; use Mollie\Subscription\Install\AttributeInstaller; @@ -999,10 +998,6 @@ public function hookActionCartUpdateQuantityBefore($params) } catch (SubscriptionProductValidationException $e) { $product = $this->makeProductNotOrderable($params['product']); - $params['product'] = $product; - } catch (ProductValidationException $e) { - $product = $this->makeProductNotOrderable($params['product']); - $params['product'] = $product; } } diff --git a/src/Handler/ErrorHandler/ErrorHandler.php b/src/Handler/ErrorHandler/ErrorHandler.php index ff701502c..e5c51bd69 100644 --- a/src/Handler/ErrorHandler/ErrorHandler.php +++ b/src/Handler/ErrorHandler/ErrorHandler.php @@ -13,7 +13,6 @@ namespace Mollie\Handler\ErrorHandler; use Configuration; -use Exception; use Mollie; use Mollie\Config\Config; use Mollie\Config\Env; @@ -94,9 +93,9 @@ public function __construct(Mollie $module, Env $env = null) } /** - * @throws Exception + * @throws \Throwable */ - public function handle(Exception $error, ?int $code = null, ?bool $throw = true): void + public function handle(\Throwable $error, ?int $code = null, ?bool $throw = true): void { $this->client->captureException($error, $this->exceptionContext); diff --git a/src/Handler/Order/OrderCreationHandler.php b/src/Handler/Order/OrderCreationHandler.php index 4f21ab40b..e09f39690 100644 --- a/src/Handler/Order/OrderCreationHandler.php +++ b/src/Handler/Order/OrderCreationHandler.php @@ -278,9 +278,10 @@ public function createBankTransferOrder($paymentData, Cart $cart) return $paymentData; } - private function createRecurringOrderEntity(Order $order, string $method) + private function createRecurringOrderEntity(Order $order, string $method): void { $cart = new Cart($order->id_cart); + if (!$this->subscriptionOrder->validate($cart)) { return; } diff --git a/subscription/Exception/ExceptionCode.php b/subscription/Exception/ExceptionCode.php index 2fabb1bcd..7820f0276 100644 --- a/subscription/Exception/ExceptionCode.php +++ b/subscription/Exception/ExceptionCode.php @@ -8,4 +8,8 @@ class ExceptionCode public const ORDER_FAILED_TO_CREATE_ORDER_PAYMENT_FEE = 1001; public const ORDER_FAILED_TO_UPDATE_ORDER_TOTAL_WITH_PAYMENT_FEE = 1002; + + //Cart error codes starts from 2000 + + public const CART_ALREADY_HAS_SUBSCRIPTION_PRODUCT = 2001; } diff --git a/subscription/Exception/ProductValidationException.php b/subscription/Exception/ProductValidationException.php deleted file mode 100644 index 52b90e556..000000000 --- a/subscription/Exception/ProductValidationException.php +++ /dev/null @@ -1,10 +0,0 @@ -module = $module; } - public function build(Order $order): SubscriptionDataDTO + public function build(Order $order, array $subscriptionProduct): SubscriptionDataDTO { $customer = $order->getCustomer(); /** @var \MolCustomer $molCustomer */ //todo: will need to improve mollie module logic to have shop id or card it so that multishop doesn't break $molCustomer = $this->customerRepository->findOneBy(['email' => $customer->email]); - $products = $order->getCartProducts(); - - // only one product is expected to be in order for subscription, if there are more than validation failed. - if (count($products) !== 1) { - throw new SubscriptionProductValidationException('Invalid amount of products for subscription', SubscriptionProductValidationException::MULTTIPLE_PRODUCTS_IN_CART); - } - /** @var Product $product */ - $product = reset($products); - $combination = $this->combination->getById((int) $product['id_product_attribute']); + $combination = $this->combination->getById((int) $subscriptionProduct['id_product_attribute']); $interval = $this->subscriptionInterval->getSubscriptionInterval($combination); $currency = $this->currencyAdapter->getById((int) $order->id_currency); $description = $this->subscriptionDescription->getSubscriptionDescription($order); - $orderAmount = new Amount((float) $order->total_paid_tax_incl, $currency->iso_code); + /** + * NOTE: we will only send product price as total for subscriptions + */ + $orderAmount = new Amount((float) $subscriptionProduct['total_price_tax_incl'], $currency->iso_code); $subscriptionData = new SubscriptionDataDTO( $molCustomer->customer_id, $orderAmount, diff --git a/subscription/Handler/RecurringOrderHandler.php b/subscription/Handler/RecurringOrderHandler.php index 757dad853..7d4ac50da 100644 --- a/subscription/Handler/RecurringOrderHandler.php +++ b/subscription/Handler/RecurringOrderHandler.php @@ -6,8 +6,6 @@ use Cart; use Mollie; -use Mollie\Action\CreateOrderPaymentFeeAction; -use Mollie\Action\UpdateOrderTotalsAction; use Mollie\Adapter\ConfigurationAdapter; use Mollie\Adapter\Shop; use Mollie\Api\Resources\Payment; @@ -15,14 +13,9 @@ use Mollie\Api\Types\PaymentStatus; use Mollie\Api\Types\SubscriptionStatus; use Mollie\Config\Config; -use Mollie\DTO\CreateOrderPaymentFeeActionData; -use Mollie\DTO\UpdateOrderTotalsData; use Mollie\Errors\Http\HttpStatusCode; -use Mollie\Exception\CouldNotCreateOrderPaymentFee; -use Mollie\Exception\CouldNotUpdateOrderTotals; use Mollie\Exception\OrderCreationException; use Mollie\Exception\TransactionException; -use Mollie\Repository\MolOrderPaymentFeeRepositoryInterface; use Mollie\Repository\PaymentMethodRepositoryInterface; use Mollie\Service\MailService; use Mollie\Service\MollieOrderCreationService; @@ -32,9 +25,9 @@ use Mollie\Subscription\Exception\CouldNotHandleRecurringOrder; use Mollie\Subscription\Factory\GetSubscriptionDataFactory; use Mollie\Subscription\Repository\RecurringOrderRepositoryInterface; +use Mollie\Subscription\Repository\RecurringOrdersProductRepositoryInterface; use Mollie\Subscription\Utility\ClockInterface; use Mollie\Utility\SecureKeyUtility; -use MolOrderPaymentFee; use MolRecurringOrder; use MolRecurringOrdersProduct; use Order; @@ -64,14 +57,10 @@ class RecurringOrderHandler private $shop; /** @var MailService */ private $mailService; - /** @var MolOrderPaymentFeeRepositoryInterface */ - private $molOrderPaymentFeeRepository; - /** @var UpdateOrderTotalsAction */ - private $updateOrderTotalsAction; - /** @var CreateOrderPaymentFeeAction */ - private $createOrderPaymentFeeAction; /** @var ConfigurationAdapter */ private $configuration; + /** @var RecurringOrdersProductRepositoryInterface */ + private $recurringOrdersProductRepository; public function __construct( SubscriptionApi $subscriptionApi, @@ -85,10 +74,8 @@ public function __construct( ClockInterface $clock, Shop $shop, MailService $mailService, - MolOrderPaymentFeeRepositoryInterface $molOrderPaymentFeeRepository, - UpdateOrderTotalsAction $updateOrderTotalsAction, - CreateOrderPaymentFeeAction $createOrderPaymentFeeAction, - ConfigurationAdapter $configuration + ConfigurationAdapter $configuration, + RecurringOrdersProductRepositoryInterface $recurringOrdersProductRepository ) { $this->subscriptionApi = $subscriptionApi; $this->subscriptionDataFactory = $subscriptionDataFactory; @@ -101,10 +88,8 @@ public function __construct( $this->clock = $clock; $this->shop = $shop; $this->mailService = $mailService; - $this->molOrderPaymentFeeRepository = $molOrderPaymentFeeRepository; - $this->updateOrderTotalsAction = $updateOrderTotalsAction; - $this->createOrderPaymentFeeAction = $createOrderPaymentFeeAction; $this->configuration = $configuration; + $this->recurringOrdersProductRepository = $recurringOrdersProductRepository; } public function handle(string $transactionId): string @@ -168,6 +153,24 @@ private function createSubscription(Payment $transaction, MolRecurringOrder $rec /** @var Cart $newCart */ $newCart = $newCart['cart']; + /** @var MolRecurringOrdersProduct $subscriptionProduct */ + $subscriptionProduct = $this->recurringOrdersProductRepository->findOneBy([ + 'id_mol_recurring_orders_product' => $recurringOrder->id_mol_recurring_orders_product, + ]); + + $cartProducts = $newCart->getProducts(); + + foreach ($cartProducts as $cartProduct) { + if ( + (int) $cartProduct['id_product'] === (int) $subscriptionProduct->id_product && + (int) $cartProduct['id_product_attribute'] === (int) $subscriptionProduct->id_product_attribute + ) { + continue; + } + + $newCart->deleteProduct((int) $cartProduct['id_product'], (int) $cartProduct['id_product_attribute']); + } + /** * NOTE: New order can't have soft deleted delivery address */ @@ -200,37 +203,6 @@ private function createSubscription(Payment $transaction, MolRecurringOrder $rec $this->mollieOrderCreationService->createMolliePayment($transaction, (int) $newCart->id, $order->reference, (int) $orderId, PaymentStatus::STATUS_PAID); - /** @var MolOrderPaymentFee|null $molOrderPaymentFee */ - $molOrderPaymentFee = $this->molOrderPaymentFeeRepository->findOneBy([ - 'id_order' => $recurringOrder->id_order, - ]); - - if ($molOrderPaymentFee) { - try { - $this->createOrderPaymentFeeAction->run(CreateOrderPaymentFeeActionData::create( - $orderId, - (int) $newCart->id, - (float) $molOrderPaymentFee->fee_tax_incl, - (float) $molOrderPaymentFee->fee_tax_excl - )); - } catch (CouldNotCreateOrderPaymentFee $exception) { - throw CouldNotHandleRecurringOrder::failedToCreateOrderPaymentFee($exception); - } - - try { - $this->updateOrderTotalsAction->run(UpdateOrderTotalsData::create( - $orderId, - (float) $molOrderPaymentFee->fee_tax_incl, - (float) $molOrderPaymentFee->fee_tax_excl, - (float) $transaction->amount->value, - (float) $cart->getOrderTotal(true, Cart::BOTH), - (float) $cart->getOrderTotal(false, Cart::BOTH) - )); - } catch (CouldNotUpdateOrderTotals $exception) { - throw CouldNotHandleRecurringOrder::failedToUpdateOrderTotalWithPaymentFee($exception); - } - } - $this->orderStatusService->setOrderStatus($orderId, (int) Config::getStatuses()[$transaction->status]); } diff --git a/subscription/Handler/SubscriptionCreationHandler.php b/subscription/Handler/SubscriptionCreationHandler.php index f0b90816d..87f5f8d6a 100644 --- a/subscription/Handler/SubscriptionCreationHandler.php +++ b/subscription/Handler/SubscriptionCreationHandler.php @@ -8,6 +8,7 @@ use Mollie\Subscription\Api\SubscriptionApi; use Mollie\Subscription\Factory\CreateSubscriptionDataFactory; use Mollie\Subscription\Utility\ClockInterface; +use Mollie\Subscription\Validator\SubscriptionProductValidator; use MolRecurringOrder; use MolRecurringOrdersProduct; use Order; @@ -22,26 +23,40 @@ class SubscriptionCreationHandler /** @var CreateSubscriptionDataFactory */ private $createSubscriptionDataFactory; + /** @var SubscriptionProductValidator */ + private $subscriptionProductValidator; public function __construct( ClockInterface $clock, SubscriptionApi $subscriptionApi, - CreateSubscriptionDataFactory $subscriptionDataFactory + CreateSubscriptionDataFactory $subscriptionDataFactory, + SubscriptionProductValidator $subscriptionProductValidator ) { $this->clock = $clock; $this->subscriptionApi = $subscriptionApi; $this->createSubscriptionDataFactory = $subscriptionDataFactory; + $this->subscriptionProductValidator = $subscriptionProductValidator; } public function handle(Order $order, string $method) { - $subscriptionData = $this->createSubscriptionDataFactory->build($order); - $subscription = $this->subscriptionApi->subscribeOrder($subscriptionData); - $products = $order->getProducts(); - $product = reset($products); + $subscriptionProduct = []; + + foreach ($products as $product) { + if (!$this->subscriptionProductValidator->validate((int) $product['product_attribute_id'])) { + continue; + } + + $subscriptionProduct = $product; + + break; + } + + $subscriptionData = $this->createSubscriptionDataFactory->build($order, $subscriptionProduct); + $subscription = $this->subscriptionApi->subscribeOrder($subscriptionData); - $recurringOrdersProduct = $this->createRecurringOrdersProduct($product); + $recurringOrdersProduct = $this->createRecurringOrdersProduct($subscriptionProduct); $this->createRecurringOrder($recurringOrdersProduct, $order, $subscription, $method); } @@ -52,7 +67,7 @@ private function createRecurringOrdersProduct(array $product): MolRecurringOrder $recurringOrdersProduct->id_product = $product['id_product']; $recurringOrdersProduct->id_product_attribute = $product['product_attribute_id']; $recurringOrdersProduct->quantity = $product['product_quantity']; - $recurringOrdersProduct->unit_price = $product['price']; + $recurringOrdersProduct->unit_price = $product['unit_price_tax_excl']; $recurringOrdersProduct->add(); return $recurringOrdersProduct; diff --git a/subscription/Validator/CanProductBeAddedToCartValidator.php b/subscription/Validator/CanProductBeAddedToCartValidator.php index d7102e684..1060d0139 100644 --- a/subscription/Validator/CanProductBeAddedToCartValidator.php +++ b/subscription/Validator/CanProductBeAddedToCartValidator.php @@ -6,7 +6,7 @@ use Mollie\Adapter\CartAdapter; use Mollie\Adapter\ToolsAdapter; -use Mollie\Subscription\Exception\ProductValidationException; +use Mollie\Subscription\Exception\ExceptionCode; use Mollie\Subscription\Exception\SubscriptionProductValidationException; class CanProductBeAddedToCartValidator @@ -32,12 +32,13 @@ public function __construct( /** * Validates if product can be added to the cart. - * Only 1 subscription product can be in cart and no other products can be added if there are subscription products - * For now we only allow one subscription product with any quantities, later might need to add logic to allow more products + * Only 1 subscription product can be to the cart * * @param int $productAttributeId * * @return bool + * + * @throws SubscriptionProductValidationException */ public function validate(int $productAttributeId): bool { @@ -49,11 +50,7 @@ public function validate(int $productAttributeId): bool $isNewSubscriptionProduct = $this->subscriptionProduct->validate($productAttributeId); - if ($isNewSubscriptionProduct) { - return $this->validateIfSubscriptionProductCanBeAdded($productAttributeId); - } - - return $this->validateIfProductCanBeAdded(); + return !$isNewSubscriptionProduct || $this->validateIfSubscriptionProductCanBeAdded($productAttributeId); } /** @@ -66,39 +63,17 @@ public function validate(int $productAttributeId): bool private function validateIfSubscriptionProductCanBeAdded(int $productAttributeId): bool { $cartProducts = $this->cart->getProducts(); - $numberOfProductsInCart = count($cartProducts); - // we can only have 1 product in cart if its subscription product - if ($numberOfProductsInCart > 1) { - throw new SubscriptionProductValidationException('Cart has multiple products', SubscriptionProductValidationException::MULTTIPLE_PRODUCTS_IN_CART); - } - // if it's the same product we can add more of the same product - if ($numberOfProductsInCart === 1) { - $cartProduct = reset($cartProducts); - - $isTheSameProduct = $productAttributeId === (int) $cartProduct['id_product_attribute']; - - if (!$isTheSameProduct) { - throw new SubscriptionProductValidationException('Cart has multiple products', SubscriptionProductValidationException::MULTTIPLE_PRODUCTS_IN_CART); + foreach ($cartProducts as $cartProduct) { + if (!$this->subscriptionProduct->validate((int) $cartProduct['id_product_attribute'])) { + continue; } - } - - return true; - } - /** - * @return bool - * - * @throws ProductValidationException - */ - private function validateIfProductCanBeAdded(): bool - { - $cartProducts = $this->cart->getProducts(); - foreach ($cartProducts as $cartProduct) { - $isSubscriptionProduct = $this->subscriptionProduct->validate((int) $cartProduct['id_product_attribute']); - if ($isSubscriptionProduct) { - throw new ProductValidationException('Cart has subscription products', ProductValidationException::SUBSCRIPTTION_PRODUCTS_IN_CART); + if ((int) $cartProduct['id_product_attribute'] === $productAttributeId) { + continue; } + + throw new SubscriptionProductValidationException('Cart already has subscription product', ExceptionCode::CART_ALREADY_HAS_SUBSCRIPTION_PRODUCT); } return true; diff --git a/subscription/Validator/SubscriptionOrderValidator.php b/subscription/Validator/SubscriptionOrderValidator.php index 100dbe24c..d6948f6aa 100644 --- a/subscription/Validator/SubscriptionOrderValidator.php +++ b/subscription/Validator/SubscriptionOrderValidator.php @@ -16,23 +16,20 @@ public function __construct(SubscriptionProductValidator $subscriptionProduct) $this->subscriptionProduct = $subscriptionProduct; } - /** Returns true if cart has subscription products */ + /** Returns true if cart has subscription product */ public function validate(Cart $cart): bool { $products = $cart->getProducts(); - // only one product can be subscribed at a time - if (count($products) !== 1) { - return false; - } - // checks if product is subscription product - // foreach is not necessary but might need to add more possible products for subscription in later updates + $subscriptionProductCount = 0; + + // checks if one of cart products is subscription product foreach ($products as $product) { if ($this->subscriptionProduct->validate((int) $product['id_product_attribute'])) { - return true; + ++$subscriptionProductCount; } } - return false; + return $subscriptionProductCount > 0 && $subscriptionProductCount < 2; } } diff --git a/subscription/Validator/SubscriptionProductValidator.php b/subscription/Validator/SubscriptionProductValidator.php index 3c24ec80a..e00f345f2 100644 --- a/subscription/Validator/SubscriptionProductValidator.php +++ b/subscription/Validator/SubscriptionProductValidator.php @@ -43,6 +43,7 @@ public function validate(int $productAttributeId): bool { $combination = $this->combination->getById($productAttributeId); $attributeIds = $this->combinationRepository->getIds((int) $combination->id); + foreach ($attributeIds as $attributeId) { if ($this->isSubscriptionAttribute((int) $attributeId['id_attribute'])) { return true; diff --git a/tests/Integration/Subscription/Factory/TestCreateSubscriptionData.php b/tests/Integration/Subscription/Factory/TestCreateSubscriptionData.php index c70d746c7..a3f3b0a2f 100644 --- a/tests/Integration/Subscription/Factory/TestCreateSubscriptionData.php +++ b/tests/Integration/Subscription/Factory/TestCreateSubscriptionData.php @@ -103,7 +103,12 @@ public function testBuild() ] ); - $subscriptionData = $createSubscriptionData->build($orderMock); + $subscriptionProduct = [ + 'id_product_attribute' => 999, + 'total_price_tax_incl' => 19.99, + ]; + + $subscriptionData = $createSubscriptionData->build($orderMock, $subscriptionProduct); $this->assertEquals(self::CUSTOMER_ID, $subscriptionData->getCustomerId()); $this->assertEquals( diff --git a/tests/Integration/Subscription/Validator/SubscriptionCartTest.php b/tests/Integration/Subscription/Validator/CanProductBeAddedToCartValidatorTest.php similarity index 93% rename from tests/Integration/Subscription/Validator/SubscriptionCartTest.php rename to tests/Integration/Subscription/Validator/CanProductBeAddedToCartValidatorTest.php index 248217403..319709425 100644 --- a/tests/Integration/Subscription/Validator/SubscriptionCartTest.php +++ b/tests/Integration/Subscription/Validator/CanProductBeAddedToCartValidatorTest.php @@ -1,9 +1,10 @@ randomAttributeId = self::NORMAL_PRODUCT_ATTRIBUTE_ID; @@ -58,7 +60,6 @@ protected function setUp(): void */ public function testValidate(string $combinationReference, bool $hasExtraAttribute, array $cartProducts, $expectedResult): void { - $language = new Language(1); $cartAdapterMock = $this->createMock(CartAdapter::class); $cartProducts = array_map(function (array $product) { @@ -114,7 +115,7 @@ public function productDataProvider(): array 'id_product_attribute' => self::NORMAL_PRODUCT_ATTRIBUTE_ID, ], ], - 'expected result' => SubscriptionProductValidationException::class, + 'expected result' => true, ], 'Add subscription product but already have another subscription product in cart' => [ 'subscription reference' => Config::SUBSCRIPTION_ATTRIBUTE_DAILY, @@ -134,7 +135,7 @@ public function productDataProvider(): array 'id_product_attribute' => Config::SUBSCRIPTION_ATTRIBUTE_MONTHLY, ], ], - 'expected result' => ProductValidationException::class, + 'expected result' => true, ], 'Add normal product but already have another normal product in cart' => [ 'subscription reference' => '', @@ -159,6 +160,6 @@ private function getCombination(string $combinationReference, bool $hasExtraAttr ]) : $this->randomAttributeId; } - return (int) Combination::getIdByReference(1, $reference); + return (int) \Combination::getIdByReference(1, $reference); } } diff --git a/tests/Integration/Subscription/Validator/SubscriptionOrderTest.php b/tests/Integration/Subscription/Validator/SubscriptionOrderValidatorTest.php similarity index 94% rename from tests/Integration/Subscription/Validator/SubscriptionOrderTest.php rename to tests/Integration/Subscription/Validator/SubscriptionOrderValidatorTest.php index df246ecc5..dd2467b2d 100644 --- a/tests/Integration/Subscription/Validator/SubscriptionOrderTest.php +++ b/tests/Integration/Subscription/Validator/SubscriptionOrderValidatorTest.php @@ -1,19 +1,21 @@ randomAttributeId = self::NORMAL_PRODUCT_ATTRIBUTE_ID; @@ -64,7 +66,7 @@ public function testValidate(array $orderProducts, $expectedResult): void $combinationMock = $this->createMock(CombinationAdapter::class); $combinationMock ->method('getById') - ->willReturn(new Combination(1)); + ->willReturn(new \Combination(1)); $subscriptionProductMock = $this->createMock(SubscriptionProductValidator::class); $mockedValidation = [ @@ -106,7 +108,7 @@ public function productDataProvider(): array Config::SUBSCRIPTION_ATTRIBUTE_DAILY, self::NORMAL_PRODUCT_ATTRIBUTE_ID, ], - 'expected result' => false, + 'expected result' => true, ], 'Only normal product' => [ 'order products' => [ diff --git a/tests/Integration/Subscription/Validator/SubscriptionProductTest.php b/tests/Integration/Subscription/Validator/SubscriptionProductValidatorTest.php similarity index 93% rename from tests/Integration/Subscription/Validator/SubscriptionProductTest.php rename to tests/Integration/Subscription/Validator/SubscriptionProductValidatorTest.php index 0b50b6c68..7448e07cb 100644 --- a/tests/Integration/Subscription/Validator/SubscriptionProductTest.php +++ b/tests/Integration/Subscription/Validator/SubscriptionProductValidatorTest.php @@ -1,15 +1,18 @@ randomAttributeId = 1; @@ -90,6 +93,6 @@ private function getCombination(string $combinationReference, bool $hasExtraAttr ]) : $this->randomAttributeId; } - return (int) Combination::getIdByReference(1, $reference); + return (int) \Combination::getIdByReference(1, $reference); } } diff --git a/tests/Unit/Factory/SubscriptionDataTest.php b/tests/Unit/Factory/SubscriptionDataTest.php index f133fdaf7..edbe8e30c 100644 --- a/tests/Unit/Factory/SubscriptionDataTest.php +++ b/tests/Unit/Factory/SubscriptionDataTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Mollie\Subscription\Tests\Unit\Factory; +namespace Mollie\Tests\Unit\Factory; use Mollie; use Mollie\Adapter\Link; @@ -74,11 +74,6 @@ public function testBuildSubscriptionData(string $customerId, float $totalAmount $order = $this->createMock('Order'); $order->method('getCustomer')->willReturn($customerMock); - $order->method('getCartProducts')->willReturn([ - [ - 'id_product_attribute' => 1, - ], - ]); $order->id = self::TEST_ORDER_ID; $order->reference = self::TEST_ORDER_REFERENCE; $order->id_cart = self::TEST_CART_ID; @@ -86,7 +81,12 @@ public function testBuildSubscriptionData(string $customerId, float $totalAmount $order->id_currency = 1; $order->total_paid_tax_incl = $totalAmount; - $subscriptionData = $subscriptionDataFactory->build($order); + $subscriptionProduct = [ + 'id_product_attribute' => 1, + 'total_price_tax_incl' => 19.99, + ]; + + $subscriptionData = $subscriptionDataFactory->build($order, $subscriptionProduct); $this->assertEquals($expectedResult, $subscriptionData); }