Skip to content

Commit a7be46f

Browse files
committed
feat: check for correct response status code on create issue
1 parent 67dd1e5 commit a7be46f

3 files changed

Lines changed: 48 additions & 9 deletions

File tree

src/Redmine/Api/Issue.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Redmine\Exception;
99
use Redmine\Exception\SerializerException;
1010
use Redmine\Exception\UnexpectedResponseException;
11+
use Redmine\Future;
1112
use Redmine\Http\HttpClient;
1213
use Redmine\Http\HttpFactory;
1314
use Redmine\Serializer\JsonSerializer;
@@ -254,8 +255,12 @@ public function create(array $params = [])
254255

255256
$body = $this->lastResponse->getContent();
256257

257-
if ($body === '') {
258-
return $body;
258+
if ($this->lastResponse->getStatusCode() !== 201) {
259+
if (!Future::isForwardCompatibilityEnabled() && $body === '') {
260+
return $body;
261+
}
262+
263+
throw UnexpectedResponseException::create($this->lastResponse);
259264
}
260265

261266
return new SimpleXMLElement($body);

tests/Unit/Api/Issue/CreateTest.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use PHPUnit\Framework\Attributes\DataProvider;
77
use PHPUnit\Framework\TestCase;
88
use Redmine\Api\Issue;
9+
use Redmine\Exception\UnexpectedResponseException;
10+
use Redmine\Future;
911
use Redmine\Tests\Fixtures\AssertingHttpClient;
1012
use SimpleXMLElement;
1113

@@ -237,7 +239,7 @@ public static function getCreateData(): array
237239
];
238240
}
239241

240-
public function testCreateReturnsEmptyString(): void
242+
public function testCreateWithIncorrectStatusCodeReturnsEmptyString(): void
241243
{
242244
$client = AssertingHttpClient::create(
243245
$this,
@@ -261,6 +263,35 @@ public function testCreateReturnsEmptyString(): void
261263
$this->assertSame('', $return);
262264
}
263265

266+
public function testCreateWithIncorrectStatusCodeThrowsException(): void
267+
{
268+
$client = AssertingHttpClient::create(
269+
$this,
270+
[
271+
'POST',
272+
'/issues.xml',
273+
'application/xml',
274+
'<?xml version="1.0" encoding="UTF-8"?><issue/>',
275+
500,
276+
'',
277+
'',
278+
],
279+
);
280+
281+
// Create the object under test
282+
$api = Issue::fromHttpClient($client);
283+
284+
$this->expectException(UnexpectedResponseException::class);
285+
286+
try {
287+
Future::enableForwardCompatibility();
288+
289+
$api->create([]);
290+
} finally {
291+
Future::disableForwardCompatibility();
292+
}
293+
}
294+
264295
public function testCreateWithHttpClientRetrievesIssueStatusId(): void
265296
{
266297
$client = AssertingHttpClient::create(
@@ -279,7 +310,7 @@ public function testCreateWithHttpClientRetrievesIssueStatusId(): void
279310
'/issues.xml',
280311
'application/xml',
281312
'<?xml version="1.0"?><issue><status_id>123</status_id></issue>',
282-
200,
313+
201,
283314
'application/xml',
284315
'<?xml version="1.0"?><issue></issue>',
285316
],
@@ -316,7 +347,7 @@ public function testCreateWithHttpClientRetrievesProjectId(): void
316347
'/issues.xml',
317348
'application/xml',
318349
'<?xml version="1.0"?><issue><project_id>3</project_id></issue>',
319-
200,
350+
201,
320351
'application/xml',
321352
'<?xml version="1.0"?><issue></issue>',
322353
],
@@ -353,7 +384,7 @@ public function testCreateWithHttpClientRetrievesIssueCategoryId(): void
353384
'/issues.xml',
354385
'application/xml',
355386
'<?xml version="1.0"?><issue><project_id>3</project_id><category_id>45</category_id></issue>',
356-
200,
387+
201,
357388
'application/xml',
358389
'<?xml version="1.0"?><issue></issue>',
359390
],
@@ -390,7 +421,7 @@ public function testCreateWithHttpClientRetrievesTrackerId(): void
390421
'/issues.xml',
391422
'application/xml',
392423
'<?xml version="1.0"?><issue><tracker_id>9</tracker_id></issue>',
393-
200,
424+
201,
394425
'application/xml',
395426
'<?xml version="1.0"?><issue></issue>',
396427
],
@@ -427,7 +458,7 @@ public function testCreateWithHttpClientRetrievesUserId(): void
427458
'/issues.xml',
428459
'application/xml',
429460
'<?xml version="1.0"?><issue><assigned_to_id>6</assigned_to_id><author_id>5</author_id></issue>',
430-
200,
461+
201,
431462
'application/xml',
432463
'<?xml version="1.0"?><issue></issue>',
433464
],
@@ -513,7 +544,7 @@ public function testCreateWithClientCleansParameters(): void
513544
<author_id>5</author_id>
514545
</issue>
515546
XML,
516-
200,
547+
201,
517548
'application/xml',
518549
'<?xml version="1.0"?><issue></issue>',
519550
],

tests/Unit/Api/IssueTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ public function testCreateWithClientCleansParameters(): void
276276
$legacyClient->expects($this->exactly(1))
277277
->method('getLastResponseContentType')
278278
->willReturn('application/xml');
279+
$legacyClient->expects($this->exactly(1))
280+
->method('getLastResponseStatusCode')
281+
->willReturn(201);
279282

280283
// Create the object under test
281284
$api = new Issue($legacyClient);

0 commit comments

Comments
 (0)