Skip to content

Commit e02ef15

Browse files
committed
feat: check for correct response status code on TimeEntry::remove()
1 parent 1d87d20 commit e02ef15

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

src/Redmine/Api/TimeEntry.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,17 @@ public function remove($id)
250250
'/time_entries/' . $id . '.xml',
251251
));
252252

253-
return $this->lastResponse->getContent();
253+
$body = $this->lastResponse->getContent();
254+
$statusCode = $this->lastResponse->getStatusCode();
255+
256+
if ($statusCode !== 200 && $statusCode !== 204) {
257+
if (!Future::isForwardCompatibilityEnabled()) {
258+
return $body;
259+
}
260+
261+
throw UnexpectedResponseException::create($this->lastResponse);
262+
}
263+
264+
return $body;
254265
}
255266
}

tests/Unit/Api/TimeEntry/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\TimeEntry;
11+
use Redmine\Exception\UnexpectedResponseException;
12+
use Redmine\Future;
1113
use Redmine\Tests\Fixtures\AssertingHttpClient;
1214

1315
#[CoversClass(TimeEntry::class)]
@@ -53,4 +55,52 @@ public static function getRemoveData(): array
5355
],
5456
];
5557
}
58+
59+
public function testRemoveWithIncorrectStatusCodeReturnsBody(): void
60+
{
61+
$client = AssertingHttpClient::create(
62+
$this,
63+
[
64+
'DELETE',
65+
'/time_entries/5.xml',
66+
'application/xml',
67+
'',
68+
500,
69+
'',
70+
'',
71+
],
72+
);
73+
74+
$api = TimeEntry::fromHttpClient($client);
75+
76+
$this->assertSame('', $api->remove(5));
77+
}
78+
79+
public function testRemoveWithIncorrectStatusCodeThrowsException(): void
80+
{
81+
$client = AssertingHttpClient::create(
82+
$this,
83+
[
84+
'DELETE',
85+
'/time_entries/5.xml',
86+
'application/xml',
87+
'',
88+
500,
89+
'',
90+
'',
91+
],
92+
);
93+
94+
$api = TimeEntry::fromHttpClient($client);
95+
96+
$this->expectException(UnexpectedResponseException::class);
97+
98+
try {
99+
Future::enableForwardCompatibility();
100+
101+
$api->remove(5);
102+
} finally {
103+
Future::disableForwardCompatibility();
104+
}
105+
}
56106
}

0 commit comments

Comments
 (0)