Skip to content

Commit 4191647

Browse files
phungthuan99Art4
andauthored
Fix $lastResponseContentType of type string (#291)
* Fix $lastResponseContentType of type string Fatal error: Uncaught TypeError: Cannot assign int to property Redmine\Client\NativeCurlClient::$lastResponseContentType of type string in D:\xampp-v8\htdocs\wordpress\wp-content\plugins\wordpress-cloodo-plugins\libs\Redmine\Client\NativeCurlClient.php:235 Stack trace: #0 D:\xampp-v8\htdocs\wordpress\wp-content\plugins\wordpress-cloodo-plugins\libs\Redmine\Client\NativeCurlClient.php(81): Redmine\Client\NativeCurlClient->request('get', '/users/current....') #1 D:\xampp-v8\htdocs\wordpress\wp-content\plugins\wordpress-cloodo-plugins\libs\Redmine\Api\AbstractApi.php(49): Redmine\Client\NativeCurlClient->requestGet('/users/current....') #2 D:\xampp-v8\htdocs\wordpress\wp-content\plugins\wordpress-cloodo-plugins\libs\Redmine\Api\User.php(121): Redmine\Api\AbstractApi->get('/users/current....') #3 D:\xampp-v8\htdocs\wordpress\wp-content\plugins\wordpress-cloodo-plugins\libs\Redmine\Api\User.php(66): Redmine\Api\User->show('current', Array) #4 D:\xampp-v8\htdocs\wordpress\wp-content\plugins\wordpress-cloodo-plugins\cloodo.php(27): Redmine\Api\User->getCurrentUser(Array) #5 D:\xampp-v8\htdocs\wordpress\wp-settings.php(391): include_once('D:\\xampp-v8\\htd...') #6 D:\xampp-v8\htdocs\wordpress\wp-config.php(90): require_once('D:\\xampp-v8\\htd...') #7 D:\xampp-v8\htdocs\wordpress\wp-load.php(37): require_once('D:\\xampp-v8\\htd...') #8 D:\xampp-v8\htdocs\wordpress\wp-admin\admin.php(34): require_once('D:\\xampp-v8\\htd...') #9 {main} thrown in D:\xampp-v8\htdocs\wordpress\wp-content\plugins\wordpress-cloodo-plugins\libs\Redmine\Client\NativeCurlClient.php on line 235 * Create test for handling of missing content type Co-authored-by: Art4 <art4@wlabs.de>
1 parent a9c0cda commit 4191647

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/Redmine/Client/NativeCurlClient.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,11 @@ private function request(string $method, string $path, string $body = ''): bool
232232

233233
$this->lastResponseBody = (false === $response) ? '' : $response;
234234
$this->lastResponseStatusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
235-
$this->lastResponseContentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
235+
$possibleContentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
236+
237+
if (is_string($possibleContentType)) {
238+
$this->lastResponseContentType = $possibleContentType;
239+
}
236240

237241
curl_close($curl);
238242

tests/Unit/Client/NativeCurlClientTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,48 @@ public function getRequestReponseData()
770770
];
771771
}
772772

773+
/**
774+
* @covers \Redmine\Client\NativeCurlClient
775+
* @test
776+
*/
777+
public function testHandlingOfResponseWithoutContent()
778+
{
779+
$content = '';
780+
$statusCode = 204;
781+
$contentType = null;
782+
783+
$curl = $this->createMock(stdClass::class);
784+
785+
$curlInit = $this->getFunctionMock(self::__NAMESPACE__, 'curl_init');
786+
$curlInit->expects($this->exactly(1))->willReturn($curl);
787+
788+
$curlExec = $this->getFunctionMock(self::__NAMESPACE__, 'curl_exec');
789+
$curlExec->expects($this->exactly(1))->willReturn('');
790+
791+
$curlSetoptArray = $this->getFunctionMock(self::__NAMESPACE__, 'curl_setopt_array');
792+
793+
$curlGetinfo = $this->getFunctionMock(self::__NAMESPACE__, 'curl_getinfo');
794+
$curlGetinfo->expects($this->exactly(2))->will($this->returnValueMap(([
795+
[$curl, CURLINFO_HTTP_CODE, $statusCode],
796+
[$curl, CURLINFO_CONTENT_TYPE, $contentType],
797+
])));
798+
799+
$curlErrno = $this->getFunctionMock(self::__NAMESPACE__, 'curl_errno');
800+
$curlErrno->expects($this->exactly(1))->willReturn(CURLE_OK);
801+
802+
$curlClose = $this->getFunctionMock(self::__NAMESPACE__, 'curl_close');
803+
804+
$client = new NativeCurlClient(
805+
'http://test.local',
806+
'access_token'
807+
);
808+
809+
$this->assertSame(true, $client->requestPut('/path', '{"foo":"bar"}'));
810+
$this->assertSame($statusCode, $client->getLastResponseStatusCode());
811+
$this->assertSame('', $client->getLastResponseContentType());
812+
$this->assertSame($content, $client->getLastResponseBody());
813+
}
814+
773815
/**
774816
* @covers \Redmine\NativeCurlClient
775817
* @test

0 commit comments

Comments
 (0)