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 a5fffe5..9ff9930 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 @@ -24,19 +24,19 @@ if (!class_exists('WC_Retailcrm_Loyalty_Validator')) : public function checkAccount(int $userId) { - $result = false; - try { $crmUser = $this->checkUser($userId); $actualAccount = $this->getLoyaltyAccount($crmUser['id']); $this->checkActiveLoyalty($actualAccount['loyalty']['id']); + + return $actualAccount; + } catch (ValidatorException $exception) { + WC_Admin_Settings::add_error((esc_html__($exception->getMessage(), 'retailcrm')) . "userId: $userId"); } catch (Throwable $exception) { - if ($exception instanceof ValidatorException) { - WC_Admin_Settings::add_error((esc_html__($exception->getMessage(), 'retailcrm')) . "userId: $userId"); - } else { - WC_Admin_Settings::add_error($exception->getMessage()); - } + WC_Admin_Settings::add_error($exception->getMessage()); } + + return false; } /** diff --git a/tests/validators/test-wc-retailcrm-loyalty-validator.php b/tests/validators/test-wc-retailcrm-loyalty-validator.php index 5773da3..0408a9b 100644 --- a/tests/validators/test-wc-retailcrm-loyalty-validator.php +++ b/tests/validators/test-wc-retailcrm-loyalty-validator.php @@ -75,53 +75,28 @@ class WC_Retailcrm_Loyalty_Validator_Test extends WC_Retailcrm_Test_Case_Helper } catch (ValidatorException $exception) { $this->assertEquals($throwMessage, $exception->getMessage()); } + } - /* $this->setResponseMock(['success' => true, 'loyaltyAccounts' => []]); - $this->setApiMock('getLoyaltyAccountList', $this->responseMock); + /** @dataProvider dataCheckActiveLoyalty */ + public function testCheckActivateLoyalty($responseMock, $throwMessage) + { + $this->setResponseMock($responseMock); + $this->setApiMock('getLoyalty', $this->responseMock); $validator = new WC_Retailcrm_Loyalty_Validator($this->apiMock); - $method = $this->getPrivateMethod('getLoyaltyAccount', $validator); + $method = $this->getPrivateMethod('checkActiveLoyalty', $validator); try { - $method->invokeArgs($validator, [777]); + $method->invokeArgs($validator, [1]); - $this->fail('ValidatorException was not thrown'); + if ($throwMessage) { + $this->fail('ValidatorException was not thrown'); + } else { + $this->assertTrue(true); + } } catch (ValidatorException $exception) { - $this->assertEquals('No active participation in the loyalty program was detected', $exception->getMessage()); + $this->assertEquals($throwMessage, $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);*/ - } @@ -172,6 +147,28 @@ class WC_Retailcrm_Loyalty_Validator_Test extends WC_Retailcrm_Test_Case_Helper ]; } + public function dataCheckActiveLoyalty() + { + return [ + [ + 'responseMock' => ['success' => true], + 'throwMessage' => 'Loyalty program not found' + ], + [ + 'responseMock' => ['success' => true, 'loyalty' => ['active' => false]], + 'throwMessage' => 'Loyalty program is not active' + ], + [ + 'responseMock' => ['success' => true, 'loyalty' => ['active' => true, 'blocked' => true]], + 'throwMessage' => 'Loyalty program blocked' + ], + [ + 'responseMock' => ['success' => true, 'loyalty' => ['active' => true, 'blocked' => false]], + 'throwMessage' => null + ] + ]; + } + private function setResponseMock($response = ['success' => true]) { $this->responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper')