Rename MSS check methods to better distinguish them from Premium ones [PREMIUM-4]

This commit is contained in:
Alexey Stoletniy
2017-05-04 09:36:38 +03:00
parent 0474985866
commit 3cb08e3c09
14 changed files with 38 additions and 38 deletions

View File

@@ -26,7 +26,7 @@ class Services extends APIEndpoint {
} }
try { try {
$result = $this->bridge->checkKey($key); $result = $this->bridge->checkAPIKey($key);
} catch(\Exception $e) { } catch(\Exception $e) {
return $this->errorResponse(array( return $this->errorResponse(array(
$e->getCode() => $e->getMessage() $e->getCode() => $e->getMessage()

View File

@@ -525,7 +525,7 @@ class Menu {
$show_notices = isset($_REQUEST['page']) $show_notices = isset($_REQUEST['page'])
&& stripos($_REQUEST['page'], 'mailpoet-newsletters') === false; && stripos($_REQUEST['page'], 'mailpoet-newsletters') === false;
$checker = $checker ?: new ServicesChecker(); $checker = $checker ?: new ServicesChecker();
$this->mp_api_key_valid = $checker->checkMailPoetAPIKeyValid($show_notices); $this->mp_api_key_valid = $checker->isMailPoetAPIKeyValid($show_notices);
} }
} }

View File

@@ -11,7 +11,7 @@ use MailPoet\WP\Notice as WPNotice;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
class ServicesChecker { class ServicesChecker {
function checkMailPoetAPIKeyValid($display_error_notice = true) { function isMailPoetAPIKeyValid($display_error_notice = true) {
if(!Bridge::isMPSendingServiceEnabled()) { if(!Bridge::isMPSendingServiceEnabled()) {
return null; return null;
} }

View File

@@ -87,7 +87,7 @@ class SendingServiceKeyCheck {
try { try {
$mailer_config = Mailer::getMailerConfig(); $mailer_config = Mailer::getMailerConfig();
$result = $this->bridge->checkKey($mailer_config['mailpoet_api_key']); $result = $this->bridge->checkAPIKey($mailer_config['mailpoet_api_key']);
$this->bridge->updateSubscriberCount($result); $this->bridge->updateSubscriberCount($result);
} catch (\Exception $e) { } catch (\Exception $e) {
$result = false; $result = false;

View File

@@ -22,7 +22,7 @@ class MailPoet {
} }
function send($newsletter, $subscriber, $extra_params = array()) { function send($newsletter, $subscriber, $extra_params = array()) {
if($this->services_checker->checkMailPoetAPIKeyValid() === false) { if($this->services_checker->isMailPoetAPIKeyValid() === false) {
$response = __('MailPoet API key is invalid!', 'mailpoet'); $response = __('MailPoet API key is invalid!', 'mailpoet');
return Mailer::formatMailerSendErrorResult($response); return Mailer::formatMailerSendErrorResult($response);
} }

View File

@@ -54,13 +54,13 @@ class Bridge {
} }
} }
function checkKey($api_key) { function checkAPIKey($api_key) {
$this->initApi($api_key); $this->initApi($api_key);
$result = $this->api->checkKey(); $result = $this->api->checkAPIKey();
return $this->processResult($result); return $this->processAPIKeyCheckResult($result);
} }
function processResult(array $result) { private function processAPIKeyCheckResult(array $result) {
$state_map = array( $state_map = array(
200 => self::MAILPOET_KEY_VALID, 200 => self::MAILPOET_KEY_VALID,
401 => self::MAILPOET_KEY_INVALID, 401 => self::MAILPOET_KEY_INVALID,
@@ -98,7 +98,7 @@ class Bridge {
return $this->processPremiumKeyCheckResult($result); return $this->processPremiumKeyCheckResult($result);
} }
function processPremiumKeyCheckResult(array $result) { private function processPremiumKeyCheckResult(array $result) {
$state_map = array( $state_map = array(
200 => self::PREMIUM_KEY_VALID, 200 => self::PREMIUM_KEY_VALID,
401 => self::PREMIUM_KEY_INVALID, 401 => self::PREMIUM_KEY_INVALID,
@@ -157,7 +157,7 @@ class Bridge {
$api_key_set = !empty($settings[Mailer::MAILER_CONFIG_SETTING_NAME]['mailpoet_api_key']); $api_key_set = !empty($settings[Mailer::MAILER_CONFIG_SETTING_NAME]['mailpoet_api_key']);
$premium_key_set = !empty($settings['premium']['premium_key']); $premium_key_set = !empty($settings['premium']['premium_key']);
if($api_key_set && self::isMPSendingServiceEnabled()) { if($api_key_set && self::isMPSendingServiceEnabled()) {
$result = $this->checkKey($settings[Mailer::MAILER_CONFIG_SETTING_NAME]['mailpoet_api_key']); $result = $this->checkAPIKey($settings[Mailer::MAILER_CONFIG_SETTING_NAME]['mailpoet_api_key']);
$this->updateSubscriberCount($result); $this->updateSubscriberCount($result);
} }
if($premium_key_set) { if($premium_key_set) {

View File

@@ -24,7 +24,7 @@ class API {
$this->setKey($api_key); $this->setKey($api_key);
} }
function checkKey() { function checkAPIKey() {
$result = $this->request( $result = $this->request(
$this->url_me, $this->url_me,
array('site' => home_url()) array('site' => home_url())

View File

@@ -20,7 +20,7 @@ class ServicesTest extends MailPoetTest {
function testItRespondsWithSuccessIfMSSKeyIsValid() { function testItRespondsWithSuccessIfMSSKeyIsValid() {
$this->services_endpoint->bridge = Stub::make( $this->services_endpoint->bridge = Stub::make(
new Bridge(), new Bridge(),
array('checkKey' => array('state' => Bridge::MAILPOET_KEY_VALID)), array('checkAPIKey' => array('state' => Bridge::MAILPOET_KEY_VALID)),
$this $this
); );
$response = $this->services_endpoint->verifyMailPoetKey($this->data); $response = $this->services_endpoint->verifyMailPoetKey($this->data);
@@ -30,7 +30,7 @@ class ServicesTest extends MailPoetTest {
function testItRespondsWithErrorIfMSSKeyIsInvalid() { function testItRespondsWithErrorIfMSSKeyIsInvalid() {
$this->services_endpoint->bridge = Stub::make( $this->services_endpoint->bridge = Stub::make(
new Bridge(), new Bridge(),
array('checkKey' => array('state' => Bridge::MAILPOET_KEY_INVALID)), array('checkAPIKey' => array('state' => Bridge::MAILPOET_KEY_INVALID)),
$this $this
); );
$response = $this->services_endpoint->verifyMailPoetKey($this->data); $response = $this->services_endpoint->verifyMailPoetKey($this->data);
@@ -41,7 +41,7 @@ class ServicesTest extends MailPoetTest {
$date = new DateTime; $date = new DateTime;
$this->services_endpoint->bridge = Stub::make( $this->services_endpoint->bridge = Stub::make(
new Bridge(), new Bridge(),
array('checkKey' => array( array('checkAPIKey' => array(
'state' => Bridge::MAILPOET_KEY_EXPIRING, 'state' => Bridge::MAILPOET_KEY_EXPIRING,
'data' => array('expire_at' => $date->format('c')) 'data' => array('expire_at' => $date->format('c'))
)), )),
@@ -55,7 +55,7 @@ class ServicesTest extends MailPoetTest {
function testItRespondsWithErrorIfServiceIsUnavailableDuringMSSCheck() { function testItRespondsWithErrorIfServiceIsUnavailableDuringMSSCheck() {
$this->services_endpoint->bridge = Stub::make( $this->services_endpoint->bridge = Stub::make(
new Bridge(), new Bridge(),
array('checkKey' => array('code' => Bridge::CHECK_ERROR_UNAVAILABLE)), array('checkAPIKey' => array('code' => Bridge::CHECK_ERROR_UNAVAILABLE)),
$this $this
); );
$response = $this->services_endpoint->verifyMailPoetKey($this->data); $response = $this->services_endpoint->verifyMailPoetKey($this->data);
@@ -66,7 +66,7 @@ class ServicesTest extends MailPoetTest {
function testItRespondsWithErrorIfMSSCheckThrowsAnException() { function testItRespondsWithErrorIfMSSCheckThrowsAnException() {
$this->services_endpoint->bridge = Stub::make( $this->services_endpoint->bridge = Stub::make(
new Bridge(), new Bridge(),
array('checkKey' => function() { throw new \Exception('test'); }), array('checkAPIKey' => function() { throw new \Exception('test'); }),
$this $this
); );
$response = $this->services_endpoint->verifyMailPoetKey($this->data); $response = $this->services_endpoint->verifyMailPoetKey($this->data);

View File

@@ -42,7 +42,7 @@ class MenuTest extends MailPoetTest {
$_REQUEST['page'] = 'mailpoet-newsletters'; $_REQUEST['page'] = 'mailpoet-newsletters';
$checker = Stub::make( $checker = Stub::make(
new ServicesChecker(), new ServicesChecker(),
array('checkMailPoetAPIKeyValid' => true), array('isMailPoetAPIKeyValid' => true),
$this $this
); );
$menu->checkMailPoetAPIKey($checker); $menu->checkMailPoetAPIKey($checker);
@@ -50,7 +50,7 @@ class MenuTest extends MailPoetTest {
$checker = Stub::make( $checker = Stub::make(
new ServicesChecker(), new ServicesChecker(),
array('checkMailPoetAPIKeyValid' => false), array('isMailPoetAPIKeyValid' => false),
$this $this
); );
$menu->checkMailPoetAPIKey($checker); $menu->checkMailPoetAPIKey($checker);

View File

@@ -8,7 +8,7 @@ use MailPoet\Services\Bridge;
class ServicesCheckerTest extends MailPoetTest { class ServicesCheckerTest extends MailPoetTest {
function testItDoesNotCheckMSSKeyIfMPSendingServiceIsDisabled() { function testItDoesNotCheckMSSKeyIfMPSendingServiceIsDisabled() {
$this->disableMailPoetSendingMethod(); $this->disableMailPoetSendingMethod();
$result = ServicesChecker::checkMailPoetAPIKeyValid(); $result = ServicesChecker::isMailPoetAPIKeyValid();
expect($result)->null(); expect($result)->null();
} }
@@ -18,14 +18,14 @@ class ServicesCheckerTest extends MailPoetTest {
Bridge::API_KEY_STATE_SETTING_NAME, Bridge::API_KEY_STATE_SETTING_NAME,
array('state' => Bridge::MAILPOET_KEY_VALID) array('state' => Bridge::MAILPOET_KEY_VALID)
); );
$result = ServicesChecker::checkMailPoetAPIKeyValid(); $result = ServicesChecker::isMailPoetAPIKeyValid();
expect($result)->true(); expect($result)->true();
Setting::setValue( Setting::setValue(
Bridge::API_KEY_STATE_SETTING_NAME, Bridge::API_KEY_STATE_SETTING_NAME,
array('state' => Bridge::MAILPOET_KEY_INVALID) array('state' => Bridge::MAILPOET_KEY_INVALID)
); );
$result = ServicesChecker::checkMailPoetAPIKeyValid(); $result = ServicesChecker::isMailPoetAPIKeyValid();
expect($result)->false(); expect($result)->false();
Setting::setValue( Setting::setValue(
@@ -35,7 +35,7 @@ class ServicesCheckerTest extends MailPoetTest {
'data' => array('expire_at' => date('c')) 'data' => array('expire_at' => date('c'))
) )
); );
$result = ServicesChecker::checkMailPoetAPIKeyValid(); $result = ServicesChecker::isMailPoetAPIKeyValid();
expect($result)->true(); expect($result)->true();
// unexpected state should be treated as valid // unexpected state should be treated as valid
@@ -45,7 +45,7 @@ class ServicesCheckerTest extends MailPoetTest {
'state' => 'unexpected' 'state' => 'unexpected'
) )
); );
$result = ServicesChecker::checkMailPoetAPIKeyValid(); $result = ServicesChecker::isMailPoetAPIKeyValid();
expect($result)->true(); expect($result)->true();
} }

View File

@@ -95,7 +95,7 @@ class SendingServiceKeyCheckTest extends MailPoetTest {
function testItProcesses() { function testItProcesses() {
$this->sskeycheck->bridge = Stub::make( $this->sskeycheck->bridge = Stub::make(
new Bridge, new Bridge,
array('checkKey' => array('code' => Bridge::MAILPOET_KEY_VALID)), array('checkAPIKey' => array('code' => Bridge::MAILPOET_KEY_VALID)),
$this $this
); );
$this->setMailPoetSendingMethod(); $this->setMailPoetSendingMethod();
@@ -113,7 +113,7 @@ class SendingServiceKeyCheckTest extends MailPoetTest {
function testItProcessesSendingServiceKeyCheckQueue() { function testItProcessesSendingServiceKeyCheckQueue() {
$this->sskeycheck->bridge = Stub::make( $this->sskeycheck->bridge = Stub::make(
new Bridge, new Bridge,
array('checkKey' => array('code' => Bridge::MAILPOET_KEY_VALID)), array('checkAPIKey' => array('code' => Bridge::MAILPOET_KEY_VALID)),
$this $this
); );
$this->setMailPoetSendingMethod(); $this->setMailPoetSendingMethod();
@@ -126,7 +126,7 @@ class SendingServiceKeyCheckTest extends MailPoetTest {
function testItReschedulesCheckOnException() { function testItReschedulesCheckOnException() {
$this->sskeycheck->bridge = Stub::make( $this->sskeycheck->bridge = Stub::make(
new Bridge, new Bridge,
array('checkKey' => function () { throw new \Exception(); }), array('checkAPIKey' => function () { throw new \Exception(); }),
$this $this
); );
$this->setMailPoetSendingMethod(); $this->setMailPoetSendingMethod();
@@ -140,7 +140,7 @@ class SendingServiceKeyCheckTest extends MailPoetTest {
function testItReschedulesCheckOnError() { function testItReschedulesCheckOnError() {
$this->sskeycheck->bridge = Stub::make( $this->sskeycheck->bridge = Stub::make(
new Bridge, new Bridge,
array('checkKey' => array('code' => Bridge::CHECK_ERROR_UNAVAILABLE)), array('checkAPIKey' => array('code' => Bridge::CHECK_ERROR_UNAVAILABLE)),
$this $this
); );
$this->setMailPoetSendingMethod(); $this->setMailPoetSendingMethod();

View File

@@ -95,7 +95,7 @@ class MailPoetAPITest extends MailPoetTest {
$this->mailer->api_key = 'someapi'; $this->mailer->api_key = 'someapi';
$this->mailer->services_checker = Stub::make( $this->mailer->services_checker = Stub::make(
new ServicesChecker(), new ServicesChecker(),
array('checkMailPoetAPIKeyValid' => false), array('isMailPoetAPIKeyValid' => false),
$this $this
); );
$result = $this->mailer->send( $result = $this->mailer->send(

View File

@@ -47,24 +47,24 @@ class BridgeTest extends MailPoetTest {
} }
function testItChecksMSSKey() { function testItChecksMSSKey() {
$result = $this->bridge->checkKey($this->valid_key); $result = $this->bridge->checkAPIKey($this->valid_key);
expect($result)->notEmpty(); expect($result)->notEmpty();
expect($result['state'])->equals(Bridge::MAILPOET_KEY_VALID); expect($result['state'])->equals(Bridge::MAILPOET_KEY_VALID);
$result = $this->bridge->checkKey($this->invalid_key); $result = $this->bridge->checkAPIKey($this->invalid_key);
expect($result)->notEmpty(); expect($result)->notEmpty();
expect($result['state'])->equals(Bridge::MAILPOET_KEY_INVALID); expect($result['state'])->equals(Bridge::MAILPOET_KEY_INVALID);
$result = $this->bridge->checkKey($this->expiring_key); $result = $this->bridge->checkAPIKey($this->expiring_key);
expect($result)->notEmpty(); expect($result)->notEmpty();
expect($result['state'])->equals(Bridge::MAILPOET_KEY_EXPIRING); expect($result['state'])->equals(Bridge::MAILPOET_KEY_EXPIRING);
expect($result['data']['expire_at'])->notEmpty(); expect($result['data']['expire_at'])->notEmpty();
} }
function testItReturnsErrorStateOnEmptyAPIResponseCodeDuringMSSCheck() { function testItReturnsErrorStateOnEmptyAPIResponseCodeDuringMSSCheck() {
$api = Stub::make(new API(null), array('checkKey' => array()), $this); $api = Stub::make(new API(null), array('checkAPIKey' => array()), $this);
$this->bridge->api = $api; $this->bridge->api = $api;
$result = $this->bridge->checkKey($this->valid_key); $result = $this->bridge->checkAPIKey($this->valid_key);
expect($result)->notEmpty(); expect($result)->notEmpty();
expect($result['state'])->equals(Bridge::MAILPOET_KEY_CHECK_ERROR); expect($result['state'])->equals(Bridge::MAILPOET_KEY_CHECK_ERROR);
} }
@@ -125,7 +125,7 @@ class BridgeTest extends MailPoetTest {
$api = Stub::make( $api = Stub::make(
new API(null), new API(null),
array( array(
'checkKey' => Stub::once(function() { return array(); }), 'checkAPIKey' => Stub::once(function() { return array(); }),
'checkPremiumKey' => Stub::once(function() { return array(); }) 'checkPremiumKey' => Stub::once(function() { return array(); })
), ),
$this $this

View File

@@ -12,11 +12,11 @@ class MockAPI {
$this->setKey($api_key); $this->setKey($api_key);
} }
function checkKey() { function checkAPIKey() {
// if a key begins with these codes, return them // if a key begins with these codes, return them
$regex = '/^(401|402|503)/'; $regex = '/^(401|402|503)/';
$code = preg_match($regex, $this->api_key, $m) ? $m[1] : 200; $code = preg_match($regex, $this->api_key, $m) ? $m[1] : 200;
return $this->processResponse($code); return $this->processAPICheckResponse($code);
} }
function checkPremiumKey() { function checkPremiumKey() {
@@ -34,7 +34,7 @@ class MockAPI {
$this->api_key = $api_key; $this->api_key = $api_key;
} }
private function processResponse($code) { private function processAPICheckResponse($code) {
switch($code) { switch($code) {
case 200: case 200:
$body = array('subscriber_limit' => 10000); $body = array('subscriber_limit' => 10000);