Skip to content

Commit 92b19de

Browse files
committed
Add retrieveData method, deprecate retrieveAll
1 parent 17950ad commit 92b19de

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

src/Redmine/Api/AbstractApi.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function lastCallFailed()
5151
*/
5252
protected function get($path, $decodeIfJson = true)
5353
{
54-
$this->client->requestGet($path);
54+
$this->client->requestGet(strval($path));
5555

5656
$body = $this->client->getLastResponseBody();
5757
$contentType = $this->client->getLastResponseContentType();
@@ -65,6 +65,7 @@ protected function get($path, $decodeIfJson = true)
6565
try {
6666
return json_decode($body, true, 512, \JSON_THROW_ON_ERROR);
6767
} catch (JsonException $e) {
68+
// TODO: Throw Exception instead of returning string
6869
return 'Error decoding body as JSON: '.$e->getMessage();
6970
}
7071
}
@@ -161,23 +162,42 @@ protected function sanitizeParams(array $defaults, array $params)
161162
* Retrieves all the elements of a given endpoint (even if the
162163
* total number of elements is greater than 100).
163164
*
165+
* @deprecated the `retrieveAll()` method is deprecated, use `retrieveData()` instead.
166+
*
164167
* @param string $endpoint API end point
165168
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
166169
*
167170
* @return array elements found
168171
*/
169172
protected function retrieveAll($endpoint, array $params = [])
173+
{
174+
@trigger_error('The '.__METHOD__.' method is deprecated, use `retrieveData()` instead.', E_USER_DEPRECATED);
175+
176+
return $this->retrieveData(strval($endpoint), $params);
177+
}
178+
179+
/**
180+
* Retrieves all the elements of a given endpoint (even if the
181+
* total number of elements is greater than 100).
182+
*
183+
* @param string $endpoint API end point
184+
* @param array $params optional parameters to be passed to the api (offset, limit, ...)
185+
*
186+
* @return array elements found
187+
*/
188+
protected function retrieveData(string $endpoint, array $params = [])/*: array*/ // TODO: check if return type could be array
170189
{
171190
if (empty($params)) {
172191
return $this->get($endpoint);
173192
}
193+
174194
$defaults = [
175195
'limit' => 25,
176196
'offset' => 0,
177197
];
178198
$params = $this->sanitizeParams($defaults, $params);
179199

180-
$ret = [];
200+
$data = [];
181201

182202
$limit = $params['limit'];
183203
$offset = $params['offset'];
@@ -194,7 +214,7 @@ protected function retrieveAll($endpoint, array $params = [])
194214
$params['offset'] = $offset;
195215

196216
$newDataSet = (array) $this->get($endpoint.'?'.preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', http_build_query($params)));
197-
$ret = array_merge_recursive($ret, $newDataSet);
217+
$data = array_merge_recursive($data, $newDataSet);
198218

199219
$offset += $_limit;
200220
if (empty($newDataSet) || !isset($newDataSet['limit']) || (
@@ -207,7 +227,7 @@ protected function retrieveAll($endpoint, array $params = [])
207227
}
208228
}
209229

210-
return $ret;
230+
return $data;
211231
}
212232

213233
/**

tests/Unit/Api/CustomFieldTest.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class CustomFieldTest extends TestCase
2525
public function testAllReturnsClientGetResponse()
2626
{
2727
// Test values
28-
$response = 'API Response';
28+
$response = '["API Response"]';
29+
$expectedResponse = ['API Response'];
2930

3031
// Create the used mock objects
3132
$client = $this->createMock(Client::class);
@@ -38,12 +39,15 @@ public function testAllReturnsClientGetResponse()
3839
$client->expects($this->exactly(1))
3940
->method('getLastResponseBody')
4041
->willReturn($response);
42+
$client->expects($this->exactly(1))
43+
->method('getLastResponseContentType')
44+
->willReturn('application/json');
4145

4246
// Create the object under test
4347
$api = new CustomField($client);
4448

4549
// Perform the tests
46-
$this->assertSame($response, $api->all());
50+
$this->assertSame($expectedResponse, $api->all());
4751
}
4852

4953
/**
@@ -59,7 +63,8 @@ public function testAllReturnsClientGetResponseWithParameters()
5963
{
6064
// Test values
6165
$allParameters = ['not-used'];
62-
$response = 'API Response';
66+
$response = '["API Response"]';
67+
$expectedResponse = ['API Response'];
6368

6469
// Create the used mock objects
6570
$client = $this->createMock(Client::class);
@@ -72,12 +77,15 @@ public function testAllReturnsClientGetResponseWithParameters()
7277
$client->expects($this->exactly(1))
7378
->method('getLastResponseBody')
7479
->willReturn($response);
80+
$client->expects($this->exactly(1))
81+
->method('getLastResponseContentType')
82+
->willReturn('application/json');
7583

7684
// Create the object under test
7785
$api = new CustomField($client);
7886

7987
// Perform the tests
80-
$this->assertSame([$response], $api->all($allParameters));
88+
$this->assertSame($expectedResponse, $api->all($allParameters));
8189
}
8290

8391
/**
@@ -94,8 +102,8 @@ public function testAllReturnsClientGetResponseWithHighLimit()
94102
// Test values
95103
$response = '{"limit":"100","items":[]}';
96104
$allParameters = ['limit' => 250];
97-
$returnDataSet = [
98-
'limit' => '100',
105+
$expectedResponse = [
106+
'limit' => ['100', '100', '100'], // TODO: Check response created by array_merge_recursive()
99107
'items' => [],
100108
];
101109

@@ -118,10 +126,7 @@ public function testAllReturnsClientGetResponseWithHighLimit()
118126
$api = new CustomField($client);
119127

120128
// Perform the tests
121-
$retrievedDataSet = $api->all($allParameters);
122-
$this->assertTrue(is_array($retrievedDataSet));
123-
$this->assertArrayHasKey('limit', $retrievedDataSet);
124-
$this->assertArrayHasKey('items', $retrievedDataSet);
129+
$this->assertSame($expectedResponse, $api->all($allParameters));
125130
}
126131

127132
/**

0 commit comments

Comments
 (0)