Skip to content

Commit a31b81a

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

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;
@@ -255,8 +256,12 @@ public function create(array $params = [])
255256

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

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

262267
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

@@ -232,7 +234,7 @@ public static function getCreateData(): array
232234
];
233235
}
234236

235-
public function testCreateReturnsEmptyString(): void
237+
public function testCreateWithIncorrectStatusCodeReturnsEmptyString(): void
236238
{
237239
$client = AssertingHttpClient::create(
238240
$this,
@@ -256,6 +258,35 @@ public function testCreateReturnsEmptyString(): void
256258
$this->assertSame('', $return);
257259
}
258260

261+
public function testCreateWithIncorrectStatusCodeThrowsException(): void
262+
{
263+
$client = AssertingHttpClient::create(
264+
$this,
265+
[
266+
'POST',
267+
'/issues.xml',
268+
'application/xml',
269+
'<?xml version="1.0" encoding="UTF-8"?><issue/>',
270+
500,
271+
'',
272+
'',
273+
],
274+
);
275+
276+
// Create the object under test
277+
$api = Issue::fromHttpClient($client);
278+
279+
$this->expectException(UnexpectedResponseException::class);
280+
281+
try {
282+
Future::enableForwardCompatibility();
283+
284+
$api->create([]);
285+
} finally {
286+
Future::disableForwardCompatibility();
287+
}
288+
}
289+
259290
public function testCreateWithHttpClientRetrievesIssueStatusId(): void
260291
{
261292
$client = AssertingHttpClient::create(
@@ -274,7 +305,7 @@ public function testCreateWithHttpClientRetrievesIssueStatusId(): void
274305
'/issues.xml',
275306
'application/xml',
276307
'<?xml version="1.0"?><issue><status_id>123</status_id></issue>',
277-
200,
308+
201,
278309
'application/xml',
279310
'<?xml version="1.0"?><issue></issue>',
280311
],
@@ -311,7 +342,7 @@ public function testCreateWithHttpClientRetrievesProjectId(): void
311342
'/issues.xml',
312343
'application/xml',
313344
'<?xml version="1.0"?><issue><project_id>3</project_id></issue>',
314-
200,
345+
201,
315346
'application/xml',
316347
'<?xml version="1.0"?><issue></issue>',
317348
],
@@ -348,7 +379,7 @@ public function testCreateWithHttpClientRetrievesIssueCategoryId(): void
348379
'/issues.xml',
349380
'application/xml',
350381
'<?xml version="1.0"?><issue><project_id>3</project_id><category_id>45</category_id></issue>',
351-
200,
382+
201,
352383
'application/xml',
353384
'<?xml version="1.0"?><issue></issue>',
354385
],
@@ -385,7 +416,7 @@ public function testCreateWithHttpClientRetrievesTrackerId(): void
385416
'/issues.xml',
386417
'application/xml',
387418
'<?xml version="1.0"?><issue><tracker_id>9</tracker_id></issue>',
388-
200,
419+
201,
389420
'application/xml',
390421
'<?xml version="1.0"?><issue></issue>',
391422
],
@@ -422,7 +453,7 @@ public function testCreateWithHttpClientRetrievesUserId(): void
422453
'/issues.xml',
423454
'application/xml',
424455
'<?xml version="1.0"?><issue><assigned_to_id>6</assigned_to_id><author_id>5</author_id></issue>',
425-
200,
456+
201,
426457
'application/xml',
427458
'<?xml version="1.0"?><issue></issue>',
428459
],
@@ -508,7 +539,7 @@ public function testCreateWithClientCleansParameters(): void
508539
<author_id>5</author_id>
509540
</issue>
510541
XML,
511-
200,
542+
201,
512543
'application/xml',
513544
'<?xml version="1.0"?><issue></issue>',
514545
],

tests/Unit/Api/IssueTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ public function testCreateWithClientCleansParameters(): void
268268
$legacyClient->expects($this->exactly(1))
269269
->method('getLastResponseContentType')
270270
->willReturn('application/xml');
271+
$legacyClient->expects($this->exactly(1))
272+
->method('getLastResponseStatusCode')
273+
->willReturn(201);
271274

272275
// Create the object under test
273276
$api = new Issue($legacyClient);

0 commit comments

Comments
 (0)