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

Commit

Permalink
PagedList: Check if uri is valid in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Mar 13, 2016
1 parent b1e1b80 commit e19f3cd
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
6 changes: 0 additions & 6 deletions src/Pinterest/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,12 +548,6 @@ private function buildRequestForPagedList(PagedList $pagedList)
{
$nextItemsUri = $pagedList->getNextUrl();

if (strpos($nextItemsUri, Authentication::BASE_URI) !== 0) {
throw new InvalidArgumentException(
'The paged list has an invalid uri'
);
}

$params = array();
$components = parse_url($nextItemsUri);
parse_str($components['query'], $params);
Expand Down
22 changes: 22 additions & 0 deletions src/Pinterest/Objects/PagedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Pinterest\Objects;

use InvalidArgumentException;
use Pinterest\Authentication;

/**
* This class represents a paged list.
Expand Down Expand Up @@ -45,6 +46,7 @@ final class PagedList
public function __construct(array $items = array(), $nextUrl = null)
{
$this->guardThatTheseAreAllPinterestObjects($items);
$this->assertValidUri($nextUrl);
$this->items = $items;
$this->nextUrl = $nextUrl;
}
Expand Down Expand Up @@ -97,4 +99,24 @@ private function guardThatTheseAreAllPinterestObjects(array $items)
}
}
}

/**
* Checks if the next uri is valid.
*
* @throws InvalidArgumentException
*
* @param $nextUri
*/
private function assertValidUri($nextUri)
{
if ($nextUri === null) {
return;
}

if (strpos($nextUri, Authentication::BASE_URI) === false) {
throw new InvalidArgumentException(
'Not a pinterest api uri'
);
}
}
}
10 changes: 10 additions & 0 deletions tests/Pinterest/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@

namespace Pinterest\Tests;

use InvalidArgumentException;
use Pinterest\Api;
use Pinterest\Authentication;
use Pinterest\Http\BuzzClient;
use Pinterest\Image;
use Pinterest\Objects\PagedList;
use Pinterest\Objects\User;
use RuntimeException;

class ApiTest extends TestCase
Expand Down Expand Up @@ -177,4 +180,11 @@ public function test_it_creates_and_updates_and_deletes_a_board()
$this->assertInstanceOf('Pinterest\Http\Response', $response);
$this->assertTrue($response->ok());
}

public function test_it_cannot_get_more_items_for_an_empty_list()
{
$pagedList = new PagedList(array(), null);
$this->setExpectedException(InvalidArgumentException::class);
$this->api->getNextItems($pagedList);
}
}
48 changes: 48 additions & 0 deletions tests/Pinterest/PagedListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Pinterest\Tests;

use InvalidArgumentException;
use Pinterest\Authentication;
use Pinterest\Objects\PagedList;
use Pinterest\Objects\User;

class PagedListTest extends TestCase
{
public function test_it_only_accepts_pinterest_objects()
{
new PagedList(array(new User()));

$this->setExpectedException(InvalidArgumentException::class);
new PagedList(array(1));

$this->setExpectedException(InvalidArgumentException::class);
new PagedList(array(1, new User()));
}

public function test_it_has_more_items()
{
new PagedList(array(new User()), Authentication::BASE_URI.'/me');

$this->setExpectedException(InvalidArgumentException::class);
new PagedList(array(new User()), 'next-uri');
}

public function test_it_returns_the_next_uri()
{
$uri = Authentication::BASE_URI.'/v1/me';
$pagedList = new PagedList(array(new User()), $uri);
$this->assertTrue($pagedList->hasNext());
$this->assertEquals($uri, $pagedList->getNextUrl());

$pagedList = new PagedList(array(new User()));
$this->assertFalse($pagedList->hasNext());
}

public function test_it_returns_the_items()
{
$items = array(new User(), new User());
$pagedList = new PagedList($items);
$this->assertEquals($items, $pagedList->items());
}
}

0 comments on commit e19f3cd

Please sign in to comment.