From 383346b9996ffa0dfda593506fa18154bfc3c4d0 Mon Sep 17 00:00:00 2001 From: Ramon de la Fuente Date: Mon, 27 May 2013 15:37:04 +0200 Subject: [PATCH 01/20] Allow complete URL in get method The get method always prepends the baseurl before the requests path. A check was added to allow for full URLS (starting with baseurl), for following pagination links. --- lib/Github/HttpClient/HttpClient.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Github/HttpClient/HttpClient.php b/lib/Github/HttpClient/HttpClient.php index ef82a2a0044..6e9e0d913b4 100644 --- a/lib/Github/HttpClient/HttpClient.php +++ b/lib/Github/HttpClient/HttpClient.php @@ -163,7 +163,9 @@ public function put($path, array $parameters = array(), array $headers = array() */ public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array()) { - $path = trim($this->options['base_url'].$path, '/'); + if (0 !== strpos( $path, $this->options['base_url'] )) { + $path = trim($this->options['base_url'].$path, '/'); + } $request = $this->createRequest($httpMethod, $path); $request->addHeaders($headers); From 07138eb97d367c9ac49325e7063e4e21e16ba7b8 Mon Sep 17 00:00:00 2001 From: Ramon de la Fuente Date: Mon, 27 May 2013 15:39:56 +0200 Subject: [PATCH 02/20] Added the perPage property for Github pagination The api client supports the per_page github parameter for api calls. Default is NULL and lets github decide the default (currently 30). $client->setPerPage() --- lib/Github/Api/AbstractApi.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/Github/Api/AbstractApi.php b/lib/Github/Api/AbstractApi.php index be6063d3cfb..c355001947a 100644 --- a/lib/Github/Api/AbstractApi.php +++ b/lib/Github/Api/AbstractApi.php @@ -18,6 +18,13 @@ abstract class AbstractApi implements ApiInterface */ protected $client; + /** + * number of items per page (GitHub pagination) + * + * @var int + */ + protected $perPage = null; + /** * @param Client $client */ @@ -30,11 +37,31 @@ public function configure() { } + /** + * @return int|null + */ + public function getPerPage() + { + return $this->perPage; + } + + /** + * @param Client $client + */ + public function setPerPage($perPage) + { + $this->perPage = (int) $perPage; + return $this; + } + /** * {@inheritDoc} */ protected function get($path, array $parameters = array(), $requestHeaders = array()) { + if (null !== $this->perPage && !isset($parameters['per_page'])) { + $parameters['per_page'] = $this->perPage; + } $response = $this->client->getHttpClient()->get($path, $parameters, $requestHeaders); return $response->getContent(); From 3683986823ed7995d0ff6f20074f821eba1dd050 Mon Sep 17 00:00:00 2001 From: Ramon de la Fuente Date: Mon, 27 May 2013 15:42:09 +0200 Subject: [PATCH 03/20] Added the ResultPaginator (incomplete) This is the first pass at adding a paginator class for github API requests. It only works with fetchAll() at the moment, to demonstrate the usage. Currently: $paginator = new \Github\ResultPaginator( \Github\Client [client] ); $paginator->fetchAll( [api], [method], [parameters] ); Todo's include: Paginator is not complete for all possible headers Next, Last, Previous, First apiClient should be able to return a paginator (i.e. $client->getPaginator() ) CacheHttpClient does not work (does not return Link headers) --- lib/Github/ResultPaginator.php | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 lib/Github/ResultPaginator.php diff --git a/lib/Github/ResultPaginator.php b/lib/Github/ResultPaginator.php new file mode 100644 index 00000000000..180ee864a14 --- /dev/null +++ b/lib/Github/ResultPaginator.php @@ -0,0 +1,57 @@ +client = $client; + } + + public function fetchAll( ApiInterface $api, $method, $parameters ) { + $result = array(); + $result = call_user_func_array( array($api,$method), $parameters); + $this->postFetch(); + + while ($this->hasNext()) { + $result = array_merge($result, $this->fetchNext()); + } + return $result; + } + + protected function postFetch() { + $this->pagination = $this->client->getHttpClient()->getLastResponse()->getPagination(); + var_dump($this->pagination); + } + + public function fetchNext() { + $result = $this->client->getHttpClient()->get($this->pagination['next']); + $this->postFetch(); + return $result->getContent(); + } + + public function hasNext() { + if (!empty($this->pagination) and isset($this->pagination['next'])) { + return true; + } + return false; + } + + public function hasPrevious() { + if (!empty($this->pagination) and isset($this->pagination['previous'])) { + return true; + } + return false; + } + +} From bf811019a29891668cacd481c99a4bf69c7f6ea9 Mon Sep 17 00:00:00 2001 From: Mitchel Verschoof Date: Mon, 27 May 2013 16:28:11 +0200 Subject: [PATCH 04/20] removed debug --- lib/Github/ResultPaginator.php | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/Github/ResultPaginator.php b/lib/Github/ResultPaginator.php index 180ee864a14..1fb37378848 100644 --- a/lib/Github/ResultPaginator.php +++ b/lib/Github/ResultPaginator.php @@ -14,43 +14,54 @@ class ResultPaginator protected $pagination = null; - public function __construct( Client $client ) { + public function __construct( Client $client ) + { $this->client = $client; } - public function fetchAll( ApiInterface $api, $method, $parameters ) { + public function fetchAll( ApiInterface $api, $method, $parameters ) + { $result = array(); - $result = call_user_func_array( array($api,$method), $parameters); + $result = call_user_func_array(array($api, $method), $parameters); + $this->postFetch(); while ($this->hasNext()) { $result = array_merge($result, $this->fetchNext()); } + return $result; } - protected function postFetch() { + protected function postFetch() + { $this->pagination = $this->client->getHttpClient()->getLastResponse()->getPagination(); - var_dump($this->pagination); } - public function fetchNext() { + public function fetchNext() + { $result = $this->client->getHttpClient()->get($this->pagination['next']); + $this->postFetch(); + return $result->getContent(); } - public function hasNext() { + public function hasNext() + { if (!empty($this->pagination) and isset($this->pagination['next'])) { return true; } + return false; } - public function hasPrevious() { + public function hasPrevious() + { if (!empty($this->pagination) and isset($this->pagination['previous'])) { return true; } + return false; } From 09e76f333034267cb3e6800b5bd77c6e8ccb8aa8 Mon Sep 17 00:00:00 2001 From: Ramon de la Fuente Date: Mon, 27 May 2013 15:37:04 +0200 Subject: [PATCH 05/20] Allow complete URL in get method The get method always prepends the baseurl before the requests path. A check was added to allow for full URLS (starting with baseurl), for following pagination links. --- lib/Github/HttpClient/HttpClient.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Github/HttpClient/HttpClient.php b/lib/Github/HttpClient/HttpClient.php index ef82a2a0044..6e9e0d913b4 100644 --- a/lib/Github/HttpClient/HttpClient.php +++ b/lib/Github/HttpClient/HttpClient.php @@ -163,7 +163,9 @@ public function put($path, array $parameters = array(), array $headers = array() */ public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array()) { - $path = trim($this->options['base_url'].$path, '/'); + if (0 !== strpos( $path, $this->options['base_url'] )) { + $path = trim($this->options['base_url'].$path, '/'); + } $request = $this->createRequest($httpMethod, $path); $request->addHeaders($headers); From 52ac68b43047da94ea331549e03b3e3964426788 Mon Sep 17 00:00:00 2001 From: Ramon de la Fuente Date: Mon, 27 May 2013 15:39:56 +0200 Subject: [PATCH 06/20] Added the perPage property for Github pagination The api client supports the per_page github parameter for api calls. Default is NULL and lets github decide the default (currently 30). $client->setPerPage() --- lib/Github/Api/AbstractApi.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/Github/Api/AbstractApi.php b/lib/Github/Api/AbstractApi.php index be6063d3cfb..c355001947a 100644 --- a/lib/Github/Api/AbstractApi.php +++ b/lib/Github/Api/AbstractApi.php @@ -18,6 +18,13 @@ abstract class AbstractApi implements ApiInterface */ protected $client; + /** + * number of items per page (GitHub pagination) + * + * @var int + */ + protected $perPage = null; + /** * @param Client $client */ @@ -30,11 +37,31 @@ public function configure() { } + /** + * @return int|null + */ + public function getPerPage() + { + return $this->perPage; + } + + /** + * @param Client $client + */ + public function setPerPage($perPage) + { + $this->perPage = (int) $perPage; + return $this; + } + /** * {@inheritDoc} */ protected function get($path, array $parameters = array(), $requestHeaders = array()) { + if (null !== $this->perPage && !isset($parameters['per_page'])) { + $parameters['per_page'] = $this->perPage; + } $response = $this->client->getHttpClient()->get($path, $parameters, $requestHeaders); return $response->getContent(); From 578332585150495df11f24e18124af2a97833081 Mon Sep 17 00:00:00 2001 From: Ramon de la Fuente Date: Mon, 27 May 2013 15:42:09 +0200 Subject: [PATCH 07/20] Added the ResultPaginator (incomplete) This is the first pass at adding a paginator class for github API requests. It only works with fetchAll() at the moment, to demonstrate the usage. Currently: $paginator = new \Github\ResultPaginator( \Github\Client [client] ); $paginator->fetchAll( [api], [method], [parameters] ); Todo's include: Paginator is not complete for all possible headers Next, Last, Previous, First apiClient should be able to return a paginator (i.e. $client->getPaginator() ) CacheHttpClient does not work (does not return Link headers) --- lib/Github/ResultPaginator.php | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 lib/Github/ResultPaginator.php diff --git a/lib/Github/ResultPaginator.php b/lib/Github/ResultPaginator.php new file mode 100644 index 00000000000..180ee864a14 --- /dev/null +++ b/lib/Github/ResultPaginator.php @@ -0,0 +1,57 @@ +client = $client; + } + + public function fetchAll( ApiInterface $api, $method, $parameters ) { + $result = array(); + $result = call_user_func_array( array($api,$method), $parameters); + $this->postFetch(); + + while ($this->hasNext()) { + $result = array_merge($result, $this->fetchNext()); + } + return $result; + } + + protected function postFetch() { + $this->pagination = $this->client->getHttpClient()->getLastResponse()->getPagination(); + var_dump($this->pagination); + } + + public function fetchNext() { + $result = $this->client->getHttpClient()->get($this->pagination['next']); + $this->postFetch(); + return $result->getContent(); + } + + public function hasNext() { + if (!empty($this->pagination) and isset($this->pagination['next'])) { + return true; + } + return false; + } + + public function hasPrevious() { + if (!empty($this->pagination) and isset($this->pagination['previous'])) { + return true; + } + return false; + } + +} From 76c1b46be387bd1dedbbc6e5de3a3142a1843107 Mon Sep 17 00:00:00 2001 From: Mitchel Verschoof Date: Mon, 27 May 2013 16:28:11 +0200 Subject: [PATCH 08/20] removed debug --- lib/Github/ResultPaginator.php | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/Github/ResultPaginator.php b/lib/Github/ResultPaginator.php index 180ee864a14..1fb37378848 100644 --- a/lib/Github/ResultPaginator.php +++ b/lib/Github/ResultPaginator.php @@ -14,43 +14,54 @@ class ResultPaginator protected $pagination = null; - public function __construct( Client $client ) { + public function __construct( Client $client ) + { $this->client = $client; } - public function fetchAll( ApiInterface $api, $method, $parameters ) { + public function fetchAll( ApiInterface $api, $method, $parameters ) + { $result = array(); - $result = call_user_func_array( array($api,$method), $parameters); + $result = call_user_func_array(array($api, $method), $parameters); + $this->postFetch(); while ($this->hasNext()) { $result = array_merge($result, $this->fetchNext()); } + return $result; } - protected function postFetch() { + protected function postFetch() + { $this->pagination = $this->client->getHttpClient()->getLastResponse()->getPagination(); - var_dump($this->pagination); } - public function fetchNext() { + public function fetchNext() + { $result = $this->client->getHttpClient()->get($this->pagination['next']); + $this->postFetch(); + return $result->getContent(); } - public function hasNext() { + public function hasNext() + { if (!empty($this->pagination) and isset($this->pagination['next'])) { return true; } + return false; } - public function hasPrevious() { + public function hasPrevious() + { if (!empty($this->pagination) and isset($this->pagination['previous'])) { return true; } + return false; } From 09b147db62bf329d10f48228b036b001be14797f Mon Sep 17 00:00:00 2001 From: Mitchel Verschoof Date: Mon, 8 Jul 2013 15:38:45 +0200 Subject: [PATCH 09/20] Bug Fix path is empty in some test cases but strpos can't handle an empty needle so added check if path is not empty --- lib/Github/HttpClient/HttpClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Github/HttpClient/HttpClient.php b/lib/Github/HttpClient/HttpClient.php index 6e9e0d913b4..93c69cd3a0f 100644 --- a/lib/Github/HttpClient/HttpClient.php +++ b/lib/Github/HttpClient/HttpClient.php @@ -163,7 +163,7 @@ public function put($path, array $parameters = array(), array $headers = array() */ public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array()) { - if (0 !== strpos( $path, $this->options['base_url'] )) { + if ( !empty($this->options['base_url']) AND 0 !== strpos( $path, $this->options['base_url'] )) { $path = trim($this->options['base_url'].$path, '/'); } From 336f50aa9b281506a9d93d5e576abec0a890cf05 Mon Sep 17 00:00:00 2001 From: Ramon de la Fuente Date: Mon, 8 Jul 2013 16:52:30 +0200 Subject: [PATCH 10/20] Renamed paginator to ResultPager, added resultPagerInterface --- lib/Github/ResultPager.php | 150 ++++++++++++++++++++++++++++ lib/Github/ResultPagerInterface.php | 66 ++++++++++++ lib/Github/ResultPaginator.php | 68 ------------- 3 files changed, 216 insertions(+), 68 deletions(-) create mode 100644 lib/Github/ResultPager.php create mode 100644 lib/Github/ResultPagerInterface.php delete mode 100644 lib/Github/ResultPaginator.php diff --git a/lib/Github/ResultPager.php b/lib/Github/ResultPager.php new file mode 100644 index 00000000000..19457d6fa72 --- /dev/null +++ b/lib/Github/ResultPager.php @@ -0,0 +1,150 @@ + + * @author Mitchel Verschoof + */ +class ResultPager implements ResultPagerInterface +{ + /** + * @var Github\Client client + */ + protected $client; + + /** + * @var array pagination + * Comes from pagination headers in Github API results + */ + protected $pagination = null; + + public function __construct( Client $client ) + { + $this->client = $client; + } + + /** + * {@inheritdoc} + */ + public function fetch( ApiInterface $api, $method ) + { + $parameters = array_slice(func_get_args(),2); + + $result = call_user_func_array(array($api, $method), $parameters); + $this->postFetch(); + + return $result; + } + + /** + * {@inheritdoc} + */ + public function fetchAll( ApiInterface $api, $method ) + { + $parameters = array_slice(func_get_args(),2); + + // Set parameters per_page to GitHub max to minimize number of requests + $api->setPerPage(100); + + $result = array(); + $result = call_user_func_array(array($api, $method), $parameters); + $this->postFetch(); + + while ($this->hasNext()) { + $result = array_merge($result, $this->fetchNext()); + } + + return $result; + } + + /** + * {@inheritdoc} + */ + public function postFetch() + { + $this->pagination = $this->client->getHttpClient()->getLastResponse()->getPagination(); + var_dump( $this->pagination ); + } + + /** + * {@inheritdoc} + */ + public function hasNext() + { + return $this->has('next'); + } + + /** + * {@inheritdoc} + */ + public function fetchNext() + { + return $this->get('next'); + } + + /** + * {@inheritdoc} + */ + public function hasPrevious() + { + return $this->has('prev'); + } + + /** + * {@inheritdoc} + */ + public function fetchPrevious() + { + return $this->get('prev'); + } + + /** + * {@inheritdoc} + */ + public function fetchFirst() + { + return $this->get('first'); + } + + /** + * {@inheritdoc} + */ + public function fetchLast() + { + return $this->get('last'); + } + + /** + * {@inheritdoc} + */ + protected function has($key) + { + if (!empty($this->pagination) and isset($this->pagination[$key])) { + return true; + } + + return false; + } + + /** + * {@inheritdoc} + */ + protected function get($key) + { + if ( $this->has($key) ) { + $result = $this->client->getHttpClient()->get($this->pagination[$key]); + $this->postFetch(); + + return $result->getContent(); + } + } + +} diff --git a/lib/Github/ResultPagerInterface.php b/lib/Github/ResultPagerInterface.php new file mode 100644 index 00000000000..4c8fcbd4370 --- /dev/null +++ b/lib/Github/ResultPagerInterface.php @@ -0,0 +1,66 @@ + + * @author Mitchel Verschoof + */ +interface ResultPagerInterface +{ + /** + * Fetch a single result (page) from an api call + */ + public function fetch( ApiInterface $api, $method ); + + /** + * Fetch all results (pages) from an api call + * Use with care - there is no maximum + */ + public function fetchAll( ApiInterface $api, $method ); + + /** + * Method that performs the actual work to refresh the pagination property + */ + public function postFetch(); + + /** + * Check to determine the availability of a next page + * @return bool + */ + public function hasNext(); + + /** + * Check to determine the availability of a previous page + * @return bool + */ + public function hasPrevious(); + + /** + * Fetch the next page + * @return array + */ + public function fetchNext(); + + /** + * Fetch the previous page + * @return array + */ + public function fetchPrevious(); + + /** + * Fetch the first page + * @return array + */ + public function fetchFirst(); + + /** + * Fetch the last page + * @return array + */ + public function fetchLast(); +} diff --git a/lib/Github/ResultPaginator.php b/lib/Github/ResultPaginator.php deleted file mode 100644 index 1fb37378848..00000000000 --- a/lib/Github/ResultPaginator.php +++ /dev/null @@ -1,68 +0,0 @@ -client = $client; - } - - public function fetchAll( ApiInterface $api, $method, $parameters ) - { - $result = array(); - $result = call_user_func_array(array($api, $method), $parameters); - - $this->postFetch(); - - while ($this->hasNext()) { - $result = array_merge($result, $this->fetchNext()); - } - - return $result; - } - - protected function postFetch() - { - $this->pagination = $this->client->getHttpClient()->getLastResponse()->getPagination(); - } - - public function fetchNext() - { - $result = $this->client->getHttpClient()->get($this->pagination['next']); - - $this->postFetch(); - - return $result->getContent(); - } - - public function hasNext() - { - if (!empty($this->pagination) and isset($this->pagination['next'])) { - return true; - } - - return false; - } - - public function hasPrevious() - { - if (!empty($this->pagination) and isset($this->pagination['previous'])) { - return true; - } - - return false; - } - -} From c94daa8775dd2a54a70f9d82187a4bf1a3c371b0 Mon Sep 17 00:00:00 2001 From: Ramon de la Fuente Date: Mon, 8 Jul 2013 17:25:08 +0200 Subject: [PATCH 11/20] code cleanup --- lib/Github/ResultPager.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/Github/ResultPager.php b/lib/Github/ResultPager.php index 19457d6fa72..045b03c49ac 100644 --- a/lib/Github/ResultPager.php +++ b/lib/Github/ResultPager.php @@ -71,7 +71,6 @@ public function fetchAll( ApiInterface $api, $method ) public function postFetch() { $this->pagination = $this->client->getHttpClient()->getLastResponse()->getPagination(); - var_dump( $this->pagination ); } /** From 496bd03fc96ec4e580603d5f21710460194e3b08 Mon Sep 17 00:00:00 2001 From: Mitchel Verschoof Date: Tue, 9 Jul 2013 16:45:24 +0200 Subject: [PATCH 12/20] tmp commit --- test/Github/Tests/Mock/TestResponse.php | 38 +++++++ test/Github/Tests/ResultPagerTest.php | 141 ++++++++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 test/Github/Tests/Mock/TestResponse.php create mode 100644 test/Github/Tests/ResultPagerTest.php diff --git a/test/Github/Tests/Mock/TestResponse.php b/test/Github/Tests/Mock/TestResponse.php new file mode 100644 index 00000000000..aded078657d --- /dev/null +++ b/test/Github/Tests/Mock/TestResponse.php @@ -0,0 +1,38 @@ + + * @author Mitchel Verschoof + */ +class ResultPagerTest extends \PHPUnit_Framework_TestCase +{ + /** + * @test + * + * description fetchAll + */ + public function shouldGetAllResults() + { + $organizationMockApi = $this->getApiMock( 'Github\Api\Organization' ); + $method = 'all'; + $parameters = array('netwerven'); + + // $paginator = new Github\ResultPaginator( $client ); + // $result = $paginator->fetchAll( $organizationMockApi, 'repositories', $parameters ); + } + + /** + * @test + * + * description postFetch + */ + public function postFetch() + { + + } + + /** + * @test + * + * description fetchNext + */ + public function fetchNext() + { + + } + + /** + * @test + * + * description hasNext + */ + public function shouldHasNext() + { + + } + + /** + * @test + * + * description hasPrevious + */ + public function shouldHasPrevious() + { + + } + + /** + * @test + * + * description first + */ + public function shouldHasFirst() + { + + } + + /** + * @test + * + * description last + */ + public function shouldHasLast() + { + + } + + protected function getApiMock( $apiClass ) + { + $responseStub = $this->getMock('Github\HttpClient\Message\Response', array('getPagination')); + $responseStub + ->expects($this->any()) + ->method('getPagination') + ->with(array('test' => 'test')); + + var_dump( "\n" ); + var_dump( $responseStub ); + exit; + + $httpClient = $this->getMock('Buzz\Client\ClientInterface', array('setTimeout', 'setVerifyPeer', 'send', 'getLastResponse')); + $httpClient + ->expects($this->any()) + ->method('setTimeout') + ->with(10); + $httpClient + ->expects($this->any()) + ->method('setVerifyPeer') + ->with(false); + $httpClient + ->expects($this->any()) + ->method('send'); + $httpClient + ->expects($this->any()) + ->method('getLastResponse') + ->with(array( + 'first' => 'test', + 'next' => 'test', + 'previous' => 'test', + 'last' => 'test', + )); + + $mock = $this->getMock('Github\HttpClient\HttpClient', array(), array(array(), $httpClient)); + + var_dump( $mock->getLastResponse(), $mock ); + + $client = new \Github\Client($mock); + $client->setHttpClient($mock); + + var_dump( $client->getHttpClient()->getLastResponse() ); + + return $this->getMockBuilder( $apiClass ) + ->setMethods(array('get', 'post', 'patch', 'delete', 'put')) + ->setConstructorArgs(array($client)) + ->getMock(); + } + +} \ No newline at end of file From 84b76eb0dc351339a93386e25a82ac1dd328bf86 Mon Sep 17 00:00:00 2001 From: Mitchel Verschoof Date: Thu, 11 Jul 2013 15:17:40 +0200 Subject: [PATCH 13/20] Added function getPagination so you have access to the pagination array --- lib/Github/ResultPager.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/Github/ResultPager.php b/lib/Github/ResultPager.php index 045b03c49ac..7530f1c4a17 100644 --- a/lib/Github/ResultPager.php +++ b/lib/Github/ResultPager.php @@ -31,6 +31,14 @@ public function __construct( Client $client ) $this->client = $client; } + /** + * @return null|array pagination result of last request + */ + public function getPagination() + { + return $this->pagination; + } + /** * {@inheritdoc} */ From 5541dec015e4bae8ca7a60a29a617554b92fdb30 Mon Sep 17 00:00:00 2001 From: Mitchel Verschoof Date: Thu, 11 Jul 2013 15:32:04 +0200 Subject: [PATCH 14/20] Added TestResponse Mock to test the FetchAll Added UnitTest --- test/Github/Tests/Mock/TestResponse.php | 40 +++-- test/Github/Tests/ResultPagerTest.php | 210 ++++++++++++++++++------ 2 files changed, 183 insertions(+), 67 deletions(-) diff --git a/test/Github/Tests/Mock/TestResponse.php b/test/Github/Tests/Mock/TestResponse.php index aded078657d..0c8f04b320e 100644 --- a/test/Github/Tests/Mock/TestResponse.php +++ b/test/Github/Tests/Mock/TestResponse.php @@ -2,22 +2,24 @@ namespace Github\Tests\Mock; -use Buzz\Message\Response as BaseResponse; -use Github\Exception\ApiLimitExceedException; - -class TestResponse extends BaseResponse +class TestResponse { - /** - * @var integer - */ - public $remainingCalls; + protected $loopCount; + + protected $content; + + public function __construct( $loopCount, array $content = array() ) + { + $this->loopCount = $loopCount; + $this->content = $content; + } /** * {@inheritDoc} */ public function getContent() { - return null; + return $this->content; } /** @@ -25,14 +27,18 @@ public function getContent() */ public function getPagination() { - return null; - } + if($this->loopCount){ + $returnArray = array( + 'next' => 'http://github.com/' . $this->loopCount + ); + } else { + $returnArray = array( + 'prev' => 'http://github.com/prev' + ); + } - /** - * {@inheritDoc} - */ - public function getApiLimit() - { - return null; + $this->loopCount--; + + return $returnArray; } } diff --git a/test/Github/Tests/ResultPagerTest.php b/test/Github/Tests/ResultPagerTest.php index 095989e65d6..d1551767f56 100644 --- a/test/Github/Tests/ResultPagerTest.php +++ b/test/Github/Tests/ResultPagerTest.php @@ -5,6 +5,8 @@ use Github; use Github\Client; use Github\ResultPager; +use Github\HttpClient\HttpClientInterface; +use Github\Tests\Mock\TestResponse; /** * ResultPagerTest @@ -21,12 +23,63 @@ class ResultPagerTest extends \PHPUnit_Framework_TestCase */ public function shouldGetAllResults() { - $organizationMockApi = $this->getApiMock( 'Github\Api\Organization' ); - $method = 'all'; - $parameters = array('netwerven'); + $amountLoops = 3; + $content = array(1,2,3,4,5,6,7,8,9,10); + $responseMock = new TestResponse( $amountLoops, $content ); + + // httpClient mock + $httpClientMock = $this->getHttpClientMock($responseMock); + $httpClientMock + ->expects($this->exactly($amountLoops)) + ->method('get') + ->will($this->returnValue($responseMock)); + + $clientMock = $this->getClientMock($httpClientMock); + + // memberApi Mock + $memberApiMock = $this->getApiMock( 'Github\Api\Organization\Members' ); + $memberApiMock + ->expects($this->once()) + ->method('all') + ->will($this->returnValue(array())); + + $method = 'all'; + $parameters = array('netwerven'); + + // Run fetchAll on result paginator + $paginator = new Github\ResultPager( $clientMock ); + $result = $paginator->fetchAll( $memberApiMock, $method, $parameters ); + + $this->assertEquals($amountLoops * count($content), count($result)); + } + + /** + * @test + * + * description fetch + */ + public function shouldGetSomeResults() + { + $pagination = array('next' => 'http://github.com/next'); + $resultContent = 'organization test'; - // $paginator = new Github\ResultPaginator( $client ); - // $result = $paginator->fetchAll( $organizationMockApi, 'repositories', $parameters ); + $responseMock = $this->getResponseMock($pagination); + $httpClient = $this->getHttpClientMock( $responseMock ); + $client = $this->getClientMock($httpClient); + + $organizationApiMock = $this->getApiMock( 'Github\Api\Organization' ); + + $organizationApiMock + ->expects($this->once()) + ->method('show') + ->with('github') + ->will($this->returnValue($resultContent)); + + $paginator = new Github\ResultPager( $client ); + $result = $paginator->fetch($organizationApiMock, 'show', 'github'); + + $this->assertEquals($resultContent, $result); + $this->assertEquals($pagination, $paginator->getPagination()); } /** @@ -36,7 +89,27 @@ public function shouldGetAllResults() */ public function postFetch() { + $pagination = array( + 'first' => 'http://github.com', + 'next' => 'http://github.com', + 'prev' => 'http://github.com', + 'last' => 'http://github.com' + ); + + // response mock + $responseMock = $this->getMock('Github\HttpClient\Message\Response'); + $responseMock + ->expects($this->any()) + ->method('getPagination') + ->will($this->returnValue($pagination)); + $httpClient = $this->getHttpClientMock( $responseMock ); + $client = $this->getClientMock($httpClient); + + $paginator = new Github\ResultPager( $client ); + $paginator->postFetch(); + + $this->assertEquals($paginator->getPagination(), $pagination); } /** @@ -46,7 +119,33 @@ public function postFetch() */ public function fetchNext() { + $pagination = array('next' => 'http://github.com/next'); + $resultContent = 'fetch test'; + + $responseMock = $this->getResponseMock( $pagination ); + $responseMock + ->expects($this->once()) + ->method('getContent') + ->will($this->returnValue($resultContent)); + // Expected 2 times, 1 for setup and 1 for the actual test + $responseMock + ->expects($this->exactly(2)) + ->method('getPagination'); + + $httpClient = $this->getHttpClientMock( $responseMock ); + + $httpClient + ->expects($this->once()) + ->method('get') + ->with( $pagination['next'] ) + ->will($this->returnValue($responseMock)); + $client = $this->getClientMock($httpClient); + + $paginator = new Github\ResultPager( $client ); + $paginator->postFetch(); + + $this->assertEquals($paginator->fetchNext(), $resultContent); } /** @@ -54,9 +153,17 @@ public function fetchNext() * * description hasNext */ - public function shouldHasNext() + public function shouldHaveNext() { + $responseMock = $this->getResponseMock(array('next' => 'http://github.com/next')); + $httpClient = $this->getHttpClientMock( $responseMock ); + $client = $this->getClientMock($httpClient); + + $paginator = new Github\ResultPager( $client ); + $paginator->postFetch(); + $this->assertEquals($paginator->hasNext(), true); + $this->assertEquals($paginator->hasPrevious(), false); } /** @@ -64,78 +171,81 @@ public function shouldHasNext() * * description hasPrevious */ - public function shouldHasPrevious() + public function shouldHavePrevious() { + $responseMock = $this->getResponseMock(array('prev' => 'http://github.com/previous')); + $httpClient = $this->getHttpClientMock( $responseMock ); + $client = $this->getClientMock($httpClient); + $paginator = new Github\ResultPager( $client ); + $paginator->postFetch(); + + $this->assertEquals($paginator->hasPrevious(), true); + $this->assertEquals($paginator->hasNext(), false); } - /** - * @test - * - * description first - */ - public function shouldHasFirst() + protected function getResponseMock( array $pagination ) { + // response mock + $responseMock = $this->getMock('Github\HttpClient\Message\Response'); + $responseMock + ->expects($this->any()) + ->method('getPagination') + ->will($this->returnValue( + $pagination + )); + return $responseMock; } - /** - * @test - * - * description last - */ - public function shouldHasLast() + protected function getClientMock( HttpClientInterface $httpClient = null ) { + // if no httpClient isset use the default HttpClient mock + if( !$httpClient ){ + $httpClient = $this->getHttpClientMock(); + } + + $client = new \Github\Client($httpClient); + $client->setHttpClient($httpClient); + return $client; } - protected function getApiMock( $apiClass ) + protected function getHttpClientMock( $responseMock = null ) { - $responseStub = $this->getMock('Github\HttpClient\Message\Response', array('getPagination')); - $responseStub - ->expects($this->any()) - ->method('getPagination') - ->with(array('test' => 'test')); - - var_dump( "\n" ); - var_dump( $responseStub ); - exit; - - $httpClient = $this->getMock('Buzz\Client\ClientInterface', array('setTimeout', 'setVerifyPeer', 'send', 'getLastResponse')); - $httpClient + // mock the client interface + $clientInterfaceMock = $this->getMock('Buzz\Client\ClientInterface', array('setTimeout', 'setVerifyPeer', 'send')); + $clientInterfaceMock ->expects($this->any()) ->method('setTimeout') ->with(10); - $httpClient + $clientInterfaceMock ->expects($this->any()) ->method('setVerifyPeer') ->with(false); - $httpClient + $clientInterfaceMock ->expects($this->any()) ->method('send'); - $httpClient - ->expects($this->any()) - ->method('getLastResponse') - ->with(array( - 'first' => 'test', - 'next' => 'test', - 'previous' => 'test', - 'last' => 'test', - )); - $mock = $this->getMock('Github\HttpClient\HttpClient', array(), array(array(), $httpClient)); + // create the httpClient mock + $httpClientMock = $this->getMock('Github\HttpClient\HttpClient', array(), array(array(), $clientInterfaceMock)); - var_dump( $mock->getLastResponse(), $mock ); + if( $responseMock ){ + $httpClientMock + ->expects($this->any()) + ->method('getLastResponse') + ->will($this->returnValue($responseMock)); + } - $client = new \Github\Client($mock); - $client->setHttpClient($mock); + return $httpClientMock; + } - var_dump( $client->getHttpClient()->getLastResponse() ); + protected function getApiMock( $apiClass ) + { + $client = $this->getClientMock(); return $this->getMockBuilder( $apiClass ) - ->setMethods(array('get', 'post', 'patch', 'delete', 'put')) ->setConstructorArgs(array($client)) ->getMock(); } - } \ No newline at end of file From 3e0ffa291a8bda3a078876faeeb8172b4a80bb4e Mon Sep 17 00:00:00 2001 From: Mitchel Verschoof Date: Tue, 9 Jul 2013 08:54:47 +0200 Subject: [PATCH 15/20] Added documentation file --- doc/index.md | 2 ++ doc/result_pager.md | 4 ++++ 2 files changed, 6 insertions(+) create mode 100644 doc/result_pager.md diff --git a/doc/index.md b/doc/index.md index 648d2cb870c..c013e13aa54 100644 --- a/doc/index.md +++ b/doc/index.md @@ -16,6 +16,8 @@ APIs: * [Repositories](repos.md) * [Users](users.md) +* [Result Pager](result_pager.md) + Additional features: * [Authentication & Security](security.md) diff --git a/doc/result_pager.md b/doc/result_pager.md new file mode 100644 index 00000000000..16ebd9db6f5 --- /dev/null +++ b/doc/result_pager.md @@ -0,0 +1,4 @@ +## Resultpager +[Back to the navigation](../index.md) + +To be written... From 74210a4c30e954ac28db2ef694d296cfb65fee83 Mon Sep 17 00:00:00 2001 From: Mitchel Verschoof Date: Tue, 9 Jul 2013 16:46:41 +0200 Subject: [PATCH 16/20] Updated result_pager.md --- doc/result_pager.md | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/doc/result_pager.md b/doc/result_pager.md index 16ebd9db6f5..0e7eeb43336 100644 --- a/doc/result_pager.md +++ b/doc/result_pager.md @@ -1,4 +1,15 @@ -## Resultpager -[Back to the navigation](../index.md) +## Result Pager +[Back to the navigation](index.md) -To be written... +### Usage examples + +Get all results of a organization + +```php +$client = new Github\Client(); + +$organizationApi = $client->api('organization'); + +$paginator = new Github\ResultPager( $client ); +$result = $paginator->fetchAll( $organizationApi, 'repositories', 'future500' ); +``` From 7fd1cb1e2eae7ecad4d2258820dba6f604a8c57d Mon Sep 17 00:00:00 2001 From: Mitchel Verschoof Date: Thu, 11 Jul 2013 15:41:14 +0200 Subject: [PATCH 17/20] Added documentation for the pagination support --- doc/index.md | 3 +-- doc/result_pager.md | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/doc/index.md b/doc/index.md index c013e13aa54..5a98737f912 100644 --- a/doc/index.md +++ b/doc/index.md @@ -16,10 +16,9 @@ APIs: * [Repositories](repos.md) * [Users](users.md) -* [Result Pager](result_pager.md) - Additional features: +* [Pagination support](result_pager.md) * [Authentication & Security](security.md) * [Request any Route](request_any_route.md) * [Customize `php-github-api` and testing](customize.md) diff --git a/doc/result_pager.md b/doc/result_pager.md index 0e7eeb43336..c18a9f2923b 100644 --- a/doc/result_pager.md +++ b/doc/result_pager.md @@ -3,7 +3,7 @@ ### Usage examples -Get all results of a organization +Get all repositories of a organization ```php $client = new Github\Client(); @@ -11,5 +11,34 @@ $client = new Github\Client(); $organizationApi = $client->api('organization'); $paginator = new Github\ResultPager( $client ); -$result = $paginator->fetchAll( $organizationApi, 'repositories', 'future500' ); +$result = $paginator->fetchAll( $organizationApi, 'repositories', 'github ); ``` + +Get the first page +```php +$client = new Github\Client(); + +$organizationApi = $client->api('organization'); + +$paginator = new Github\ResultPager( $client ); +$result = $paginator->fetch( $organizationApi, 'repositories', 'github ); +``` +Check for a next page: +```php +$paginator->hasNext(); +``` + +Get next page: +```php +$paginator->fetchNext(); +``` + +Check for pervious page: +```php +$paginator->getPrevious(); +``` + +Get prevrious page: +```php +$paginator->fetchPrevious(); +``` \ No newline at end of file From 99ed0c73ea1f18ee3c03ad525b4aa59dbe8a0bac Mon Sep 17 00:00:00 2001 From: Mitchel Verschoof Date: Fri, 12 Jul 2013 08:56:29 +0200 Subject: [PATCH 18/20] Fixed documentation --- doc/result_pager.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/result_pager.md b/doc/result_pager.md index c18a9f2923b..3c9e24493b5 100644 --- a/doc/result_pager.md +++ b/doc/result_pager.md @@ -11,7 +11,7 @@ $client = new Github\Client(); $organizationApi = $client->api('organization'); $paginator = new Github\ResultPager( $client ); -$result = $paginator->fetchAll( $organizationApi, 'repositories', 'github ); +$result = $paginator->fetchAll( $organizationApi, 'repositories', 'github' ); ``` Get the first page @@ -21,7 +21,7 @@ $client = new Github\Client(); $organizationApi = $client->api('organization'); $paginator = new Github\ResultPager( $client ); -$result = $paginator->fetch( $organizationApi, 'repositories', 'github ); +$result = $paginator->fetch( $organizationApi, 'repositories', 'github' ); ``` Check for a next page: ```php @@ -35,7 +35,7 @@ $paginator->fetchNext(); Check for pervious page: ```php -$paginator->getPrevious(); +$paginator->hasPrevious(); ``` Get prevrious page: From c8f5738541f3fd67c7225f31c0b139b6abec3d01 Mon Sep 17 00:00:00 2001 From: Mitchel Verschoof Date: Fri, 12 Jul 2013 10:49:01 +0200 Subject: [PATCH 19/20] fixed CS Updated coding standard to PSR-2 fixed CS issues and typos updated docs changed fetch() and fetchAll() signature to an array of parameters for flexibility in the future --- doc/result_pager.md | 18 ++++++--- lib/Github/Api/AbstractApi.php | 10 ++--- lib/Github/HttpClient/HttpClient.php | 2 +- lib/Github/ResultPager.php | 39 ++++++++++++------- lib/Github/ResultPagerInterface.php | 22 ++++++++++- test/Github/Tests/ResultPagerTest.php | 56 +++++++++++++-------------- 6 files changed, 90 insertions(+), 57 deletions(-) diff --git a/doc/result_pager.md b/doc/result_pager.md index 3c9e24493b5..a802be1caa9 100644 --- a/doc/result_pager.md +++ b/doc/result_pager.md @@ -10,8 +10,9 @@ $client = new Github\Client(); $organizationApi = $client->api('organization'); -$paginator = new Github\ResultPager( $client ); -$result = $paginator->fetchAll( $organizationApi, 'repositories', 'github' ); +$paginator = new Github\ResultPager($client); +$parameters = array('github'); +$result = $paginator->fetchAll($organizationApi, 'repositories', $parameters); ``` Get the first page @@ -20,9 +21,11 @@ $client = new Github\Client(); $organizationApi = $client->api('organization'); -$paginator = new Github\ResultPager( $client ); -$result = $paginator->fetch( $organizationApi, 'repositories', 'github' ); +$paginator = new Github\ResultPager( $client ); +$parameters = array('github'); +$result = $paginator->fetch($organizationApi, 'repositories', $parameters); ``` + Check for a next page: ```php $paginator->hasNext(); @@ -41,4 +44,9 @@ $paginator->hasPrevious(); Get prevrious page: ```php $paginator->fetchPrevious(); -``` \ No newline at end of file +``` + +If you want to retrieve the pagination links (available after the call to fetch): +```php +$paginator->getPagination(); +``` diff --git a/lib/Github/Api/AbstractApi.php b/lib/Github/Api/AbstractApi.php index c355001947a..378b3414d71 100644 --- a/lib/Github/Api/AbstractApi.php +++ b/lib/Github/Api/AbstractApi.php @@ -21,9 +21,9 @@ abstract class AbstractApi implements ApiInterface /** * number of items per page (GitHub pagination) * - * @var int + * @var null|int */ - protected $perPage = null; + protected $perPage; /** * @param Client $client @@ -38,7 +38,7 @@ public function configure() } /** - * @return int|null + * @return null|int */ public function getPerPage() { @@ -46,11 +46,11 @@ public function getPerPage() } /** - * @param Client $client + * @param null|int $perPage */ public function setPerPage($perPage) { - $this->perPage = (int) $perPage; + $this->perPage = (null === $perPage ? $perPage : (int) $perPage); return $this; } diff --git a/lib/Github/HttpClient/HttpClient.php b/lib/Github/HttpClient/HttpClient.php index 93c69cd3a0f..92467bb1e91 100644 --- a/lib/Github/HttpClient/HttpClient.php +++ b/lib/Github/HttpClient/HttpClient.php @@ -163,7 +163,7 @@ public function put($path, array $parameters = array(), array $headers = array() */ public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array()) { - if ( !empty($this->options['base_url']) AND 0 !== strpos( $path, $this->options['base_url'] )) { + if (!empty($this->options['base_url']) && 0 !== strpos($path, $this->options['base_url'])) { $path = trim($this->options['base_url'].$path, '/'); } diff --git a/lib/Github/ResultPager.php b/lib/Github/ResultPager.php index 7530f1c4a17..0ce7d11f595 100644 --- a/lib/Github/ResultPager.php +++ b/lib/Github/ResultPager.php @@ -24,15 +24,27 @@ class ResultPager implements ResultPagerInterface * @var array pagination * Comes from pagination headers in Github API results */ - protected $pagination = null; + protected $pagination; - public function __construct( Client $client ) + + /** + * The Github client to use for pagination. This must be the same + * instance that you got the Api instance from, i.e.: + * + * $client = new \Github\Client(); + * $api = $client->api('someApi'); + * $pager = new \Github\ResultPager($client); + * + * @param \Github\Client $client + * + */ + public function __construct(Client $client) { $this->client = $client; } /** - * @return null|array pagination result of last request + * {@inheritdoc} */ public function getPagination() { @@ -42,10 +54,8 @@ public function getPagination() /** * {@inheritdoc} */ - public function fetch( ApiInterface $api, $method ) + public function fetch(ApiInterface $api, $method, array $parameters = array()) { - $parameters = array_slice(func_get_args(),2); - $result = call_user_func_array(array($api, $method), $parameters); $this->postFetch(); @@ -55,9 +65,10 @@ public function fetch( ApiInterface $api, $method ) /** * {@inheritdoc} */ - public function fetchAll( ApiInterface $api, $method ) + public function fetchAll(ApiInterface $api, $method, array $parameters = array()) { - $parameters = array_slice(func_get_args(),2); + // get the perPage from the api + $perPage = $api->getPerPage(); // Set parameters per_page to GitHub max to minimize number of requests $api->setPerPage(100); @@ -70,6 +81,9 @@ public function fetchAll( ApiInterface $api, $method ) $result = array_merge($result, $this->fetchNext()); } + // restore the perPage + $api->setPerPage($perPage); + return $result; } @@ -134,11 +148,7 @@ public function fetchLast() */ protected function has($key) { - if (!empty($this->pagination) and isset($this->pagination[$key])) { - return true; - } - - return false; + return !empty($this->pagination) && isset($this->pagination[$key]); } /** @@ -146,12 +156,11 @@ protected function has($key) */ protected function get($key) { - if ( $this->has($key) ) { + if ($this->has($key)) { $result = $this->client->getHttpClient()->get($this->pagination[$key]); $this->postFetch(); return $result->getContent(); } } - } diff --git a/lib/Github/ResultPagerInterface.php b/lib/Github/ResultPagerInterface.php index 4c8fcbd4370..6faab5efda2 100644 --- a/lib/Github/ResultPagerInterface.php +++ b/lib/Github/ResultPagerInterface.php @@ -12,16 +12,34 @@ */ interface ResultPagerInterface { + + /** + * @return null|array pagination result of last request + */ + public function getPagination(); + /** * Fetch a single result (page) from an api call + * + * @param ApiInterface $api the Api instance + * @param string $method the method name to call on the Api instance + * @param array $parameters the method parameters in an array + * + * @return array returns the result of the Api::$method() call */ - public function fetch( ApiInterface $api, $method ); + public function fetch(ApiInterface $api, $method, array $parameters = array()); /** * Fetch all results (pages) from an api call * Use with care - there is no maximum + * + * @param ApiInterface $api the Api instance + * @param string $method the method name to call on the Api instance + * @param array $parameters the method parameters in an array + * + * @return array returns a merge of the results of the Api::$method() call */ - public function fetchAll( ApiInterface $api, $method ); + public function fetchAll(ApiInterface $api, $method, array $parameters = array()); /** * Method that performs the actual work to refresh the pagination property diff --git a/test/Github/Tests/ResultPagerTest.php b/test/Github/Tests/ResultPagerTest.php index d1551767f56..df862c1d50a 100644 --- a/test/Github/Tests/ResultPagerTest.php +++ b/test/Github/Tests/ResultPagerTest.php @@ -25,7 +25,7 @@ public function shouldGetAllResults() { $amountLoops = 3; $content = array(1,2,3,4,5,6,7,8,9,10); - $responseMock = new TestResponse( $amountLoops, $content ); + $responseMock = new TestResponse($amountLoops, $content); // httpClient mock $httpClientMock = $this->getHttpClientMock($responseMock); @@ -37,7 +37,7 @@ public function shouldGetAllResults() $clientMock = $this->getClientMock($httpClientMock); // memberApi Mock - $memberApiMock = $this->getApiMock( 'Github\Api\Organization\Members' ); + $memberApiMock = $this->getApiMock('Github\Api\Organization\Members'); $memberApiMock ->expects($this->once()) ->method('all') @@ -47,8 +47,8 @@ public function shouldGetAllResults() $parameters = array('netwerven'); // Run fetchAll on result paginator - $paginator = new Github\ResultPager( $clientMock ); - $result = $paginator->fetchAll( $memberApiMock, $method, $parameters ); + $paginator = new Github\ResultPager($clientMock); + $result = $paginator->fetchAll($memberApiMock, $method, $parameters); $this->assertEquals($amountLoops * count($content), count($result)); } @@ -64,10 +64,10 @@ public function shouldGetSomeResults() $resultContent = 'organization test'; $responseMock = $this->getResponseMock($pagination); - $httpClient = $this->getHttpClientMock( $responseMock ); + $httpClient = $this->getHttpClientMock($responseMock); $client = $this->getClientMock($httpClient); - $organizationApiMock = $this->getApiMock( 'Github\Api\Organization' ); + $organizationApiMock = $this->getApiMock('Github\Api\Organization'); $organizationApiMock ->expects($this->once()) @@ -75,8 +75,8 @@ public function shouldGetSomeResults() ->with('github') ->will($this->returnValue($resultContent)); - $paginator = new Github\ResultPager( $client ); - $result = $paginator->fetch($organizationApiMock, 'show', 'github'); + $paginator = new Github\ResultPager($client); + $result = $paginator->fetch($organizationApiMock, 'show', array('github')); $this->assertEquals($resultContent, $result); $this->assertEquals($pagination, $paginator->getPagination()); @@ -103,10 +103,10 @@ public function postFetch() ->method('getPagination') ->will($this->returnValue($pagination)); - $httpClient = $this->getHttpClientMock( $responseMock ); + $httpClient = $this->getHttpClientMock($responseMock); $client = $this->getClientMock($httpClient); - $paginator = new Github\ResultPager( $client ); + $paginator = new Github\ResultPager($client); $paginator->postFetch(); $this->assertEquals($paginator->getPagination(), $pagination); @@ -122,7 +122,7 @@ public function fetchNext() $pagination = array('next' => 'http://github.com/next'); $resultContent = 'fetch test'; - $responseMock = $this->getResponseMock( $pagination ); + $responseMock = $this->getResponseMock($pagination); $responseMock ->expects($this->once()) ->method('getContent') @@ -132,17 +132,17 @@ public function fetchNext() ->expects($this->exactly(2)) ->method('getPagination'); - $httpClient = $this->getHttpClientMock( $responseMock ); + $httpClient = $this->getHttpClientMock($responseMock); $httpClient ->expects($this->once()) ->method('get') - ->with( $pagination['next'] ) + ->with($pagination['next']) ->will($this->returnValue($responseMock)); $client = $this->getClientMock($httpClient); - $paginator = new Github\ResultPager( $client ); + $paginator = new Github\ResultPager($client); $paginator->postFetch(); $this->assertEquals($paginator->fetchNext(), $resultContent); @@ -156,10 +156,10 @@ public function fetchNext() public function shouldHaveNext() { $responseMock = $this->getResponseMock(array('next' => 'http://github.com/next')); - $httpClient = $this->getHttpClientMock( $responseMock ); + $httpClient = $this->getHttpClientMock($responseMock); $client = $this->getClientMock($httpClient); - $paginator = new Github\ResultPager( $client ); + $paginator = new Github\ResultPager($client); $paginator->postFetch(); $this->assertEquals($paginator->hasNext(), true); @@ -174,34 +174,32 @@ public function shouldHaveNext() public function shouldHavePrevious() { $responseMock = $this->getResponseMock(array('prev' => 'http://github.com/previous')); - $httpClient = $this->getHttpClientMock( $responseMock ); + $httpClient = $this->getHttpClientMock($responseMock); $client = $this->getClientMock($httpClient); - $paginator = new Github\ResultPager( $client ); + $paginator = new Github\ResultPager($client); $paginator->postFetch(); $this->assertEquals($paginator->hasPrevious(), true); $this->assertEquals($paginator->hasNext(), false); } - protected function getResponseMock( array $pagination ) + protected function getResponseMock(array $pagination) { // response mock $responseMock = $this->getMock('Github\HttpClient\Message\Response'); $responseMock ->expects($this->any()) ->method('getPagination') - ->will($this->returnValue( - $pagination - )); + ->will($this->returnValue($pagination)); return $responseMock; } - protected function getClientMock( HttpClientInterface $httpClient = null ) + protected function getClientMock(HttpClientInterface $httpClient = null) { // if no httpClient isset use the default HttpClient mock - if( !$httpClient ){ + if (!$httpClient) { $httpClient = $this->getHttpClientMock(); } @@ -211,7 +209,7 @@ protected function getClientMock( HttpClientInterface $httpClient = null ) return $client; } - protected function getHttpClientMock( $responseMock = null ) + protected function getHttpClientMock($responseMock = null) { // mock the client interface $clientInterfaceMock = $this->getMock('Buzz\Client\ClientInterface', array('setTimeout', 'setVerifyPeer', 'send')); @@ -230,7 +228,7 @@ protected function getHttpClientMock( $responseMock = null ) // create the httpClient mock $httpClientMock = $this->getMock('Github\HttpClient\HttpClient', array(), array(array(), $clientInterfaceMock)); - if( $responseMock ){ + if ($responseMock) { $httpClientMock ->expects($this->any()) ->method('getLastResponse') @@ -240,12 +238,12 @@ protected function getHttpClientMock( $responseMock = null ) return $httpClientMock; } - protected function getApiMock( $apiClass ) + protected function getApiMock($apiClass) { $client = $this->getClientMock(); - return $this->getMockBuilder( $apiClass ) + return $this->getMockBuilder($apiClass) ->setConstructorArgs(array($client)) ->getMock(); } -} \ No newline at end of file +} From 470b8ac122597aec0ef993ed0ce18d33c02b6e26 Mon Sep 17 00:00:00 2001 From: Mitchel Verschoof Date: Tue, 30 Jul 2013 13:28:49 +0200 Subject: [PATCH 20/20] Added newline after return Added spaces after comma in an array --- lib/Github/Api/AbstractApi.php | 1 + test/Github/Tests/ResultPagerTest.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/Github/Api/AbstractApi.php b/lib/Github/Api/AbstractApi.php index 378b3414d71..727f4e51824 100644 --- a/lib/Github/Api/AbstractApi.php +++ b/lib/Github/Api/AbstractApi.php @@ -51,6 +51,7 @@ public function getPerPage() public function setPerPage($perPage) { $this->perPage = (null === $perPage ? $perPage : (int) $perPage); + return $this; } diff --git a/test/Github/Tests/ResultPagerTest.php b/test/Github/Tests/ResultPagerTest.php index df862c1d50a..7d37bb4a957 100644 --- a/test/Github/Tests/ResultPagerTest.php +++ b/test/Github/Tests/ResultPagerTest.php @@ -24,7 +24,7 @@ class ResultPagerTest extends \PHPUnit_Framework_TestCase public function shouldGetAllResults() { $amountLoops = 3; - $content = array(1,2,3,4,5,6,7,8,9,10); + $content = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); $responseMock = new TestResponse($amountLoops, $content); // httpClient mock