Skip to content

Commit cfef6d2

Browse files
authored
Merge pull request #50 from spinen/feature/fixGetAllCall
Feature/fix get all call
2 parents b560e5c + cb5dcf7 commit cfef6d2

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

src/Api/Client.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class Client
143143
* @param Token $token
144144
* @param Guzzle $guzzle
145145
* @param ModelResolver $resolver
146-
* @param string $version Version of the models to use with the API responses
146+
* @param string|null $version Version of the models to use with the API responses
147147
*/
148148
public function __construct(Token $token, Guzzle $guzzle, ModelResolver $resolver, $version = null)
149149
{
@@ -170,7 +170,10 @@ public function __call($verb, $args)
170170
}
171171

172172
// For "getAll", set page to 1 & change verb to "get", otherwise, no page
173-
($verb === 'getAll') ? $this->page = 1 && $verb = 'get' : $this->page = 0;
173+
if ($verb === 'getAll') {
174+
$this->page = 1;
175+
$verb = 'get';
176+
}
174177

175178
if (!in_array($verb, $this->verbs)) {
176179
throw new InvalidArgumentException(sprintf("Unsupported verb [%s] was requested.", $verb));
@@ -381,10 +384,10 @@ protected function isCollection(array $array)
381384
*
382385
* Here are some examples...
383386
* <https://some.host/v4_6_release/apis/3.0/finance/agreements&pageSize=10&page=2>; rel="next", \
384-
* <https://some.host/v4_6_release/apis/3.0/finance/agreements&pageSize=10&page=3>; rel="last
387+
* <https://some.host/v4_6_release/apis/3.0/finance/agreements&pageSize=10&page=3>; rel="last"
385388
*
386389
* <https://some.host/v4_6_release/apis/3.0/finance/agreements&pageSize=10&page=3>; rel="next", \
387-
* <https://some.host/v4_6_release/apis/3.0/finance/agreements&pageSize=10&page=1>; rel="first
390+
* <https://some.host/v4_6_release/apis/3.0/finance/agreements&pageSize=10&page=1>; rel="first"
388391
*
389392
* @param ResponseInterface $response
390393
*
@@ -418,7 +421,7 @@ protected function processError(RequestException $exception)
418421
* @param string $resource
419422
* @param ResponseInterface $response
420423
*
421-
* @return Collection|Model|ResponseInterface
424+
* @return Collection|Model|ResponseInterface|array
422425
*/
423426
protected function processResponse($resource, ResponseInterface $response)
424427
{
@@ -465,7 +468,7 @@ protected function request($method, $resource, array $options = [])
465468

466469
$processed = $this->processResponse($resource, $response);
467470

468-
// If, not a "gatAll" call, then return the response
471+
// If, not a "getAll" call, then return the response
469472
if (!$this->page) {
470473
return $processed;
471474
}
@@ -545,15 +548,15 @@ public function setPassword($password)
545548
}
546549

547550
/**
548-
* Set the page size
551+
* Set the page size, not allowing < 0
549552
*
550553
* @param integer $page_size
551554
*
552555
* @return $this
553556
*/
554557
public function setPageSize($page_size)
555558
{
556-
$this->page_size = $page_size;
559+
$this->page_size = $page_size >= 0 ? $page_size : 0;
557560

558561
return $this;
559562
}

tests/Api/ClientTest.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class ClientTest extends TestCase
3131
*/
3232
protected $resolver;
3333

34+
/**
35+
* @var Mock
36+
*/
37+
protected $response;
38+
3439
/**
3540
* @var Mock
3641
*/
@@ -61,17 +66,17 @@ public function it_can_be_constructed()
6166
*/
6267
public function it_returns_a_model_for_an_object_response()
6368
{
64-
$response = Mockery::mock(Response::class);
69+
$this->response = Mockery::mock(Response::class);
6570

66-
$response->shouldReceive('getBody')
71+
$this->response->shouldReceive('getBody')
6772
->once()
6873
->withNoArgs()
6974
->andReturn('{"version":"v2016.6.43325","isCloud":false,"serverTimeZone":"Eastern Standard Time"}');
7075

7176
$this->guzzle->shouldReceive('request')
7277
->once()
7378
->withAnyArgs()
74-
->andReturn($response);
79+
->andReturn($this->response);
7580

7681
$this->resolver->shouldReceive('find')
7782
->once()
@@ -106,9 +111,9 @@ public function it_returns_a_model_for_an_object_response()
106111
*/
107112
public function it_returns_a_collection_for_an_array_response()
108113
{
109-
$response = Mockery::mock(Response::class);
114+
$this->response = Mockery::mock(Response::class);
110115

111-
$response->shouldReceive('getBody')
116+
$this->response->shouldReceive('getBody')
112117
->once()
113118
->withNoArgs()
114119
->andReturn(
@@ -118,7 +123,7 @@ public function it_returns_a_collection_for_an_array_response()
118123
$this->guzzle->shouldReceive('request')
119124
->once()
120125
->withAnyArgs()
121-
->andReturn($response);
126+
->andReturn($this->response);
122127

123128
$this->resolver->shouldReceive('find')
124129
->once()
@@ -153,17 +158,17 @@ public function it_returns_a_collection_for_an_array_response()
153158
*/
154159
public function it_returns_an_array_when_there_is_not_a_model_to_map()
155160
{
156-
$response = Mockery::mock(Response::class);
161+
$this->response = Mockery::mock(Response::class);
157162

158-
$response->shouldReceive('getBody')
163+
$this->response->shouldReceive('getBody')
159164
->once()
160165
->withNoArgs()
161166
->andReturn('{"version":"v2016.6.43325","isCloud":false,"serverTimeZone":"Eastern Standard Time"}');
162167

163168
$this->guzzle->shouldReceive('request')
164169
->once()
165170
->withAnyArgs()
166-
->andReturn($response);
171+
->andReturn($this->response);
167172

168173
$this->resolver->shouldReceive('find')
169174
->once()
@@ -579,17 +584,17 @@ public function it_raises_exception_when_making_a_request_without_a_uri()
579584

580585
protected function mockOutEverythingForTestingThatGuzzleIsCalled()
581586
{
582-
$response = Mockery::mock(Response::class);
587+
$this->response = Mockery::mock(Response::class);
583588

584-
$response->shouldReceive('getBody')
589+
$this->response->shouldReceive('getBody')
585590
->once()
586591
->withNoArgs()
587592
->andReturn("{'responded': true}");
588593

589594
$this->guzzle->shouldReceive('request')
590595
->once()
591596
->withAnyArgs()
592-
->andReturn($response);
597+
->andReturn($this->response);
593598

594599
$this->resolver->shouldReceive('find')
595600
->once()
@@ -636,4 +641,21 @@ public function it_throws_an_exception_on_non_supported_versions()
636641
$this->client->setVersion('2012.2');
637642
$this->assertNotEquals('2012.2', $this->client->getVersion());
638643
}
644+
645+
/**
646+
* @test
647+
*/
648+
public function it_has_a_getAll_method_that_it_passes_to_guzzle_as_paginated_get_calls()
649+
{
650+
$this->mockOutEverythingForTestingThatGuzzleIsCalled();
651+
$get_header_link = '<https://some.host/v4_6_release/apis/3.0/finance/agreements&pageSize=10&page=2>; \
652+
rel="next", <https://some.host/v4_6_release/apis/3.0/finance/agreements&pageSize=10&page=3>; rel="last"';
653+
654+
$this->response->shouldReceive('getHeader')
655+
->once()
656+
->with('Link')
657+
->andReturn($get_header_link);
658+
659+
$this->client->getAll('uri');
660+
}
639661
}

0 commit comments

Comments
 (0)