From 7e2613565b725027be753c2153ba158dc681492c Mon Sep 17 00:00:00 2001 From: Art4 Date: Tue, 8 Oct 2024 16:38:53 +0200 Subject: [PATCH 1/5] Deprecate getLastResponse... methods in Client classes --- src/Redmine/Client/Client.php | 9 +++ src/Redmine/Client/NativeCurlClient.php | 15 ++++ src/Redmine/Client/Psr18Client.php | 15 ++++ tests/Unit/Client/NativeCurlClientTest.php | 72 +++++++++++++++++++ tests/Unit/Client/Psr18ClientTest.php | 81 ++++++++++++++++++++++ 5 files changed, 192 insertions(+) diff --git a/src/Redmine/Client/Client.php b/src/Redmine/Client/Client.php index 610b5234..5b0f4759 100644 --- a/src/Redmine/Client/Client.php +++ b/src/Redmine/Client/Client.php @@ -48,16 +48,25 @@ public function requestDelete(string $path): bool; /** * Returns status code of the last response. + * + * @deprecated v2.8.0 Use `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @see \Redmine\Api\AbstractApi::getLastResponse() */ public function getLastResponseStatusCode(): int; /** * Returns content type of the last response. + * + * @deprecated v2.8.0 Use `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @see \Redmine\Api\AbstractApi::getLastResponse() */ public function getLastResponseContentType(): string; /** * Returns the body of the last response. + * + * @deprecated v2.8.0 Use `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @see \Redmine\Api\AbstractApi::getLastResponse() */ public function getLastResponseBody(): string; } diff --git a/src/Redmine/Client/NativeCurlClient.php b/src/Redmine/Client/NativeCurlClient.php index ccabb429..34c17e03 100644 --- a/src/Redmine/Client/NativeCurlClient.php +++ b/src/Redmine/Client/NativeCurlClient.php @@ -143,25 +143,40 @@ public function requestDelete(string $path): bool /** * Returns status code of the last response. + * + * @deprecated v2.8.0 Use `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @see \Redmine\Api\AbstractApi::getLastResponse() */ public function getLastResponseStatusCode(): int { + @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', E_USER_DEPRECATED); + return $this->lastResponseStatusCode; } /** * Returns content type of the last response. + * + * @deprecated v2.8.0 Use `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @see \Redmine\Api\AbstractApi::getLastResponse() */ public function getLastResponseContentType(): string { + @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', E_USER_DEPRECATED); + return $this->lastResponseContentType; } /** * Returns the body of the last response. + * + * @deprecated v2.8.0 Use `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @see \Redmine\Api\AbstractApi::getLastResponse() */ public function getLastResponseBody(): string { + @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', E_USER_DEPRECATED); + return $this->lastResponseBody; } diff --git a/src/Redmine/Client/Psr18Client.php b/src/Redmine/Client/Psr18Client.php index a62d415f..135f04b1 100644 --- a/src/Redmine/Client/Psr18Client.php +++ b/src/Redmine/Client/Psr18Client.php @@ -173,9 +173,14 @@ public function requestDelete(string $path): bool /** * Returns status code of the last response. + * + * @deprecated v2.8.0 Use `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @see \Redmine\Api\AbstractApi::getLastResponse() */ public function getLastResponseStatusCode(): int { + @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', E_USER_DEPRECATED); + if (null === $this->lastResponse) { return 0; } @@ -185,9 +190,14 @@ public function getLastResponseStatusCode(): int /** * Returns content type of the last response. + * + * @deprecated v2.8.0 Use `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @see \Redmine\Api\AbstractApi::getLastResponse() */ public function getLastResponseContentType(): string { + @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', E_USER_DEPRECATED); + if (null === $this->lastResponse) { return ''; } @@ -197,9 +207,14 @@ public function getLastResponseContentType(): string /** * Returns the body of the last response. + * + * @deprecated v2.8.0 Use `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @see \Redmine\Api\AbstractApi::getLastResponse() */ public function getLastResponseBody(): string { + @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', E_USER_DEPRECATED); + if (null === $this->lastResponse) { return ''; } diff --git a/tests/Unit/Client/NativeCurlClientTest.php b/tests/Unit/Client/NativeCurlClientTest.php index 1f4c234d..7ca05d70 100644 --- a/tests/Unit/Client/NativeCurlClientTest.php +++ b/tests/Unit/Client/NativeCurlClientTest.php @@ -69,6 +69,30 @@ public function testGetLastResponseStatusCodeIsInitialNull(): void $this->assertSame(0, $client->getLastResponseStatusCode()); } + public function testGetLastResponseStatusCodeTriggersDeprecationWarning(): void + { + $client = new NativeCurlClient( + 'http://test.local', + 'access_token', + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + '`Redmine\Client\NativeCurlClient::getLastResponseStatusCode()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', + $errstr, + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED, + ); + + $client->getLastResponseStatusCode(); + } + public function testGetLastResponseContentTypeIsInitialEmpty(): void { $client = new NativeCurlClient( @@ -79,6 +103,30 @@ public function testGetLastResponseContentTypeIsInitialEmpty(): void $this->assertSame('', $client->getLastResponseContentType()); } + public function testGetLastResponseContentTypeTriggersDeprecationWarning(): void + { + $client = new NativeCurlClient( + 'http://test.local', + 'access_token', + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + '`Redmine\Client\NativeCurlClient::getLastResponseContentType()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', + $errstr, + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED, + ); + + $client->getLastResponseContentType(); + } + public function testGetLastResponseBodyIsInitialEmpty(): void { $client = new NativeCurlClient( @@ -89,6 +137,30 @@ public function testGetLastResponseBodyIsInitialEmpty(): void $this->assertSame('', $client->getLastResponseBody()); } + public function testGetLastResponseBodyTriggersDeprecationWarning(): void + { + $client = new NativeCurlClient( + 'http://test.local', + 'access_token', + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + '`Redmine\Client\NativeCurlClient::getLastResponseBody()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', + $errstr, + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED, + ); + + $client->getLastResponseBody(); + } + public function testStartAndStopImpersonateUser(): void { $expectedOptions = [ diff --git a/tests/Unit/Client/Psr18ClientTest.php b/tests/Unit/Client/Psr18ClientTest.php index 3f5b6592..84642145 100644 --- a/tests/Unit/Client/Psr18ClientTest.php +++ b/tests/Unit/Client/Psr18ClientTest.php @@ -90,6 +90,33 @@ public function testGetLastResponseStatusCodeIsInitialZero(): void $this->assertSame(0, $client->getLastResponseStatusCode()); } + public function testGetLastResponseStatusCodeTriggersDeprecationWarning(): void + { + $client = new Psr18Client( + $this->createMock(ClientInterface::class), + $this->createMock(RequestFactoryInterface::class), + $this->createMock(StreamFactoryInterface::class), + 'http://test.local', + 'access_token', + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + '`Redmine\Client\Psr18Client::getLastResponseStatusCode()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', + $errstr, + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED, + ); + + $client->getLastResponseStatusCode(); + } + public function testGetLastResponseContentTypeIsInitialEmpty(): void { $client = new Psr18Client( @@ -103,6 +130,33 @@ public function testGetLastResponseContentTypeIsInitialEmpty(): void $this->assertSame('', $client->getLastResponseContentType()); } + public function testGetLastResponseContentTypeTriggersDeprecationWarning(): void + { + $client = new Psr18Client( + $this->createMock(ClientInterface::class), + $this->createMock(RequestFactoryInterface::class), + $this->createMock(StreamFactoryInterface::class), + 'http://test.local', + 'access_token', + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + '`Redmine\Client\Psr18Client::getLastResponseContentType()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', + $errstr, + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED, + ); + + $client->getLastResponseContentType(); + } + public function testGetLastResponseBodyIsInitialEmpty(): void { $client = new Psr18Client( @@ -116,6 +170,33 @@ public function testGetLastResponseBodyIsInitialEmpty(): void $this->assertSame('', $client->getLastResponseBody()); } + public function testGetLastResponseBodyTriggersDeprecationWarning(): void + { + $client = new Psr18Client( + $this->createMock(ClientInterface::class), + $this->createMock(RequestFactoryInterface::class), + $this->createMock(StreamFactoryInterface::class), + 'http://test.local', + 'access_token', + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + '`Redmine\Client\Psr18Client::getLastResponseBody()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', + $errstr, + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED, + ); + + $client->getLastResponseBody(); + } + public function testStartAndStopImpersonateUser(): void { $request = $this->createMock(RequestInterface::class); From cd1830b234627470e256a3c1d6fc510659dc8b13 Mon Sep 17 00:00:00 2001 From: Art4 Date: Tue, 8 Oct 2024 16:41:32 +0200 Subject: [PATCH 2/5] Update CHANGELOG.md --- CHANGELOG.md | 6 ++++++ composer.json | 1 + 2 files changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6de5dc3..1e6d18bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/kbsali/php-redmine-api/compare/v2.7.0...v2.x) +### Deprecated + +- `Redmine\Client\Client::getLastResponseStatusCode()` is deprecated, use `\Redmine\Api\AbstractApi::getLastResponse()->getStatusCode()` instead. +- `Redmine\Client\Client::getLastResponseContentType()` is deprecated, use `\Redmine\Api\AbstractApi::getLastResponse()->getContentType()` instead. +- `Redmine\Client\Client::getLastResponseBody()` is deprecated, use `\Redmine\Api\AbstractApi::getLastResponse()->getContent()` instead. + ## [v2.7.0](https://github.com/kbsali/php-redmine-api/compare/v2.6.0...v2.7.0) - 2024-07-10 ### Added diff --git a/composer.json b/composer.json index f90b7334..55d10390 100644 --- a/composer.json +++ b/composer.json @@ -51,6 +51,7 @@ }, "scripts": { "bdt": [ + "Composer\\Config::disableProcessTimeout", "@behat --format=progress --suite=redmine_50103", "@behat --format=progress --suite=redmine_50009", "@behat --format=progress --suite=redmine_40210" From fad7949452b1d4f5c2888f3f473ca6adc4a11975 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 10 Oct 2024 16:17:30 +0200 Subject: [PATCH 3/5] Update docs in README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 7d2b805b..58564f4e 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,8 @@ like [Guzzle](https://github.com/guzzle/guzzle) for handling http connections * [mid-level API](docs/usage.md#mid-level-api) e.g. ```php $client->getApi('issue')->create(['project_id' => 1, 'subject' => 'issue title']); + + $response = $client->getApi('issue')->getLastResponse(); ``` * [low-level API](docs/usage.md#low-level-api) e.g. ```php From 48d350b4d106eb0ed4e4f4126d1170a815b32df5 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 10 Oct 2024 17:00:25 +0200 Subject: [PATCH 4/5] Deprecate request...() methods in all clients --- CHANGELOG.md | 10 +- src/Redmine/Client/Client.php | 21 ++- src/Redmine/Client/NativeCurlClient.php | 35 +++- src/Redmine/Client/Psr18Client.php | 20 +++ tests/Unit/Client/NativeCurlClientTest.php | 183 ++++++++++++++++++++- tests/Unit/Client/Psr18ClientTest.php | 137 +++++++++++++++ 6 files changed, 391 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03892e45..263a6661 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,9 +13,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Deprecated -- `Redmine\Client\Client::getLastResponseStatusCode()` is deprecated, use `\Redmine\Api\AbstractApi::getLastResponse()->getStatusCode()` instead. -- `Redmine\Client\Client::getLastResponseContentType()` is deprecated, use `\Redmine\Api\AbstractApi::getLastResponse()->getContentType()` instead. -- `Redmine\Client\Client::getLastResponseBody()` is deprecated, use `\Redmine\Api\AbstractApi::getLastResponse()->getContent()` instead. +- `Redmine\Client\Client::requestGet()` is deprecated, use `\Redmine\Client\Client::request()` instead. +- `Redmine\Client\Client::requestPost()` is deprecated, use `\Redmine\Client\Client::request()` instead. +- `Redmine\Client\Client::requestPut()` is deprecated, use `\Redmine\Client\Client::request()` instead. +- `Redmine\Client\Client::requestDelete()` is deprecated, use `\Redmine\Client\Client::request()` instead. +- `Redmine\Client\Client::getLastResponseStatusCode()` is deprecated, use `\Redmine\Client\Client::request()` or `\Redmine\Api\AbstractApi::getLastResponse()->getStatusCode()` instead. +- `Redmine\Client\Client::getLastResponseContentType()` is deprecated, use `\Redmine\Client\Client::request()` or `\Redmine\Api\AbstractApi::getLastResponse()->getContentType()` instead. +- `Redmine\Client\Client::getLastResponseBody()` is deprecated, use `\Redmine\Client\Client::request()` or `\Redmine\Api\AbstractApi::getLastResponse()->getContent()` instead. ## [v2.7.0](https://github.com/kbsali/php-redmine-api/compare/v2.6.0...v2.7.0) - 2024-07-10 diff --git a/src/Redmine/Client/Client.php b/src/Redmine/Client/Client.php index 5b0f4759..0832ace1 100644 --- a/src/Redmine/Client/Client.php +++ b/src/Redmine/Client/Client.php @@ -28,28 +28,41 @@ public function stopImpersonateUser(): void; /** * Create and send a GET request. + * + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` instead + * @see \Redmine\Http\HttpClient::request() */ public function requestGet(string $path): bool; /** * Create and send a POST request. + * + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` instead + * @see \Redmine\Http\HttpClient::request() */ public function requestPost(string $path, string $body): bool; /** * Create and send a PUT request. + * + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` instead + * @see \Redmine\Http\HttpClient::request() */ public function requestPut(string $path, string $body): bool; /** * Create and send a DELETE request. + * + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` instead + * @see \Redmine\Http\HttpClient::request() */ public function requestDelete(string $path): bool; /** * Returns status code of the last response. * - * @deprecated v2.8.0 Use `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` or `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @see \Redmine\Http\HttpClient::request() * @see \Redmine\Api\AbstractApi::getLastResponse() */ public function getLastResponseStatusCode(): int; @@ -57,7 +70,8 @@ public function getLastResponseStatusCode(): int; /** * Returns content type of the last response. * - * @deprecated v2.8.0 Use `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` or `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @see \Redmine\Http\HttpClient::request() * @see \Redmine\Api\AbstractApi::getLastResponse() */ public function getLastResponseContentType(): string; @@ -65,7 +79,8 @@ public function getLastResponseContentType(): string; /** * Returns the body of the last response. * - * @deprecated v2.8.0 Use `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` or `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @see \Redmine\Http\HttpClient::request() * @see \Redmine\Api\AbstractApi::getLastResponse() */ public function getLastResponseBody(): string; diff --git a/src/Redmine/Client/NativeCurlClient.php b/src/Redmine/Client/NativeCurlClient.php index 34c17e03..6dfb2c06 100644 --- a/src/Redmine/Client/NativeCurlClient.php +++ b/src/Redmine/Client/NativeCurlClient.php @@ -111,45 +111,66 @@ public function stopImpersonateUser(): void /** * Create and send a GET request. + * + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` instead + * @see \Redmine\Http\HttpClient::request() */ public function requestGet(string $path): bool { + @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Client\NativeCurlClient::request()` instead.', E_USER_DEPRECATED); + return $this->runRequest('GET', $path); } /** * Create and send a POST request. + * + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` instead + * @see \Redmine\Http\HttpClient::request() */ public function requestPost(string $path, string $body): bool { + @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Client\NativeCurlClient::request()` instead.', E_USER_DEPRECATED); + return $this->runRequest('POST', $path, $body); } /** * Create and send a PUT request. + * + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` instead + * @see \Redmine\Http\HttpClient::request() */ public function requestPut(string $path, string $body): bool { + @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Client\NativeCurlClient::request()` instead.', E_USER_DEPRECATED); + return $this->runRequest('PUT', $path, $body); } /** * Create and send a DELETE request. + * + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` instead + * @see \Redmine\Http\HttpClient::request() */ public function requestDelete(string $path): bool { + @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Client\NativeCurlClient::request()` instead.', E_USER_DEPRECATED); + return $this->runRequest('DELETE', $path); } /** * Returns status code of the last response. * - * @deprecated v2.8.0 Use `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` or `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @see \Redmine\Http\HttpClient::request() * @see \Redmine\Api\AbstractApi::getLastResponse() */ public function getLastResponseStatusCode(): int { - @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', E_USER_DEPRECATED); + @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Client\NativeCurlClient::request()` or `\Redmine\Api\AbstractApi::getLastResponse()` instead.', E_USER_DEPRECATED); return $this->lastResponseStatusCode; } @@ -157,12 +178,13 @@ public function getLastResponseStatusCode(): int /** * Returns content type of the last response. * - * @deprecated v2.8.0 Use `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` or `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @see \Redmine\Http\HttpClient::request() * @see \Redmine\Api\AbstractApi::getLastResponse() */ public function getLastResponseContentType(): string { - @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', E_USER_DEPRECATED); + @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Client\NativeCurlClient::request()` or `\Redmine\Api\AbstractApi::getLastResponse()` instead.', E_USER_DEPRECATED); return $this->lastResponseContentType; } @@ -170,12 +192,13 @@ public function getLastResponseContentType(): string /** * Returns the body of the last response. * - * @deprecated v2.8.0 Use `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` or `\Redmine\Api\AbstractApi::getLastResponse()` instead + * @see \Redmine\Http\HttpClient::request() * @see \Redmine\Api\AbstractApi::getLastResponse() */ public function getLastResponseBody(): string { - @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', E_USER_DEPRECATED); + @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Client\NativeCurlClient::request()` or `\Redmine\Api\AbstractApi::getLastResponse()` instead.', E_USER_DEPRECATED); return $this->lastResponseBody; } diff --git a/src/Redmine/Client/Psr18Client.php b/src/Redmine/Client/Psr18Client.php index 135f04b1..d0c0da17 100644 --- a/src/Redmine/Client/Psr18Client.php +++ b/src/Redmine/Client/Psr18Client.php @@ -118,12 +118,17 @@ public function stopImpersonateUser(): void /** * Create and send a GET request. * + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` instead + * @see \Redmine\Http\HttpClient::request() + * * @throws ClientException If anything goes wrong on the request * * @return bool true if status code of the response is not 4xx oder 5xx */ public function requestGet(string $path): bool { + @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Client\Psr18Client::request()` instead.', E_USER_DEPRECATED); + $response = $this->runRequest('GET', $path); return $response->getStatusCode() < 400; @@ -132,12 +137,17 @@ public function requestGet(string $path): bool /** * Create and send a POST request. * + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` instead + * @see \Redmine\Http\HttpClient::request() + * * @throws ClientException If anything goes wrong on the request * * @return bool true if status code of the response is not 4xx oder 5xx */ public function requestPost(string $path, string $body): bool { + @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Client\Psr18Client::request()` instead.', E_USER_DEPRECATED); + $response = $this->runRequest('POST', $path, $body); return $response->getStatusCode() < 400; @@ -146,12 +156,17 @@ public function requestPost(string $path, string $body): bool /** * Create and send a PUT request. * + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` instead + * @see \Redmine\Http\HttpClient::request() + * * @throws ClientException If anything goes wrong on the request * * @return bool true if status code of the response is not 4xx oder 5xx */ public function requestPut(string $path, string $body): bool { + @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Client\Psr18Client::request()` instead.', E_USER_DEPRECATED); + $response = $this->runRequest('PUT', $path, $body); return $response->getStatusCode() < 400; @@ -160,12 +175,17 @@ public function requestPut(string $path, string $body): bool /** * Create and send a DELETE request. * + * @deprecated v2.8.0 Use `\Redmine\Http\HttpClient::request()` instead + * @see \Redmine\Http\HttpClient::request() + * * @throws ClientException If anything goes wrong on the request * * @return bool true if status code of the response is not 4xx oder 5xx */ public function requestDelete(string $path): bool { + @trigger_error('`' . __METHOD__ . '()` is deprecated since v2.8.0, use `\Redmine\Client\Psr18Client::request()` instead.', E_USER_DEPRECATED); + $response = $this->runRequest('DELETE', $path); return $response->getStatusCode() < 400; diff --git a/tests/Unit/Client/NativeCurlClientTest.php b/tests/Unit/Client/NativeCurlClientTest.php index 7ca05d70..29af5561 100644 --- a/tests/Unit/Client/NativeCurlClientTest.php +++ b/tests/Unit/Client/NativeCurlClientTest.php @@ -12,6 +12,7 @@ use PHPUnit\Framework\TestCase; use Redmine\Client\Client; use Redmine\Client\NativeCurlClient; +use Redmine\Exception\ClientException; use Redmine\Http\HttpClient; use stdClass; @@ -80,7 +81,7 @@ public function testGetLastResponseStatusCodeTriggersDeprecationWarning(): void set_error_handler( function ($errno, $errstr): bool { $this->assertSame( - '`Redmine\Client\NativeCurlClient::getLastResponseStatusCode()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', + '`Redmine\Client\NativeCurlClient::getLastResponseStatusCode()` is deprecated since v2.8.0, use `\Redmine\Client\NativeCurlClient::request()` or `\Redmine\Api\AbstractApi::getLastResponse()` instead.', $errstr, ); @@ -114,7 +115,7 @@ public function testGetLastResponseContentTypeTriggersDeprecationWarning(): void set_error_handler( function ($errno, $errstr): bool { $this->assertSame( - '`Redmine\Client\NativeCurlClient::getLastResponseContentType()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', + '`Redmine\Client\NativeCurlClient::getLastResponseContentType()` is deprecated since v2.8.0, use `\Redmine\Client\NativeCurlClient::request()` or `\Redmine\Api\AbstractApi::getLastResponse()` instead.', $errstr, ); @@ -148,7 +149,7 @@ public function testGetLastResponseBodyTriggersDeprecationWarning(): void set_error_handler( function ($errno, $errstr): bool { $this->assertSame( - '`Redmine\Client\NativeCurlClient::getLastResponseBody()` is deprecated since v2.8.0, use `\Redmine\Api\AbstractApi::getLastResponse()` instead.', + '`Redmine\Client\NativeCurlClient::getLastResponseBody()` is deprecated since v2.8.0, use `\Redmine\Client\NativeCurlClient::request()` or `\Redmine\Api\AbstractApi::getLastResponse()` instead.', $errstr, ); @@ -723,6 +724,182 @@ public static function getRequestReponseData(): array ]; } + public function testRequestGetTriggersDeprecationWarning(): void + { + $curl = $this->createMock(stdClass::class); + + $curlInit = $this->getFunctionMock(self::__NAMESPACE__, 'curl_init'); + + $curlExec = $this->getFunctionMock(self::__NAMESPACE__, 'curl_exec'); + + $curlSetoptArray = $this->getFunctionMock(self::__NAMESPACE__, 'curl_setopt_array'); + + $curlGetinfo = $this->getFunctionMock(self::__NAMESPACE__, 'curl_getinfo'); + + $curlErrno = $this->getFunctionMock(self::__NAMESPACE__, 'curl_errno'); + $curlErrno->expects($this->exactly(1))->willReturn(CURLE_COULDNT_CONNECT); + + $curlError = $this->getFunctionMock(self::__NAMESPACE__, 'curl_error'); + $curlError->expects($this->exactly(1))->willReturn(''); + + $curlClose = $this->getFunctionMock(self::__NAMESPACE__, 'curl_close'); + + $client = new NativeCurlClient( + 'http://test.local', + 'access_token', + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + '`Redmine\Client\NativeCurlClient::requestGet()` is deprecated since v2.8.0, use `\Redmine\Client\NativeCurlClient::request()` instead.', + $errstr, + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED, + ); + + try { + $client->requestGet('/path'); + } catch (ClientException $th) {} + } + + public function testRequestPostTriggersDeprecationWarning(): void + { + $curl = $this->createMock(stdClass::class); + + $curlInit = $this->getFunctionMock(self::__NAMESPACE__, 'curl_init'); + + $curlExec = $this->getFunctionMock(self::__NAMESPACE__, 'curl_exec'); + + $curlSetoptArray = $this->getFunctionMock(self::__NAMESPACE__, 'curl_setopt_array'); + + $curlGetinfo = $this->getFunctionMock(self::__NAMESPACE__, 'curl_getinfo'); + + $curlErrno = $this->getFunctionMock(self::__NAMESPACE__, 'curl_errno'); + $curlErrno->expects($this->exactly(1))->willReturn(CURLE_COULDNT_CONNECT); + + $curlError = $this->getFunctionMock(self::__NAMESPACE__, 'curl_error'); + $curlError->expects($this->exactly(1))->willReturn(''); + + $curlClose = $this->getFunctionMock(self::__NAMESPACE__, 'curl_close'); + + $client = new NativeCurlClient( + 'http://test.local', + 'access_token', + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + '`Redmine\Client\NativeCurlClient::requestPost()` is deprecated since v2.8.0, use `\Redmine\Client\NativeCurlClient::request()` instead.', + $errstr, + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED, + ); + + try { + $client->requestPost('/path', ''); + } catch (ClientException $th) {} + } + + public function testRequestPutTriggersDeprecationWarning(): void + { + $curl = $this->createMock(stdClass::class); + + $curlInit = $this->getFunctionMock(self::__NAMESPACE__, 'curl_init'); + + $curlExec = $this->getFunctionMock(self::__NAMESPACE__, 'curl_exec'); + + $curlSetoptArray = $this->getFunctionMock(self::__NAMESPACE__, 'curl_setopt_array'); + + $curlGetinfo = $this->getFunctionMock(self::__NAMESPACE__, 'curl_getinfo'); + + $curlErrno = $this->getFunctionMock(self::__NAMESPACE__, 'curl_errno'); + $curlErrno->expects($this->exactly(1))->willReturn(CURLE_COULDNT_CONNECT); + + $curlError = $this->getFunctionMock(self::__NAMESPACE__, 'curl_error'); + $curlError->expects($this->exactly(1))->willReturn(''); + + $curlClose = $this->getFunctionMock(self::__NAMESPACE__, 'curl_close'); + + $client = new NativeCurlClient( + 'http://test.local', + 'access_token', + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + '`Redmine\Client\NativeCurlClient::requestPut()` is deprecated since v2.8.0, use `\Redmine\Client\NativeCurlClient::request()` instead.', + $errstr, + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED, + ); + + try { + $client->requestPut('/path', ''); + } catch (ClientException $th) {} + } + + public function testRequestDeleteTriggersDeprecationWarning(): void + { + $curl = $this->createMock(stdClass::class); + + $curlInit = $this->getFunctionMock(self::__NAMESPACE__, 'curl_init'); + + $curlExec = $this->getFunctionMock(self::__NAMESPACE__, 'curl_exec'); + + $curlSetoptArray = $this->getFunctionMock(self::__NAMESPACE__, 'curl_setopt_array'); + + $curlGetinfo = $this->getFunctionMock(self::__NAMESPACE__, 'curl_getinfo'); + + $curlErrno = $this->getFunctionMock(self::__NAMESPACE__, 'curl_errno'); + $curlErrno->expects($this->exactly(1))->willReturn(CURLE_COULDNT_CONNECT); + + $curlError = $this->getFunctionMock(self::__NAMESPACE__, 'curl_error'); + $curlError->expects($this->exactly(1))->willReturn(''); + + $curlClose = $this->getFunctionMock(self::__NAMESPACE__, 'curl_close'); + + $client = new NativeCurlClient( + 'http://test.local', + 'access_token', + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + '`Redmine\Client\NativeCurlClient::requestDelete()` is deprecated since v2.8.0, use `\Redmine\Client\NativeCurlClient::request()` instead.', + $errstr, + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED, + ); + + try { + $client->requestDelete('/path'); + } catch (ClientException $th) {} + } + public function testHandlingOfResponseWithoutContent(): void { $content = ''; diff --git a/tests/Unit/Client/Psr18ClientTest.php b/tests/Unit/Client/Psr18ClientTest.php index 84642145..5c9094a5 100644 --- a/tests/Unit/Client/Psr18ClientTest.php +++ b/tests/Unit/Client/Psr18ClientTest.php @@ -17,6 +17,7 @@ use Psr\Http\Message\StreamInterface; use Redmine\Client\Client; use Redmine\Client\Psr18Client; +use Redmine\Exception\ClientException; use Redmine\Http\HttpClient; use stdClass; @@ -316,6 +317,142 @@ public static function getRequestReponseData(): array ]; } + public function testRequestGetTriggersDeprecationWarning(): void + { + $requestFactory = $this->createMock(RequestFactoryInterface::class); + $requestFactory->method('createRequest')->willThrowException( + $this->createMock(ClientException::class), + ); + + $client = new Psr18Client( + $this->createMock(ClientInterface::class), + $requestFactory, + $this->createMock(StreamFactoryInterface::class), + 'http://test.local', + 'access_token', + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + '`Redmine\Client\Psr18Client::requestGet()` is deprecated since v2.8.0, use `\Redmine\Client\Psr18Client::request()` instead.', + $errstr, + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED, + ); + + try { + $client->requestGet('/path'); + } catch (ClientException $th) {} + } + + public function testRequestPostTriggersDeprecationWarning(): void + { + $requestFactory = $this->createMock(RequestFactoryInterface::class); + $requestFactory->method('createRequest')->willThrowException( + $this->createMock(ClientException::class), + ); + + $client = new Psr18Client( + $this->createMock(ClientInterface::class), + $requestFactory, + $this->createMock(StreamFactoryInterface::class), + 'http://test.local', + 'access_token', + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + '`Redmine\Client\Psr18Client::requestPost()` is deprecated since v2.8.0, use `\Redmine\Client\Psr18Client::request()` instead.', + $errstr, + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED, + ); + + try { + $client->requestPost('/path', ''); + } catch (ClientException $th) {} + } + + public function testRequestPutTriggersDeprecationWarning(): void + { + $requestFactory = $this->createMock(RequestFactoryInterface::class); + $requestFactory->method('createRequest')->willThrowException( + $this->createMock(ClientException::class), + ); + + $client = new Psr18Client( + $this->createMock(ClientInterface::class), + $requestFactory, + $this->createMock(StreamFactoryInterface::class), + 'http://test.local', + 'access_token', + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + '`Redmine\Client\Psr18Client::requestPut()` is deprecated since v2.8.0, use `\Redmine\Client\Psr18Client::request()` instead.', + $errstr, + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED, + ); + + try { + $client->requestPut('/path', ''); + } catch (ClientException $th) {} + } + + public function testRequestDeleteTriggersDeprecationWarning(): void + { + $requestFactory = $this->createMock(RequestFactoryInterface::class); + $requestFactory->method('createRequest')->willThrowException( + $this->createMock(ClientException::class), + ); + + $client = new Psr18Client( + $this->createMock(ClientInterface::class), + $requestFactory, + $this->createMock(StreamFactoryInterface::class), + 'http://test.local', + 'access_token', + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + '`Redmine\Client\Psr18Client::requestDelete()` is deprecated since v2.8.0, use `\Redmine\Client\Psr18Client::request()` instead.', + $errstr, + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED, + ); + + try { + $client->requestDelete('/path'); + } catch (ClientException $th) {} + } + /** * @dataProvider getApiClassesProvider */ From cf370a223dcde82ef84d8823867e2218b76db740 Mon Sep 17 00:00:00 2001 From: Art4 Date: Fri, 11 Oct 2024 11:28:56 +0200 Subject: [PATCH 5/5] Fix code style --- tests/Unit/Client/NativeCurlClientTest.php | 12 ++++++++---- tests/Unit/Client/Psr18ClientTest.php | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/Unit/Client/NativeCurlClientTest.php b/tests/Unit/Client/NativeCurlClientTest.php index 29af5561..c229db48 100644 --- a/tests/Unit/Client/NativeCurlClientTest.php +++ b/tests/Unit/Client/NativeCurlClientTest.php @@ -765,7 +765,8 @@ function ($errno, $errstr): bool { try { $client->requestGet('/path'); - } catch (ClientException $th) {} + } catch (ClientException $th) { + } } public function testRequestPostTriggersDeprecationWarning(): void @@ -809,7 +810,8 @@ function ($errno, $errstr): bool { try { $client->requestPost('/path', ''); - } catch (ClientException $th) {} + } catch (ClientException $th) { + } } public function testRequestPutTriggersDeprecationWarning(): void @@ -853,7 +855,8 @@ function ($errno, $errstr): bool { try { $client->requestPut('/path', ''); - } catch (ClientException $th) {} + } catch (ClientException $th) { + } } public function testRequestDeleteTriggersDeprecationWarning(): void @@ -897,7 +900,8 @@ function ($errno, $errstr): bool { try { $client->requestDelete('/path'); - } catch (ClientException $th) {} + } catch (ClientException $th) { + } } public function testHandlingOfResponseWithoutContent(): void diff --git a/tests/Unit/Client/Psr18ClientTest.php b/tests/Unit/Client/Psr18ClientTest.php index 5c9094a5..472cf458 100644 --- a/tests/Unit/Client/Psr18ClientTest.php +++ b/tests/Unit/Client/Psr18ClientTest.php @@ -348,7 +348,8 @@ function ($errno, $errstr): bool { try { $client->requestGet('/path'); - } catch (ClientException $th) {} + } catch (ClientException $th) { + } } public function testRequestPostTriggersDeprecationWarning(): void @@ -382,7 +383,8 @@ function ($errno, $errstr): bool { try { $client->requestPost('/path', ''); - } catch (ClientException $th) {} + } catch (ClientException $th) { + } } public function testRequestPutTriggersDeprecationWarning(): void @@ -416,7 +418,8 @@ function ($errno, $errstr): bool { try { $client->requestPut('/path', ''); - } catch (ClientException $th) {} + } catch (ClientException $th) { + } } public function testRequestDeleteTriggersDeprecationWarning(): void @@ -450,7 +453,8 @@ function ($errno, $errstr): bool { try { $client->requestDelete('/path'); - } catch (ClientException $th) {} + } catch (ClientException $th) { + } } /**