Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
[zendframework/zendframework#3295] Fixed tests and logic flow
Browse files Browse the repository at this point in the history
- Fixed the unit tests for the unsubscribe functionality. The table
  gateway was expecting the "delete" method now (not "update"), with
  only the "id" member in the array argument; additionally, we needed to
  test the return value, so I added a "will" clause.
- Altered the workflow of Callback to use a switch() statement for the
  "hub_mode"; this will make adding additional cases in the future
  easier, and allows us to raise an exception on an invalid mode.
  • Loading branch information
weierophinney committed Jan 14, 2013
1 parent 9cc0548 commit a7ef12f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
30 changes: 20 additions & 10 deletions src/PubSubHubbub/Subscriber/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Zend\Feed\PubSubHubbub\Subscriber;

use Zend\Feed\PubSubHubbub;
use Zend\Feed\PubSubHubbub\Exception;
use Zend\Uri;

/**
Expand Down Expand Up @@ -95,23 +96,32 @@ public function handle(array $httpGetData = null, $sendResponseNow = false)
} elseif ($this->isValidHubVerification($httpGetData)) {
$this->getHttpResponse()->setContent($httpGetData['hub_challenge']);

if ($httpGetData['hub_mode'] == 'subscribe') {
$data = $this->currentSubscriptionData;
$data['subscription_state'] = PubSubHubbub\PubSubHubbub::SUBSCRIPTION_VERIFIED;
if (isset($httpGetData['hub_lease_seconds'])) {
$data['lease_seconds'] = $httpGetData['hub_lease_seconds'];
}
$this->getStorage()->setSubscription($data);
} else {
$verifyTokenKey = $this->_detectVerifyTokenKey($httpGetData);
$this->getStorage()->deleteSubscription($verifyTokenKey);
switch (strtolower($httpGetData['hub_mode'])) {
case 'subscribe':
$data = $this->currentSubscriptionData;
$data['subscription_state'] = PubSubHubbub\PubSubHubbub::SUBSCRIPTION_VERIFIED;
if (isset($httpGetData['hub_lease_seconds'])) {
$data['lease_seconds'] = $httpGetData['hub_lease_seconds'];
}
$this->getStorage()->setSubscription($data);
break;
case 'unsubscribe':
$verifyTokenKey = $this->_detectVerifyTokenKey($httpGetData);
$this->getStorage()->deleteSubscription($verifyTokenKey);
break;
default:
throw new Exception\RuntimeException(sprintf(
'Invalid hub_mode ("%s") provided',
$httpGetData['hub_mode']
));
}
/**
* Hey, C'mon! We tried everything else!
*/
} else {
$this->getHttpResponse()->setStatusCode(404);
}

if ($sendResponseNow) {
$this->sendResponse();
}
Expand Down
15 changes: 3 additions & 12 deletions test/PubSubHubbub/Subscriber/CallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,21 +280,12 @@ public function testRespondsToValidConfirmationWith200Response()
->will($this->returnValue(1));

$this->_tableGateway->expects($this->once())
->method('update')
->with(
$this->equalTo(array('id' => 'verifytokenkey',
'verify_token' => hash('sha256', 'cba'),
'created_time' => $t->getTimestamp(),
'lease_seconds' => 1234567,
'subscription_state'=> 'to_delete',
'expiration_time' => $t->add(new DateInterval('PT1234567S'))
->format('Y-m-d H:i:s'))),
$this->equalTo(array('id' => 'verifytokenkey'))
);
->method('delete')
->with($this->equalTo(array('id' => 'verifytokenkey')))
->will($this->returnValue(true));

$this->_callback->handle($this->_get);
$this->assertTrue($this->_callback->getHttpResponse()->getStatusCode() == 200);
$this->assertTrue($this->_callback->getStorage()->hasSubscription('verifytokenkey') == false);
}

public function testRespondsToValidConfirmationWithBodyContainingHubChallenge()
Expand Down

0 comments on commit a7ef12f

Please sign in to comment.