Skip to content

Commit 5f68c45

Browse files
committed
fix: correct BC guard in Issue::create(), IssueCategory::create(), Membership::create()
1 parent 48f500e commit 5f68c45

6 files changed

Lines changed: 87 additions & 6 deletions

File tree

src/Redmine/Api/Issue.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,12 @@ public function create(array $params = [])
258258
$body = $this->lastResponse->getContent();
259259

260260
if ($this->lastResponse->getStatusCode() !== 201) {
261-
if (!Future::isForwardCompatibilityEnabled() && $body === '') {
262-
return $body;
261+
if (!Future::isForwardCompatibilityEnabled()) {
262+
if ($body === '') {
263+
return $body;
264+
}
265+
266+
return new SimpleXMLElement($body);
263267
}
264268

265269
throw UnexpectedResponseException::create($this->lastResponse);

src/Redmine/Api/IssueCategory.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,12 @@ public function create($project, array $params = [])
269269
$body = $this->lastResponse->getContent();
270270

271271
if ($this->lastResponse->getStatusCode() !== 201) {
272-
if (!Future::isForwardCompatibilityEnabled() && $body === '') {
273-
return $body;
272+
if (!Future::isForwardCompatibilityEnabled()) {
273+
if ($body === '') {
274+
return $body;
275+
}
276+
277+
return new SimpleXMLElement($body);
274278
}
275279

276280
throw UnexpectedResponseException::create($this->lastResponse);

src/Redmine/Api/Membership.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,12 @@ public function create($project, array $params = [])
151151
$body = $this->lastResponse->getContent();
152152

153153
if ($this->lastResponse->getStatusCode() !== 201) {
154-
if (!Future::isForwardCompatibilityEnabled() && $body === '') {
155-
return $body;
154+
if (!Future::isForwardCompatibilityEnabled()) {
155+
if ($body === '') {
156+
return $body;
157+
}
158+
159+
return new SimpleXMLElement($body);
156160
}
157161

158162
throw UnexpectedResponseException::create($this->lastResponse);

tests/Unit/Api/Issue/CreateTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,29 @@ public function testCreateWithIncorrectStatusCodeReturnsEmptyString(): void
265265
$this->assertSame('', $return);
266266
}
267267

268+
public function testCreateWithIncorrectStatusCodeReturnsBody(): 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+
'<error>error</error>',
280+
],
281+
);
282+
283+
$api = Issue::fromHttpClient($client);
284+
285+
$return = $api->create([]);
286+
287+
$this->assertInstanceOf(SimpleXMLElement::class, $return);
288+
$this->assertXmlStringEqualsXmlString('<error>error</error>', $return->asXml());
289+
}
290+
268291
public function testCreateWithIncorrectStatusCodeThrowsException(): void
269292
{
270293
$client = AssertingHttpClient::create(

tests/Unit/Api/IssueCategory/CreateTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,29 @@ public function testCreateReturnsEmptyString(): void
107107
$this->assertSame('', $return);
108108
}
109109

110+
public function testCreateWithIncorrectStatusCodeReturnsBody(): void
111+
{
112+
$client = AssertingHttpClient::create(
113+
$this,
114+
[
115+
'POST',
116+
'/projects/5/issue_categories.xml',
117+
'application/xml',
118+
'<?xml version="1.0" encoding="UTF-8"?><issue_category><name>Test Category</name></issue_category>',
119+
500,
120+
'',
121+
'<error>error</error>',
122+
],
123+
);
124+
125+
$api = IssueCategory::fromHttpClient($client);
126+
127+
$return = $api->create(5, ['name' => 'Test Category']);
128+
129+
$this->assertInstanceOf(SimpleXMLElement::class, $return);
130+
$this->assertXmlStringEqualsXmlString('<error>error</error>', $return->asXml());
131+
}
132+
110133
public function testCreateWithIncorrectStatusCodeThrowsException(): void
111134
{
112135
$client = AssertingHttpClient::create(

tests/Unit/Api/Membership/CreateTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,29 @@ public function testCreateReturnsEmptyString(): void
9898
$this->assertSame('', $return);
9999
}
100100

101+
public function testCreateWithIncorrectStatusCodeReturnsBody(): void
102+
{
103+
$client = AssertingHttpClient::create(
104+
$this,
105+
[
106+
'POST',
107+
'/projects/5/memberships.xml',
108+
'application/xml',
109+
'<?xml version="1.0" encoding="UTF-8"?><membership><user_id>4</user_id><role_ids>2</role_ids></membership>',
110+
500,
111+
'',
112+
'<error>error</error>',
113+
],
114+
);
115+
116+
$api = Membership::fromHttpClient($client);
117+
118+
$return = $api->create(5, ['user_id' => 4, 'role_ids' => 2]);
119+
120+
$this->assertInstanceOf(SimpleXMLElement::class, $return);
121+
$this->assertXmlStringEqualsXmlString('<error>error</error>', $return->asXml());
122+
}
123+
101124
public function testCreateThrowsExceptionWithEmptyParameters(): void
102125
{
103126
$client = $this->createStub(HttpClient::class);

0 commit comments

Comments
 (0)