diff --git a/tests/test-wc-retailcrm-loyalty.php b/tests/test-wc-retailcrm-loyalty.php index 82b9605..795bf59 100644 --- a/tests/test-wc-retailcrm-loyalty.php +++ b/tests/test-wc-retailcrm-loyalty.php @@ -43,7 +43,7 @@ class WC_Retailcrm_Loyalty_Test extends WC_Retailcrm_Test_Case_Helper $this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Client_V5') ->disableOriginalConstructor() - ->setMethods(['customersGet', 'getLoyaltyAccountList', 'createLoyaltyAccount', 'activateLoyaltyAccount', 'calculateDiscountLoyalty', 'getSingleSiteForKey']) + ->setMethods(['customersGet', 'getLoyaltyAccountList', 'createLoyaltyAccount', 'activateLoyaltyAccount', 'calculateDiscountLoyalty', 'getSingleSiteForKey', 'applyBonusToOrder']) ->getMock() ; @@ -227,10 +227,226 @@ class WC_Retailcrm_Loyalty_Test extends WC_Retailcrm_Test_Case_Helper public function testCreateLoyaltyCouponWithRefreshCoupon() { + $products = DataLoyaltyRetailCrm::createProducts(); + $user = DataLoyaltyRetailcrm::createUsers()[0]; + $cart = new WC_Cart(); + $cart->add_to_cart($products[0]->get_id()); + $cart->add_to_cart($products[1]->get_id()); + + $coupon = new WC_Coupon(); + $coupon->set_usage_limit(0); + $coupon->set_amount('50'); + $coupon->set_email_restrictions($user->get_email()); + $coupon->set_code('pl' . mt_rand()); + $coupon->save(); + $cart->apply_coupon($coupon->get_code()); + + $couponCode = $coupon->get_code(); + $woocommerce = wc(); + $woocommerce->cart = $cart; + $woocommerce->customer = $user; + + $validatorMock = $this->getMockBuilder('\WC_Retailcrm_Loyalty_Validator') + ->disableOriginalConstructor() + ->getMock() + ; + $this->setMockResponse($validatorMock, 'checkAccount', true); + $validatorMock->loyaltyAccount['level']['type'] = 'discount'; + + $responseCalculation = DataLoyaltyRetailCrm::getDataCalculation()[1]; + $responseMock = new WC_Retailcrm_Response(200, json_encode($responseCalculation['response'])); + $this->setMockResponse($this->apiMock, 'calculateDiscountLoyalty', $responseMock); + + $this->loyalty = new WC_Retailcrm_Loyalty($this->apiMock, []); + $reflection = new \ReflectionClass($this->loyalty); + $reflection_property = $reflection->getProperty('validator'); + $reflection_property->setAccessible(true); + $reflection_property->setValue($this->loyalty, $validatorMock); + + $GLOBALS['woocommerce'] = $woocommerce; + $result = $this->loyalty->createLoyaltyCoupon(true); + + $this->assertEmpty($result); + $this->assertNotEmpty($this->loyalty->getCouponLoyalty($woocommerce->customer->get_email())); + + $coupons = $woocommerce->cart->get_coupons(); + + $this->assertNotEmpty($coupons); + $this->assertFalse(isset($coupons[$couponCode])); } + public function testDeleteLoyaltyCouponInOrder() + { + $products = DataLoyaltyRetailCrm::createProducts(); + $user = DataLoyaltyRetailcrm::createUsers()[0]; + $coupon = new WC_Coupon(); + $coupon->set_usage_limit(0); + $coupon->set_amount('50'); + $coupon->set_email_restrictions($user->get_email()); + $coupon->set_code('pl' . mt_rand()); + $coupon->save(); + + $wcOrder = wc_create_order([ + 'status' => null, + 'customer_id' => $user->get_id(), + 'customer_note' => null, + 'parent' => null, + 'created_via' => null, + 'cart_hash' => null, + 'order_id' => 0, + ]); + + $wcOrder->add_product($products[0]); + $wcOrder->add_product($products[1]); + $wcOrder->apply_coupon($coupon->get_code()); + $wcOrder->calculate_totals(); + $wcOrder->save(); + + $this->assertNotEmpty($wcOrder->get_coupons()); + $this->loyalty->deleteLoyaltyCouponInOrder($wcOrder); + $this->assertEmpty($wcOrder->get_coupons()); + + $wcOrder->delete(true); + } + + public function testApplyLoyaltyDiscountWithBonuses() + { + $products = DataLoyaltyRetailCrm::createProducts(); + $user = DataLoyaltyRetailcrm::createUsers()[0]; + + $wcOrder = wc_create_order([ + 'status' => null, + 'customer_id' => $user->get_id(), + 'customer_note' => null, + 'parent' => null, + 'created_via' => null, + 'cart_hash' => null, + 'order_id' => 0, + ]); + + $wcOrder->add_product($products[0]); + $wcOrder->add_product($products[1]); + $wcOrder->calculate_totals(); + $wcOrder->save(); + + foreach ($wcOrder->get_items() as $id => $item) { + $currentItemsPrice[$id] = $item->get_total(); + $itemIds[] = $id; + } + + $createdCrmOrderResponse = ['site' => 'test', 'externalId' => 1, 'items' => [['discounts' => [],], ['discounts' => []]]]; + $response = new WC_Retailcrm_Response( + 200, + json_encode([ + 'order' => [ + 'items' => [ + [ + 'externalIds' => [ + [ + 'value' => '11_' . $itemIds[0] + ] + ], + 'discounts' => [ + [ + 'type' => 'bonus_charge', + 'amount' => 25 + ] + ] + ], + [ + 'externalIds' => [ + [ + 'value' => '22_' . $itemIds[1] + ] + ], + 'discounts' => [ + [ + 'type' => 'bonus_charge', + 'amount' => 25 + ] + ] + ] + ] + ] + ]) + ); + + $this->setMockResponse($this->apiMock, 'applyBonusToOrder', $response); + $this->loyalty = new WC_Retailcrm_Loyalty($this->apiMock, []); + + $this->loyalty->applyLoyaltyDiscount($wcOrder, 50, $createdCrmOrderResponse); + + foreach ($wcOrder->get_items() as $id => $item) { + $this->assertNotEquals($item->get_total(), $currentItemsPrice[$id]); + } + } + + public function testApplyLoyaltyDiscountWithPercentDiscount() + { + $products = DataLoyaltyRetailCrm::createProducts(); + $user = DataLoyaltyRetailcrm::createUsers()[0]; + + $wcOrder = wc_create_order([ + 'status' => null, + 'customer_id' => $user->get_id(), + 'customer_note' => null, + 'parent' => null, + 'created_via' => null, + 'cart_hash' => null, + 'order_id' => 0, + ]); + + $wcOrder->add_product($products[0]); + $wcOrder->add_product($products[1]); + $wcOrder->calculate_totals(); + $wcOrder->save(); + + foreach ($wcOrder->get_items() as $id => $item) { + $currentItemsPrice[$id] = $item->get_total(); + $itemIds[] = $id; + } + + $createdCrmOrderResponse = [ + 'site' => 'test', + 'externalId' => 1, + 'items' => [ + [ + 'externalIds' => [ + [ + 'value' => '11_' . $itemIds[0] + ] + ], + 'discounts' => [ + [ + 'type' => 'loyalty_level', + 'amount' => 25 + ] + ] + ], + [ + 'externalIds' => [ + [ + 'value' => '22_' . $itemIds[1] + ] + ], + 'discounts' => [ + [ + 'type' => 'loyalty_level', + 'amount' => 25 + ] + ] + ] + ] + ]; + + $this->loyalty->applyLoyaltyDiscount($wcOrder, 0, $createdCrmOrderResponse); + + foreach ($wcOrder->get_items() as $id => $item) { + $this->assertNotEquals($item->get_total(), $currentItemsPrice[$id]); + } + } private function getPrivateMethod($method, $class) {