diff --git a/tests/Unit/Api/AbstractApi/RetrieveDataTest.php b/tests/Unit/Api/AbstractApi/RetrieveDataTest.php index eb763c0d..e6992207 100644 --- a/tests/Unit/Api/AbstractApi/RetrieveDataTest.php +++ b/tests/Unit/Api/AbstractApi/RetrieveDataTest.php @@ -8,6 +8,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Redmine\Api\AbstractApi; +use Redmine\Exception\SerializerException; use Redmine\Exception\UnexpectedResponseException; use Redmine\Http\HttpClient; use Redmine\Tests\Fixtures\AssertingHttpClient; @@ -184,6 +185,63 @@ public static function getRetrieveDataWithParamsData(): array ]; } + /** + * @dataProvider getRequestContentTypeData + */ + #[DataProvider('getRequestContentTypeData')] + public function testRetrieveDataSendsContentTypeGuessedFromPath(string $path, string $expectedContentType): void + { + $api = $this->createApi(AssertingHttpClient::create( + $this, + ['GET', $path, $expectedContentType, '', 200, 'application/json', '{}'], + )); + + $this->assertSame([], $this->retrieveData($api, $path)); + } + + /** + * @return array + */ + public static function getRequestContentTypeData(): array + { + return [ + 'JSON path' => ['/issues.json', 'application/json'], + 'JSON path with query' => ['/issues.json?limit=1', 'application/json'], + 'XML path' => ['/issues.xml', 'application/xml'], + 'JSON upload path' => ['/uploads.json', 'application/octet-stream'], + 'XML upload path' => ['/uploads.xml', 'application/octet-stream'], + 'other path' => ['/issues', ''], + ]; + } + + /** + * @dataProvider getNotConvertibleResponseData + */ + #[DataProvider('getNotConvertibleResponseData')] + public function testRetrieveDataAndNotConvertibleResponseThrowsException(string $contentType, string $response): void + { + $api = $this->createApi(AssertingHttpClient::create( + $this, + ['GET', '/issues.json', 'application/json', '', 200, $contentType, $response], + )); + + $this->expectException(SerializerException::class); + $this->expectExceptionMessage('Could not convert response body into array: ' . $response); + + $this->retrieveData($api, '/issues.json'); + } + + /** + * @return array + */ + public static function getNotConvertibleResponseData(): array + { + return [ + 'unsupported content type' => ['text/plain', 'not an array'], + 'JSON scalar' => ['application/json', '12345'], + ]; + } + private function createApi(HttpClient $client): AbstractApi { return new class ($client) extends AbstractApi {};