From 69638775b285067ee37dd00be294de484cf8b5c1 Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Wed, 15 May 2024 17:59:10 +0300 Subject: [PATCH] ref #72069 Moving validator initialization to the constructor Creating tests --- src/include/class-wc-retailcrm-loyalty.php | 12 ++- tests/datasets/data-loyalty-retailcrm.php | 95 ++++++++++++---------- tests/test-wc-retailcrm-loyalty.php | 90 +++++++++++++++++--- 3 files changed, 141 insertions(+), 56 deletions(-) diff --git a/src/include/class-wc-retailcrm-loyalty.php b/src/include/class-wc-retailcrm-loyalty.php index f4c86a7..618209f 100644 --- a/src/include/class-wc-retailcrm-loyalty.php +++ b/src/include/class-wc-retailcrm-loyalty.php @@ -27,12 +27,18 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : /** @var WC_Retailcrm_Loyalty_Form */ protected $loyaltyForm; + protected $validator; + public function __construct($apiClient, $settings) { $this->apiClient = $apiClient; $this->settings = $settings; $this->dateFormat = 'Y-m-d H:i:sP'; $this->loyaltyForm = new WC_Retailcrm_Loyalty_Form(); + $this->validator = new WC_Retailcrm_Loyalty_Validator( + $this->apiClient, + $this->settings['corporate_enabled'] ?? WC_Retailcrm_Base::NO + ); } public function getForm(int $userId) @@ -225,9 +231,7 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : $woocommerce->cart->calculate_totals(); } - $validator = new WC_Retailcrm_Loyalty_Validator($this->apiClient, $this->settings['corporate_enabled'] ?? WC_Retailcrm_Base::NO); - - if (!$validator->checkAccount($customerId)) { + if (!$this->validator->checkAccount($customerId)) { return null; } @@ -262,7 +266,7 @@ if (!class_exists('WC_Retailcrm_Loyalty')) : } //If a percentage discount, automatically apply a loyalty coupon - if ($validator->loyaltyAccount['level']['type'] === 'discount') { + if ($this->validator->loyaltyAccount['level']['type'] === 'discount') { $woocommerce->cart->apply_coupon($coupon->get_code()); return $resultString; diff --git a/tests/datasets/data-loyalty-retailcrm.php b/tests/datasets/data-loyalty-retailcrm.php index 84ca2d1..f688118 100644 --- a/tests/datasets/data-loyalty-retailcrm.php +++ b/tests/datasets/data-loyalty-retailcrm.php @@ -250,26 +250,9 @@ class DataLoyaltyRetailCrm ] ]; - $coupon = new WC_Coupon(); - - $coupon->set_usage_limit(0); - $coupon->set_amount(100); - $coupon->set_email_restrictions('test1@gmail.com'); - $coupon->set_code('pl' . mt_rand()); - $coupon->save(); - - $data[0]['expectedCode'] = $coupon->get_code(); - - $coupon = new WC_Coupon(); - - $coupon->set_usage_limit(0); - $coupon->set_amount(100); - $coupon->set_email_restrictions('test2@gmail.com'); - $coupon->set_code('pl' . mt_rand()); - $coupon->save(); - - $data[1]['expectedCode'] = $coupon->get_code(); - + $coupons = self::createCoupons(); + $data[0]['expectedCode'] = $coupons[0]->get_code(); + $data[1]['expectedCode'] = $coupons[1]->get_code(); $data[2] = [ 'email' => 'test3@gmail.com', 'expectedCode' => false @@ -279,6 +262,34 @@ class DataLoyaltyRetailCrm } public static function dataValidUser() + { + $users = self::createUsers(); + + return [ + [ + 'customer' => $users[0], + 'corporate_enabled' => 'yes', + 'expected' => true + ], + [ + 'customer' => $users[1], + 'corporate_enabled' => 'yes', + 'expected' => false + ], + [ + 'customer' => $users[1], + 'corporate_enabled' => 'no', + 'expected' => true + ], + [ + 'customer' => null, + 'corporate_enabled' => 'yes', + 'expected' => false + ] + ]; + } + + public static function createUsers() { $customer = new WC_Customer(); @@ -301,27 +312,27 @@ class DataLoyaltyRetailCrm $customer1->set_shipping_company('OOO TEST'); $customer1->save(); - return [ - [ - 'customer' => $customer, - 'corporate_enabled' => 'yes', - 'expected' => true - ], - [ - 'customer' => $customer1, - 'corporate_enabled' => 'yes', - 'expected' => false - ], - [ - 'customer' => $customer1, - 'corporate_enabled' => 'no', - 'expected' => true - ], - [ - 'customer' => null, - 'corporate_enabled' => 'yes', - 'expected' => false - ] - ]; + return [$customer, $customer1]; + } + + public static function createCoupons($email1 = 'test1@gmail.com', $email2 = 'test2@gmail.com') + { + $coupon = new WC_Coupon(); + + $coupon->set_usage_limit(0); + $coupon->set_amount(100); + $coupon->set_email_restrictions($email1); + $coupon->set_code('pl' . mt_rand()); + $coupon->save(); + + $coupon1 = new WC_Coupon(); + + $coupon1->set_usage_limit(0); + $coupon1->set_amount(100); + $coupon1->set_email_restrictions($email2); + $coupon1->set_code('pl' . mt_rand()); + $coupon1->save(); + + return [$coupon, $coupon1]; } } diff --git a/tests/test-wc-retailcrm-loyalty.php b/tests/test-wc-retailcrm-loyalty.php index 7047206..82b9605 100644 --- a/tests/test-wc-retailcrm-loyalty.php +++ b/tests/test-wc-retailcrm-loyalty.php @@ -43,13 +43,14 @@ 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']) + ->setMethods(['customersGet', 'getLoyaltyAccountList', 'createLoyaltyAccount', 'activateLoyaltyAccount', 'calculateDiscountLoyalty', 'getSingleSiteForKey']) ->getMock() ; $this->setMockResponse($this->apiMock, 'customersGet', ['customer' => ['id' => 1]]); $this->setMockResponse($this->apiMock, 'createLoyaltyAccount', $this->responseMock); $this->setMockResponse($this->apiMock, 'activateLoyaltyAccount', $this->responseMock); + $this->setMockResponse($this->apiMock, 'getSingleSiteForKey', 'woo'); $this->loyalty = new WC_Retailcrm_Loyalty($this->apiMock, []); } @@ -149,19 +150,88 @@ class WC_Retailcrm_Loyalty_Test extends WC_Retailcrm_Test_Case_Helper } } - /** - * @group loyalty - */ - public function testCreateLoyaltyCoupon() + public function testCreateLoyaltyCouponWithoutAppliedCoupon() { + $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()); + $woocommerce = wc(); - var_dump($woocommerce->cart); - /*$woocommerce->cart = 'test'; - var_dump($woocommerce->cart);*/ - //die(); - $this->loyalty->createLoyaltyCoupon(false); + $woocommerce->cart = $cart; + $woocommerce->customer = $user; + + $validatorMock = $this->getMockBuilder('\WC_Retailcrm_Loyalty_Validator') + ->disableOriginalConstructor() + ->getMock() + ; + $this->setMockResponse($validatorMock, 'checkAccount', true); + $validatorMock->loyaltyAccount['level']['type'] = 'loyalty_level'; + + $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(); + + $this->assertNotEmpty($result); + $this->assertNotEmpty($this->loyalty->getCouponLoyalty($woocommerce->customer->get_email())); } + public function testCreateLoyaltyCouponWithPercentDiscount() + { + $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()); + + $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(); + + $this->assertEmpty($result); + $this->assertNotEmpty($this->loyalty->getCouponLoyalty($woocommerce->customer->get_email())); + $this->assertNotEmpty($woocommerce->cart->get_coupons()); + } + + public function testCreateLoyaltyCouponWithRefreshCoupon() + { + + } + + + private function getPrivateMethod($method, $class) { $reflection = new ReflectionClass($class);