Skip to content

Commit f51aed2

Browse files
committed
feat: check for correct response status code on Version::create(), Version::update() and Version::remove()
1 parent e02ef15 commit f51aed2

4 files changed

Lines changed: 164 additions & 2 deletions

File tree

src/Redmine/Api/Version.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Redmine\Exception\MissingParameterException;
1111
use Redmine\Exception\SerializerException;
1212
use Redmine\Exception\UnexpectedResponseException;
13+
use Redmine\Future;
1314
use Redmine\Http\HttpClient;
1415
use Redmine\Http\HttpFactory;
1516
use Redmine\Serializer\JsonSerializer;
@@ -274,6 +275,15 @@ public function create($project, array $params = [])
274275
));
275276

276277
$body = $this->lastResponse->getContent();
278+
$statusCode = $this->lastResponse->getStatusCode();
279+
280+
if ($statusCode !== 201) {
281+
if (!Future::isForwardCompatibilityEnabled()) {
282+
return $body;
283+
}
284+
285+
throw UnexpectedResponseException::create($this->lastResponse);
286+
}
277287

278288
if ('' !== $body) {
279289
return new SimpleXMLElement($body);
@@ -311,7 +321,18 @@ public function update($id, array $params)
311321
XmlSerializer::createFromArray(['version' => $params])->getEncoded(),
312322
));
313323

314-
return $this->lastResponse->getContent();
324+
$body = $this->lastResponse->getContent();
325+
$statusCode = $this->lastResponse->getStatusCode();
326+
327+
if ($statusCode !== 200 && $statusCode !== 204) {
328+
if (!Future::isForwardCompatibilityEnabled()) {
329+
return $body;
330+
}
331+
332+
throw UnexpectedResponseException::create($this->lastResponse);
333+
}
334+
335+
return $body;
315336
}
316337

317338
/**
@@ -368,7 +389,18 @@ public function remove($id)
368389
'/versions/' . $id . '.xml',
369390
));
370391

371-
return $this->lastResponse->getContent();
392+
$body = $this->lastResponse->getContent();
393+
$statusCode = $this->lastResponse->getStatusCode();
394+
395+
if ($statusCode !== 200 && $statusCode !== 204) {
396+
if (!Future::isForwardCompatibilityEnabled()) {
397+
return $body;
398+
}
399+
400+
throw UnexpectedResponseException::create($this->lastResponse);
401+
}
402+
403+
return $body;
372404
}
373405

374406
/**

tests/Unit/Api/Version/CreateTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Redmine\Api\Version;
1111
use Redmine\Exception\InvalidParameterException;
1212
use Redmine\Exception\MissingParameterException;
13+
use Redmine\Exception\UnexpectedResponseException;
14+
use Redmine\Future;
1315
use Redmine\Http\HttpClient;
1416
use Redmine\Tests\Fixtures\AssertingHttpClient;
1517
use SimpleXMLElement;
@@ -217,4 +219,32 @@ public function testCreateWithInvalidStatusThrowsInvalidParameterException(): vo
217219
// Perform the tests
218220
$api->create('test', $parameters);
219221
}
222+
223+
public function testCreateWithIncorrectStatusCodeThrowsException(): void
224+
{
225+
$client = AssertingHttpClient::create(
226+
$this,
227+
[
228+
'POST',
229+
'/projects/5/versions.xml',
230+
'application/xml',
231+
'<?xml version="1.0" encoding="UTF-8"?><version><name>test</name></version>',
232+
500,
233+
'',
234+
'',
235+
],
236+
);
237+
238+
$api = Version::fromHttpClient($client);
239+
240+
$this->expectException(UnexpectedResponseException::class);
241+
242+
try {
243+
Future::enableForwardCompatibility();
244+
245+
$api->create(5, ['name' => 'test']);
246+
} finally {
247+
Future::disableForwardCompatibility();
248+
}
249+
}
220250
}

tests/Unit/Api/Version/RemoveTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use PHPUnit\Framework\Attributes\DataProvider;
99
use PHPUnit\Framework\TestCase;
1010
use Redmine\Api\Version;
11+
use Redmine\Exception\UnexpectedResponseException;
12+
use Redmine\Future;
1113
use Redmine\Tests\Fixtures\AssertingHttpClient;
1214

1315
#[CoversClass(Version::class)]
@@ -61,4 +63,52 @@ public static function getRemoveData(): array
6163
],
6264
];
6365
}
66+
67+
public function testRemoveWithIncorrectStatusCodeReturnsBody(): void
68+
{
69+
$client = AssertingHttpClient::create(
70+
$this,
71+
[
72+
'DELETE',
73+
'/versions/5.xml',
74+
'application/xml',
75+
'',
76+
500,
77+
'',
78+
'',
79+
],
80+
);
81+
82+
$api = Version::fromHttpClient($client);
83+
84+
$this->assertSame('', $api->remove(5));
85+
}
86+
87+
public function testRemoveWithIncorrectStatusCodeThrowsException(): void
88+
{
89+
$client = AssertingHttpClient::create(
90+
$this,
91+
[
92+
'DELETE',
93+
'/versions/5.xml',
94+
'application/xml',
95+
'',
96+
500,
97+
'',
98+
'',
99+
],
100+
);
101+
102+
$api = Version::fromHttpClient($client);
103+
104+
$this->expectException(UnexpectedResponseException::class);
105+
106+
try {
107+
Future::enableForwardCompatibility();
108+
109+
$api->remove(5);
110+
} finally {
111+
Future::disableForwardCompatibility();
112+
}
113+
}
64114
}

tests/Unit/Api/Version/UpdateTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use PHPUnit\Framework\TestCase;
1010
use Redmine\Api\Version;
1111
use Redmine\Exception\InvalidParameterException;
12+
use Redmine\Exception\UnexpectedResponseException;
13+
use Redmine\Future;
1214
use Redmine\Http\HttpClient;
1315
use Redmine\Tests\Fixtures\AssertingHttpClient;
1416

@@ -223,4 +225,52 @@ public function testUpdateWithInvalidSharingThrowsInvalidParameterException(): v
223225
// Perform the tests
224226
$api->update(5, $parameters);
225227
}
228+
229+
public function testUpdateWithIncorrectStatusCodeReturnsBody(): void
230+
{
231+
$client = AssertingHttpClient::create(
232+
$this,
233+
[
234+
'PUT',
235+
'/versions/5.xml',
236+
'application/xml',
237+
'<?xml version="1.0" encoding="UTF-8"?><version></version>',
238+
500,
239+
'',
240+
'',
241+
],
242+
);
243+
244+
$api = Version::fromHttpClient($client);
245+
246+
$this->assertSame('', $api->update(5, []));
247+
}
248+
249+
public function testUpdateWithIncorrectStatusCodeThrowsException(): void
250+
{
251+
$client = AssertingHttpClient::create(
252+
$this,
253+
[
254+
'PUT',
255+
'/versions/5.xml',
256+
'application/xml',
257+
'<?xml version="1.0" encoding="UTF-8"?><version></version>',
258+
500,
259+
'',
260+
'',
261+
],
262+
);
263+
264+
$api = Version::fromHttpClient($client);
265+
266+
$this->expectException(UnexpectedResponseException::class);
267+
268+
try {
269+
Future::enableForwardCompatibility();
270+
271+
$api->update(5, []);
272+
} finally {
273+
Future::disableForwardCompatibility();
274+
}
275+
}
226276
}

0 commit comments

Comments
 (0)