Skip to content

Commit

Permalink
Load the results when initing the pager
Browse files Browse the repository at this point in the history
  • Loading branch information
drewish committed Nov 18, 2015
1 parent 5431c47 commit e5bb1ff
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 137 deletions.
6 changes: 3 additions & 3 deletions Tests/Recurly/Account_List_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testGetActive() {

$accounts = Recurly_AccountList::getActive($params, $this->client);
$this->assertInstanceOf('Recurly_AccountList', $accounts);
// $this->assertEquals($url, $accounts->getHref());
$this->assertEquals($url, $accounts->getHref());
}

public function testGetClosed() {
Expand All @@ -35,7 +35,7 @@ public function testGetClosed() {

$accounts = Recurly_AccountList::getClosed($params, $this->client);
$this->assertInstanceOf('Recurly_AccountList', $accounts);
// $this->assertEquals($url, $accounts->getHref());
$this->assertEquals($url, $accounts->getHref());
}

public function testGetPastDue() {
Expand All @@ -45,6 +45,6 @@ public function testGetPastDue() {

$accounts = Recurly_AccountList::getPastDue($params, $this->client);
$this->assertInstanceOf('Recurly_AccountList', $accounts);
// $this->assertEquals($url, $accounts->getHref());
$this->assertEquals($url, $accounts->getHref());
}
}
8 changes: 4 additions & 4 deletions Tests/Recurly/Invoice_List_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function testGetOpen() {

$invoices = Recurly_InvoiceList::getOpen($params, $this->client);
$this->assertInstanceOf('Recurly_InvoiceList', $invoices);
// $this->assertEquals($url, $invoices->getHref());
$this->assertEquals($url, $invoices->getHref());
}

public function testGetCollected() {
Expand All @@ -21,7 +21,7 @@ public function testGetCollected() {

$invoices = Recurly_InvoiceList::getCollected($params, $this->client);
$this->assertInstanceOf('Recurly_InvoiceList', $invoices);
// $this->assertEquals($url, $invoices->getHref());
$this->assertEquals($url, $invoices->getHref());
}

public function testGetFailed() {
Expand All @@ -31,7 +31,7 @@ public function testGetFailed() {

$invoices = Recurly_InvoiceList::getFailed($params, $this->client);
$this->assertInstanceOf('Recurly_InvoiceList', $invoices);
// $this->assertEquals($url, $invoices->getHref());
$this->assertEquals($url, $invoices->getHref());
}

public function testGetPastDue() {
Expand All @@ -41,6 +41,6 @@ public function testGetPastDue() {

$invoices = Recurly_InvoiceList::getPastDue($params, $this->client);
$this->assertInstanceOf('Recurly_InvoiceList', $invoices);
// $this->assertEquals($url, $invoices->getHref());
$this->assertEquals($url, $invoices->getHref());
}
}
12 changes: 0 additions & 12 deletions Tests/Recurly/Pager_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,6 @@ private function assertIteratesCorrectly($pager, $count) {
$this->assertEquals(0, $pager->key());
}

public function testHeadCount() {
// Fires off a HEAD request, looks at the `X-Records` header
$url = '/mocks';
$this->client->addResponse('HEAD', $url, 'pager/head-200.xml');
$pager = new Mock_Pager($url, $this->client);

$this->assertEquals($pager->getHref(), $url);
$this->assertEquals(33, $pager->count());
}

public function testFromHref() {
$url = '/mocks';
$pager = new Mock_Pager($url, $this->client);
Expand All @@ -94,10 +84,8 @@ public function testFromStub() {
// TODO: It seems to load the first page of the results, but doesn't have a
// link to the next page. These tests document the desired behavior but are
// currently broken:
/*
$this->assertEquals($pager->count(), 6, 'Returns correct count');
$this->assertIteratesCorrectly($pager, 6);
*/
}

public function testFromNested() {
Expand Down
14 changes: 7 additions & 7 deletions Tests/Recurly/Subscription_List_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function testGetActive() {

$subscriptions = Recurly_SubscriptionList::getActive($params, $this->client);
$this->assertInstanceOf('Recurly_SubscriptionList', $subscriptions);
// $this->assertEquals($url, $subscriptions->getHref());
$this->assertEquals($url, $subscriptions->getHref());
}

public function testGetCanceled() {
Expand All @@ -22,7 +22,7 @@ public function testGetCanceled() {

$subscriptions = Recurly_SubscriptionList::getCanceled($params, $this->client);
$this->assertInstanceOf('Recurly_SubscriptionList', $subscriptions);
// $this->assertEquals($url, $subscriptions->getHref());
$this->assertEquals($url, $subscriptions->getHref());
}

public function testGetExpired() {
Expand All @@ -32,7 +32,7 @@ public function testGetExpired() {

$subscriptions = Recurly_SubscriptionList::getExpired($params, $this->client);
$this->assertInstanceOf('Recurly_SubscriptionList', $subscriptions);
// $this->assertEquals($url, $subscriptions->getHref());
$this->assertEquals($url, $subscriptions->getHref());
}

public function testGetFuture() {
Expand All @@ -42,7 +42,7 @@ public function testGetFuture() {

$subscriptions = Recurly_SubscriptionList::getFuture($params, $this->client);
$this->assertInstanceOf('Recurly_SubscriptionList', $subscriptions);
// $this->assertEquals($url, $subscriptions->getHref());
$this->assertEquals($url, $subscriptions->getHref());
}

public function testGetLive() {
Expand All @@ -52,7 +52,7 @@ public function testGetLive() {

$subscriptions = Recurly_SubscriptionList::getLive($params, $this->client);
$this->assertInstanceOf('Recurly_SubscriptionList', $subscriptions);
// $this->assertEquals($url, $subscriptions->getHref());
$this->assertEquals($url, $subscriptions->getHref());
}

public function testGetPastDue() {
Expand All @@ -62,7 +62,7 @@ public function testGetPastDue() {

$subscriptions = Recurly_SubscriptionList::getPastDue($params, $this->client);
$this->assertInstanceOf('Recurly_SubscriptionList', $subscriptions);
// $this->assertEquals($url, $subscriptions->getHref());
$this->assertEquals($url, $subscriptions->getHref());
}

public function testGetTrials() {
Expand All @@ -72,6 +72,6 @@ public function testGetTrials() {

$subscriptions = Recurly_SubscriptionList::getTrials($params, $this->client);
$this->assertInstanceOf('Recurly_SubscriptionList', $subscriptions);
// $this->assertEquals($url, $subscriptions->getHref());
$this->assertEquals($url, $subscriptions->getHref());
}
}
4 changes: 2 additions & 2 deletions Tests/Recurly/Transaction_List_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function testGetSuccessful() {

$transactions = Recurly_TransactionList::getSuccessful($params, $this->client);
$this->assertInstanceOf('Recurly_TransactionList', $transactions);
// $this->assertEquals($url, $transactions->getHref());
$this->assertEquals($url, $transactions->getHref());
}

public function testGetVoided() {
Expand All @@ -21,6 +21,6 @@ public function testGetVoided() {

$transactions = Recurly_TransactionList::getVoided($params, $this->client);
$this->assertInstanceOf('Recurly_TransactionList', $transactions);
// $this->assertEquals($url, $transactions->getHref());
$this->assertEquals($url, $transactions->getHref());
}
}
2 changes: 1 addition & 1 deletion Tests/fixtures/notes/index-200.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8
X-Records: 42
X-Records: 2
Link: <https://api.recurly.com/v2/accounts/abcdef1234567890/notes?cursor=1234567890&per_page=20>; rel="start", <https://api.recurly.com/v2/accounts/abcdef1234567890/notes?cursor=1234566890&per_page=20>; rel="next"

<?xml version="1.0" encoding="UTF-8"?>
Expand Down
14 changes: 6 additions & 8 deletions lib/recurly/account_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
class Recurly_AccountList extends Recurly_Pager
{
public static function getActive($params = null, $client = null) {
return Recurly_AccountList::get(Recurly_Pager::_setState($params, 'active'), $client);
return self::get(Recurly_Pager::_setState($params, 'active'), $client);
}

public static function getClosed($params = null, $client = null) {
return Recurly_AccountList::get(Recurly_Pager::_setState($params, 'closed'), $client);
return self::get(Recurly_Pager::_setState($params, 'closed'), $client);
}

public static function getPastDue($params = null, $client = null) {
return Recurly_AccountList::get(Recurly_Pager::_setState($params, 'past_due'), $client);
return self::get(Recurly_Pager::_setState($params, 'past_due'), $client);
}

public static function get($params = null, $client = null)
{
$list = new Recurly_AccountList(Recurly_Client::PATH_ACCOUNTS, $client);
$list->_loadFrom(Recurly_Client::PATH_ACCOUNTS, $params);
return $list;
public static function get($params = null, $client = null) {
$uri = self::_uriWithParams(Recurly_Client::PATH_ACCOUNTS, $params);
return new self($uri, $client);
}

protected function getNodeName() {
Expand Down
5 changes: 2 additions & 3 deletions lib/recurly/addon_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ class Recurly_AddonList extends Recurly_Pager
{
public static function get($planCode, $client = null)
{
$list = new Recurly_AddonList();
$list->_loadFrom(Recurly_Client::PATH_PLANS . '/' . rawurlencode($planCode) . Recurly_Client::PATH_ADDONS, $client);
return $list;
$uri = Recurly_Client::PATH_PLANS . '/' . rawurlencode($planCode) . Recurly_Client::PATH_ADDONS;
return new self($uri, $client);
}

protected function getNodeName() {
Expand Down
8 changes: 3 additions & 5 deletions lib/recurly/adjustment_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

class Recurly_AdjustmentList extends Recurly_Pager
{
public static function get($accountCode, $params = null, $client = null)
{
$list = new Recurly_AdjustmentList(Recurly_Client::PATH_ACCOUNTS . '/' . rawurlencode($accountCode) . Recurly_Client::PATH_ADJUSTMENTS, $client);
$list->_loadFrom(Recurly_Client::PATH_ACCOUNTS . '/' . rawurlencode($accountCode) . Recurly_Client::PATH_ADJUSTMENTS, $params);
return $list;
public static function get($accountCode, $params = null, $client = null) {
$uri = self::_uriWithParams(Recurly_Client::PATH_ACCOUNTS . '/' . rawurlencode($accountCode) . Recurly_Client::PATH_ADJUSTMENTS, $params);
return new self($uri, $client);
}

protected function getNodeName() {
Expand Down
35 changes: 28 additions & 7 deletions lib/recurly/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function _get($uri, $client = null)
}
$response = $client->request(Recurly_Client::GET, $uri);
$response->assertValidResponse();
return Recurly_Base::__parseXmlToNewObject($response->body, $client);
return Recurly_Base::__parseResponseToNewObject($response, $uri, $client);
}

/**
Expand All @@ -41,7 +41,7 @@ protected static function _post($uri, $data = null, $client = null)
}
$response = $client->request(Recurly_Client::POST, $uri, $data);
$response->assertValidResponse();
$object = Recurly_Base::__parseXmlToNewObject($response->body, $client);
$object = Recurly_Base::__parseResponseToNewObject($response, $uri, $client);
$response->assertSuccessResponse($object);
return $object;
}
Expand All @@ -59,7 +59,7 @@ protected static function _put($uri, $client = null)
$response = $client->request(Recurly_Client::PUT, $uri);
$response->assertValidResponse();
if ($response->body) {
$object = Recurly_Base::__parseXmlToNewObject($response->body, $client);
$object = Recurly_Base::__parseResponseToNewObject($response, $uri, $client);
}
$response->assertSuccessResponse($object);
return $object;
Expand All @@ -78,11 +78,24 @@ protected static function _delete($uri, $client = null)
$response = $client->request(Recurly_Client::DELETE, $uri);
$response->assertValidResponse();
if ($response->body) {
return Recurly_Base::__parseXmlToNewObject($response->body, $client);
return Recurly_Base::__parseResponseToNewObject($response, $uri, $client);
}
return null;
}

protected static function _uriWithParams($uri, $params = null) {
if (is_null($params) || !is_array($params)) {
return $uri;
}

$vals = array();
foreach ($params as $k => $v) {
$vals[] = $k . '=' . urlencode($v);
}

return $uri . '?' . implode($vals, '&');
}

/**
* Pretty string version of the object
*/
Expand Down Expand Up @@ -191,19 +204,28 @@ public function getLinks() {
'unit_amount_in_cents' => 'Recurly_CurrencyList',
);

protected static function __parseXmlToNewObject($xml, $client=null) {
// Use a valid Recurly_Response to populate a new object.
protected static function __parseResponseToNewObject($response, $uri, $client) {
$dom = new DOMDocument();
if (empty($xml) || !$dom->loadXML($xml, LIBXML_NOBLANKS)) return null;
if (empty($response->body) || !$dom->loadXML($response->body, LIBXML_NOBLANKS)) {
return null;
}

$rootNode = $dom->documentElement;

$obj = Recurly_Resource::__createNodeObject($rootNode);
$obj->_client = $client;
Recurly_Resource::__parseXmlToObject($rootNode->firstChild, $obj);
if ($obj instanceof self) {
$obj->_afterParseResponse($response, $uri);
}
return $obj;
}

// Optional method to allow objects access to the full response with headers.
protected function _afterParseResponse($response, $uri) { }

// Use the XML to update $this object.
protected function __parseXmlToUpdateObject($xml)
{
$dom = new DOMDocument();
Expand Down Expand Up @@ -286,7 +308,6 @@ protected static function __parseXmlToObject($node, &$object)
}
}
}

} else if ($node->firstChild->nodeType == XML_ELEMENT_NODE) {
// has element children, drop in and continue parsing
$new_obj = Recurly_Resource::__createNodeObject($node);
Expand Down
8 changes: 3 additions & 5 deletions lib/recurly/coupon_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

class Recurly_CouponList extends Recurly_Pager
{
public static function get($params = null, $client = null)
{
$list = new Recurly_CouponList(Recurly_Client::PATH_COUPONS, $client);
$list->_loadFrom(Recurly_Client::PATH_COUPONS, $params);
return $list;
public static function get($params = null, $client = null) {
$uri = self::_uriWithParams(Recurly_Client::PATH_COUPONS, $params);
return new self($uri, $client);
}

protected function getNodeName() {
Expand Down
22 changes: 9 additions & 13 deletions lib/recurly/invoice_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,17 @@ public static function getFailed($params = null, $client = null) {
}

public static function getPastDue($params = null, $client = null) {
return Recurly_InvoiceList::get(Recurly_Pager::_setState($params, 'past_due'), $client);
}
return Recurly_InvoiceList::get(Recurly_Pager::_setState($params, 'past_due'), $client);
}

public static function get($params = null, $client = null)
{
$list = new Recurly_InvoiceList(Recurly_Client::PATH_INVOICES, $client);
$list->_loadFrom(Recurly_Client::PATH_INVOICES, $params);
return $list;
public static function get($params = null, $client = null) {
$uri = self::_uriWithParams(Recurly_Client::PATH_INVOICES, $params);
return new self($uri, $client);
}

public static function getForAccount($accountCode, $params = null, $client = null)
{
$list = new Recurly_InvoiceList(Recurly_Client::PATH_ACCOUNTS . '/' . rawurlencode($accountCode) . Recurly_Client::PATH_INVOICES, $client);
$list->_loadFrom(Recurly_Client::PATH_ACCOUNTS . '/' . rawurlencode($accountCode) . Recurly_Client::PATH_INVOICES, $params);
return $list;

public static function getForAccount($accountCode, $params = null, $client = null) {
$uri = self::_uriWithParams(Recurly_Client::PATH_ACCOUNTS . '/' . rawurlencode($accountCode) . Recurly_Client::PATH_INVOICES, $params);
return new self($uri, $client);
}

protected function getNodeName() {
Expand Down
Loading

0 comments on commit e5bb1ff

Please sign in to comment.