Skip to content

Commit dd287a2

Browse files
committed
feat: check for correct response status code on create issue
1 parent 1d89ab0 commit dd287a2

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
@@ -10,6 +10,7 @@
1010
use Redmine\Exception;
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;
@@ -256,8 +257,12 @@ public function create(array $params = [])
256257

257258
$body = $this->lastResponse->getContent();
258259

259-
if ($body === '') {
260-
return $body;
260+
if ($this->lastResponse->getStatusCode() !== 201) {
261+
if (!Future::isForwardCompatibilityEnabled() && $body === '') {
262+
return $body;
263+
}
264+
265+
throw UnexpectedResponseException::create($this->lastResponse);
261266
}
262267

263268
return new SimpleXMLElement($body);

tests/Unit/Api/Issue/CreateTest.php

Lines changed: 38 additions & 7 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\Issue;
11+
use Redmine\Exception\UnexpectedResponseException;
12+
use Redmine\Future;
1113
use Redmine\Tests\Fixtures\AssertingHttpClient;
1214
use SimpleXMLElement;
1315

@@ -239,7 +241,7 @@ public static function getCreateData(): array
239241
];
240242
}
241243

242-
public function testCreateReturnsEmptyString(): void
244+
public function testCreateWithIncorrectStatusCodeReturnsEmptyString(): void
243245
{
244246
$client = AssertingHttpClient::create(
245247
$this,
@@ -263,6 +265,35 @@ public function testCreateReturnsEmptyString(): void
263265
$this->assertSame('', $return);
264266
}
265267

268+
public function testCreateWithIncorrectStatusCodeThrowsException(): void
269+
{
270+
$client = AssertingHttpClient::create(
271+
$this,
272+
[
273+
'POST',
274+
'/issues.xml',
275+
'application/xml',
276+
'<?xml version="1.0" encoding="UTF-8"?><issue/>',
277+
500,
278+
'',
279+
'',
280+
],
281+
);
282+
283+
// Create the object under test
284+
$api = Issue::fromHttpClient($client);
285+
286+
$this->expectException(UnexpectedResponseException::class);
287+
288+
try {
289+
Future::enableForwardCompatibility();
290+
291+
$api->create([]);
292+
} finally {
293+
Future::disableForwardCompatibility();
294+
}
295+
}
296+
266297
public function testCreateWithHttpClientRetrievesIssueStatusId(): void
267298
{
268299
$client = AssertingHttpClient::create(
@@ -281,7 +312,7 @@ public function testCreateWithHttpClientRetrievesIssueStatusId(): void
281312
'/issues.xml',
282313
'application/xml',
283314
'<?xml version="1.0"?><issue><status_id>123</status_id></issue>',
284-
200,
315+
201,
285316
'application/xml',
286317
'<?xml version="1.0"?><issue></issue>',
287318
],
@@ -318,7 +349,7 @@ public function testCreateWithHttpClientRetrievesProjectId(): void
318349
'/issues.xml',
319350
'application/xml',
320351
'<?xml version="1.0"?><issue><project_id>3</project_id></issue>',
321-
200,
352+
201,
322353
'application/xml',
323354
'<?xml version="1.0"?><issue></issue>',
324355
],
@@ -355,7 +386,7 @@ public function testCreateWithHttpClientRetrievesIssueCategoryId(): void
355386
'/issues.xml',
356387
'application/xml',
357388
'<?xml version="1.0"?><issue><project_id>3</project_id><category_id>45</category_id></issue>',
358-
200,
389+
201,
359390
'application/xml',
360391
'<?xml version="1.0"?><issue></issue>',
361392
],
@@ -392,7 +423,7 @@ public function testCreateWithHttpClientRetrievesTrackerId(): void
392423
'/issues.xml',
393424
'application/xml',
394425
'<?xml version="1.0"?><issue><tracker_id>9</tracker_id></issue>',
395-
200,
426+
201,
396427
'application/xml',
397428
'<?xml version="1.0"?><issue></issue>',
398429
],
@@ -429,7 +460,7 @@ public function testCreateWithHttpClientRetrievesUserId(): void
429460
'/issues.xml',
430461
'application/xml',
431462
'<?xml version="1.0"?><issue><assigned_to_id>6</assigned_to_id><author_id>5</author_id></issue>',
432-
200,
463+
201,
433464
'application/xml',
434465
'<?xml version="1.0"?><issue></issue>',
435466
],
@@ -515,7 +546,7 @@ public function testCreateWithClientCleansParameters(): void
515546
<author_id>5</author_id>
516547
</issue>
517548
XML,
518-
200,
549+
201,
519550
'application/xml',
520551
'<?xml version="1.0"?><issue></issue>',
521552
],

tests/Unit/Api/IssueTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ public function testCreateWithClientCleansParameters(): void
278278
$legacyClient->expects($this->exactly(1))
279279
->method('getLastResponseContentType')
280280
->willReturn('application/xml');
281+
$legacyClient->expects($this->exactly(1))
282+
->method('getLastResponseStatusCode')
283+
->willReturn(201);
281284

282285
// Create the object under test
283286
$api = new Issue($legacyClient);

0 commit comments

Comments
 (0)