diff --git a/src/include/validators/loyalty-validator/class-wc-retailcrm-loyalty-validator.php b/src/include/validators/loyalty-validator/class-wc-retailcrm-loyalty-validator.php index 542ff3c..a5fffe5 100644 --- a/src/include/validators/loyalty-validator/class-wc-retailcrm-loyalty-validator.php +++ b/src/include/validators/loyalty-validator/class-wc-retailcrm-loyalty-validator.php @@ -17,12 +17,9 @@ if (!class_exists('WC_Retailcrm_Loyalty_Validator')) : /** @var WC_Retailcrm_Client_V5 */ protected $apiClient; - protected $settings; - - public function __construct($apiClient, $settings) + public function __construct($apiClient) { $this->apiClient = $apiClient; - $this->settings = $settings; } public function checkAccount(int $userId) @@ -31,10 +28,8 @@ if (!class_exists('WC_Retailcrm_Loyalty_Validator')) : try { $crmUser = $this->checkUser($userId); - //add check customer corporate $actualAccount = $this->getLoyaltyAccount($crmUser['id']); $this->checkActiveLoyalty($actualAccount['loyalty']['id']); - } catch (Throwable $exception) { if ($exception instanceof ValidatorException) { WC_Admin_Settings::add_error((esc_html__($exception->getMessage(), 'retailcrm')) . "userId: $userId"); diff --git a/src/retailcrm.php b/src/retailcrm.php index 0a5b3ba..5445b72 100644 --- a/src/retailcrm.php +++ b/src/retailcrm.php @@ -134,6 +134,8 @@ if (!class_exists( 'WC_Integration_Retailcrm')) : require_once(self::checkCustomFile('include/validators/url-validator/class-wc-retailcrm-url-validator.php')); require_once(self::checkCustomFile('include/validators/class-wc-retailcrm-validator-exception.php')); require_once(self::checkCustomFile('include/components/class-wc-retailcrm-loyalty-form.php')); + require_once(self::checkCustomFile('include/validators/loyalty-validator/class-wc-retailcrm-loyalty-constraint.php')); + require_once(self::checkCustomFile('include/validators/loyalty-validator/class-wc-retailcrm-loyalty-validator.php')); } /** diff --git a/tests/validators/test-wc-retailcrm-loyalty-validator.php b/tests/validators/test-wc-retailcrm-loyalty-validator.php new file mode 100644 index 0000000..5773da3 --- /dev/null +++ b/tests/validators/test-wc-retailcrm-loyalty-validator.php @@ -0,0 +1,211 @@ +individualClient = new WC_Customer(); + $this->individualClient->set_first_name('Test'); + $this->individualClient->set_last_name('Test'); + $this->individualClient->set_email(uniqid(md5(date('Y-m-d H:i:s'))) . '@mail.com'); + $this->individualClient->set_billing_email( $this->individualClient->get_email()); + $this->individualClient->set_password('password'); + $this->individualClient->set_billing_phone('89000000000'); + $this->individualClient->set_date_created(date('Y-m-d H:i:s')); + $this->individualClient->save(); + + $this->corpClient = new WC_Customer(); + $this->corpClient->set_first_name('Test'); + $this->corpClient->set_last_name('Test'); + $this->corpClient->set_email(uniqid(md5(date('Y-m-d H:i:s'))) . '@mail.com'); + $this->corpClient->set_billing_email($this->corpClient->get_email()); + $this->corpClient->set_password('password'); + $this->corpClient->set_billing_phone('89000000000'); + $this->corpClient->set_date_created(date('Y-m-d H:i:s')); + $this->corpClient->set_shipping_company('TEST COMPANY'); + $this->corpClient->save(); + } + + /** @dataProvider dataCheckUser */ + public function testCheckUser($responseApiMethod, $wcUserType, $throwMessage) + { + $this->setResponseMock(); + $this->setApiMock('customersGet', $responseApiMethod); + + $validator = new WC_Retailcrm_Loyalty_Validator($this->apiMock); + $method = $this->getPrivateMethod('checkUser', $validator); + + $wcUserId = $wcUserType === 'individual' ? $this->individualClient->get_id() : $this->corpClient->get_id(); + + try { + $result = $method->invokeArgs($validator, [$wcUserId]); + + if ($throwMessage) { + $this->fail('ValidatorException was not thrown'); + } else { + $this->assertEquals($responseApiMethod['customer']['id'], $result['id']); + } + } catch (ValidatorException $exception) { + $this->assertEquals($throwMessage, $exception->getMessage()); + } + } + + /** @dataProvider dataLoyaltyAccount */ + public function testGetLoyaltyAccount($responseMock, $throwMessage) + { + $this->setResponseMock($responseMock); + $this->setApiMock('getLoyaltyAccountList', $this->responseMock); + + $validator = new WC_Retailcrm_Loyalty_Validator($this->apiMock); + $method = $this->getPrivateMethod('getLoyaltyAccount', $validator); + + try { + $result = $method->invokeArgs($validator, [777]); + + if ($throwMessage) { + $this->fail('ValidatorException was not thrown'); + } else { + $this->assertNotEmpty($result); + } + } catch (ValidatorException $exception) { + $this->assertEquals($throwMessage, $exception->getMessage()); + } + + /* $this->setResponseMock(['success' => true, 'loyaltyAccounts' => []]); + $this->setApiMock('getLoyaltyAccountList', $this->responseMock); + + $validator = new WC_Retailcrm_Loyalty_Validator($this->apiMock); + $method = $this->getPrivateMethod('getLoyaltyAccount', $validator); + + try { + $method->invokeArgs($validator, [777]); + + $this->fail('ValidatorException was not thrown'); + } catch (ValidatorException $exception) { + $this->assertEquals('No active participation in the loyalty program was detected', $exception->getMessage()); + } + + $this->setResponseMock(['success' => true, 'loyaltyAccounts' => [['active' => true, 'amount' => 0, 'level' => ['type' => 'bonus_converting']]]]); + $this->setApiMock('getLoyaltyAccountList', $this->responseMock); + + $validator = new WC_Retailcrm_Loyalty_Validator($this->apiMock); + $method = $this->getPrivateMethod('getLoyaltyAccount', $validator); + + try { + $method->invokeArgs($validator, [777]); + + $this->fail('ValidatorException was not thrown'); + } catch (ValidatorException $exception) { + $this->assertEquals('No bonuses for debiting', $exception->getMessage()); + } + + $this->setResponseMock(['success' => true, 'loyaltyAccounts' => [['active' => true, 'amount' => 0, 'level' => ['type' => 'discount']]]]); + $this->setApiMock('getLoyaltyAccountList', $this->responseMock); + + $validator = new WC_Retailcrm_Loyalty_Validator($this->apiMock); + $method = $this->getPrivateMethod('getLoyaltyAccount', $validator); + $result = $method->invokeArgs($validator, [777]); + + $this->assertNotEmpty($result); + + $this->setResponseMock(['success' => true, 'loyaltyAccounts' => [['active' => true, 'amount' => 100, 'level' => ['type' => 'bonus_converting']]]]); + $this->setApiMock('getLoyaltyAccountList', $this->responseMock); + + $validator = new WC_Retailcrm_Loyalty_Validator($this->apiMock); + $method = $this->getPrivateMethod('getLoyaltyAccount', $validator); + $result = $method->invokeArgs($validator, [777]); + + $this->assertNotEmpty($result);*/ + + } + + + public function dataCheckUser() + { + return [ + [ + 'responseApiMethod' => [], + 'wcUserType' => 'individual', + 'throwMessage' => 'User not found in the system' + ], + [ + 'responseApiMethod' => ['customer' => ['id' => 1]], + 'wcUserType' => 'corp', + 'throwMessage' => 'This user is a corporate person' + ], + [ + 'responseApiMethod' => ['customer' => ['id' => 1]], + 'wcUserType' => 'individual', + 'throwMessage' => null + ], + ]; + } + + public function dataLoyaltyAccount() + { + return [ + [ + 'responseMock' => ['success' => true], + 'throwMessage' => 'Error when searching for participation in loyalty programs' + ], + [ + 'responseMock' => ['success' => true, 'loyaltyAccounts' => []], + 'throwMessage' => 'No active participation in the loyalty program was detected' + ], + [ + 'responseMock' => ['success' => true, 'loyaltyAccounts' => [['active' => true, 'amount' => 0, 'level' => ['type' => 'bonus_converting']]]], + 'throwMessage' => 'No bonuses for debiting' + ], + [ + 'responseMock' => ['success' => true, 'loyaltyAccounts' => [['active' => true, 'amount' => 0, 'level' => ['type' => 'discount']]]], + 'throwMessage' => null + ], + [ + 'responseMock' => ['success' => true, 'loyaltyAccounts' => [['active' => true, 'amount' => 100, 'level' => ['type' => 'bonus_converting']]]], + 'throwMessage' => null + ], + ]; + } + + private function setResponseMock($response = ['success' => true]) + { + $this->responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') + ->disableOriginalConstructor() + ->setMethods(['isSuccessful']) + ->getMock() + ; + + $this->responseMock->setResponse($response); + $this->setMockResponse($this->responseMock, 'isSuccessful', true); + } + + private function setApiMock($method, $response) + { + $this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Client_V5') + ->disableOriginalConstructor() + ->setMethods([$method]) + ->getMock() + ; + $this->setMockResponse($this->apiMock, $method, $response); + } + + private function getPrivateMethod($method, $class) + { + $reflection = new ReflectionClass($class); + $method = $reflection->getMethod($method); + $method->setAccessible(true); + + return $method; + } + + public function tearDown() + { + $this->individualClient->delete(); + $this->corpClient->delete(); + } +}