From 42354bd9063f5e92e0d63eca4a4241888563d851 Mon Sep 17 00:00:00 2001 From: Mohammed Elhaouari Date: Tue, 24 Feb 2026 02:25:44 +0100 Subject: [PATCH 1/2] WIP --- src/WatiClient.php | 5 ++--- src/WatiResponse.php | 24 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/WatiClient.php b/src/WatiClient.php index 2a231a2..bf89388 100644 --- a/src/WatiClient.php +++ b/src/WatiClient.php @@ -10,7 +10,6 @@ use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\ServerException; use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; use Wati\Http\Exceptions\AuthenticationException; use Wati\Http\Exceptions\RateLimitException; use Wati\Http\Exceptions\ValidationException; @@ -58,7 +57,7 @@ public function __construct( * @throws WatiApiException * @throws WatiException */ - public function send(RequestInterface $request): ResponseInterface + public function send(RequestInterface $request): WatiResponse { $request = $this->normalizeRequestPath($request); @@ -70,7 +69,7 @@ public function send(RequestInterface $request): ResponseInterface $request = $this->injectSdkHeaders($request); try { - return $this->client->send($request); + return WatiResponse::fromResponse($this->client->send($request)); } catch (ClientException $e) { $response = $e->getResponse(); $statusCode = $response->getStatusCode(); diff --git a/src/WatiResponse.php b/src/WatiResponse.php index bb7766e..d0f4443 100644 --- a/src/WatiResponse.php +++ b/src/WatiResponse.php @@ -7,4 +7,26 @@ use GuzzleHttp\Psr7\Response; use Psr\Http\Message\ResponseInterface; -abstract class WatiResponse extends Response implements ResponseInterface {} +final class WatiResponse extends Response implements ResponseInterface +{ + public static function fromResponse(ResponseInterface $response): self + { + return new self( + $response->getStatusCode(), + $response->getHeaders(), + $response->getBody(), + $response->getProtocolVersion(), + $response->getReasonPhrase() + ); + } + + public function json(): array + { + return json_decode($this->getBody()->getContents(), true); + } + + public function isSuccessful(): bool + { + return ($this->json()['result'] ?? null) === 'success'; + } +} From e50c33045ccd77a0d214600847229a7d34078603 Mon Sep 17 00:00:00 2001 From: Mohammed Elhaouari Date: Tue, 24 Feb 2026 02:37:55 +0100 Subject: [PATCH 2/2] Add tests to wati response --- changelog.md | 4 +++ src/WatiResponse.php | 12 +++++-- tests/WatiResponseTest.php | 67 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 tests/WatiResponseTest.php diff --git a/changelog.md b/changelog.md index ac7b251..cfdf952 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,10 @@ All notable changes to `phpjuice/wati-http-client` will be documented in this file. +## 1.0.4 + +- Add tests for `WatiResponse` + ## 1.0.3 - Add `WatiResponse` - Base response class diff --git a/src/WatiResponse.php b/src/WatiResponse.php index d0f4443..82cf2be 100644 --- a/src/WatiResponse.php +++ b/src/WatiResponse.php @@ -19,14 +19,20 @@ public static function fromResponse(ResponseInterface $response): self $response->getReasonPhrase() ); } - - public function json(): array + + public function json(): mixed { return json_decode($this->getBody()->getContents(), true); } public function isSuccessful(): bool { - return ($this->json()['result'] ?? null) === 'success'; + $json = $this->json(); + + if (! is_array($json)) { + return false; + } + + return ($json['result'] ?? null) === 'success'; } } diff --git a/tests/WatiResponseTest.php b/tests/WatiResponseTest.php new file mode 100644 index 0000000..9f08bbf --- /dev/null +++ b/tests/WatiResponseTest.php @@ -0,0 +1,67 @@ + 'application/json'], '{"key":"value"}'); + $watiResponse = WatiResponse::fromResponse($response); + + expect($watiResponse->getStatusCode())->toBe(200) + ->and($watiResponse->getHeaderLine('Content-Type'))->toBe('application/json') + ->and($watiResponse->getBody()->getContents())->toBe('{"key":"value"}'); +}); + +it('can parse json body', function (): void { + $response = new Response(200, [], '{"name":"John","age":30}'); + $watiResponse = WatiResponse::fromResponse($response); + + expect($watiResponse->json())->toBe(['name' => 'John', 'age' => 30]); +}); + +it('returns null for invalid json', function (): void { + $response = new Response(200, [], 'not valid json'); + $watiResponse = WatiResponse::fromResponse($response); + + expect($watiResponse->json())->toBeNull(); +}); + +it('returns true when result is success', function (): void { + $response = new Response(200, [], '{"result":"success","data":[]}'); + $watiResponse = WatiResponse::fromResponse($response); + + expect($watiResponse->isSuccessful())->toBeTrue(); +}); + +it('returns false when result is not success', function (): void { + $response = new Response(200, [], '{"result":"error","message":"Failed"}'); + $watiResponse = WatiResponse::fromResponse($response); + + expect($watiResponse->isSuccessful())->toBeFalse(); +}); + +it('returns false when result key is missing', function (): void { + $response = new Response(200, [], '{"status":"ok","data":[]}'); + $watiResponse = WatiResponse::fromResponse($response); + + expect($watiResponse->isSuccessful())->toBeFalse(); +}); + +it('returns false for non array json', function (): void { + $response = new Response(200, [], '"just a string"'); + $watiResponse = WatiResponse::fromResponse($response); + + expect($watiResponse->isSuccessful())->toBeFalse(); +}); + +it('preserves protocol version and reason phrase', function (): void { + $response = new Response(404, [], 'Not Found', '1.1', 'Not Found'); + $watiResponse = WatiResponse::fromResponse($response); + + expect($watiResponse->getProtocolVersion())->toBe('1.1') + ->and($watiResponse->getReasonPhrase())->toBe('Not Found'); +});