Skip to content

Commit

Permalink
Add success response check
Browse files Browse the repository at this point in the history
  • Loading branch information
nuradiyana committed Jul 26, 2023
1 parent 4d2df80 commit 118d729
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Inisiatif\WhatsappQontakPhp;

use Inisiatif\WhatsappQontakPhp\Exceptions\ClientSendingException;
use Webmozart\Assert\Assert;
use Http\Discovery\Psr18ClientDiscovery;
use Http\Client\Common\HttpMethodsClient;
Expand Down Expand Up @@ -59,17 +60,21 @@ public function send(string $templateId, string $channelId, Message $message): R
)
);

/** @var array $responseBody */
$responseBody = \json_decode((string) $response->getBody(), true);
if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
/** @var array $responseBody */
$responseBody = \json_decode((string) $response->getBody(), true);

Assert::keyExists($responseBody, 'data');
Assert::keyExists($responseBody, 'data');

/** @var array<string, string|int> $responseData */
$responseData = $responseBody['data'];
Assert::keyExists($responseData, 'id');
Assert::keyExists($responseData, 'name');
/** @var array<string, string|int> $responseData */
$responseData = $responseBody['data'];
Assert::keyExists($responseData, 'id');
Assert::keyExists($responseData, 'name');

return new Response((string) $responseData['id'], (string) $responseData['name'], $responseData);
return new Response((string) $responseData['id'], (string) $responseData['name'], $responseData);
}

throw ClientSendingException::make($response);
}

private function getAccessToken(): void
Expand Down
18 changes: 18 additions & 0 deletions src/Exceptions/ClientSendingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Inisiatif\WhatsappQontakPhp\Exceptions;

use Exception;
use Psr\Http\Message\ResponseInterface;

final class ClientSendingException extends Exception
{
public static function make(ResponseInterface $response): self
{
$reason = (string) $response->getBody();

$code = $response->getStatusCode();

return new self($reason, $code);
}
}
27 changes: 27 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Inisiatif\WhatsappQontakPhp\Tests;

use Inisiatif\WhatsappQontakPhp\Exceptions\ClientSendingException;
use Mockery;
use PHPUnit\Framework\TestCase;
use Nyholm\Psr7\Factory\Psr17Factory;
Expand Down Expand Up @@ -32,6 +33,7 @@ public function test_send_message(): void
$mResponse->expects('getBody')->andReturn(
(new Psr17Factory())->createStream('{"access_token":"access-token","token_type":"bearer","expires_in":31556952,"refresh_token":"refresh-token","created_at":1663138844}')
);
$mResponse->expects('getStatusCode')->andReturn(200)->twice();
$mResponse->expects('getBody')->andReturn(
(new Psr17Factory())->createStream('{"status": "success", "data": { "id": "dataId", "name": "Foo Bar"}}')
);
Expand All @@ -46,4 +48,29 @@ public function test_send_message(): void
$this->assertSame('dataId', $response->getMessageId());
$this->assertSame(\json_decode('{ "id": "dataId", "name": "Foo Bar"}', true), $response->getData());
}

public function test_throw_exception_when_sending_message(): void
{
$this->expectException(ClientSendingException::class);

$credential = new Credential('username', 'password', 'clientId', 'secret');

$receiver = new Receiver('+628000000000', 'Foo bar');
$message = new Message($receiver);

$mResponse = Mockery::mock(ResponseInterface::class);
$mResponse->expects('getBody')->andReturn(
(new Psr17Factory())->createStream('{"access_token":"access-token","token_type":"bearer","expires_in":31556952,"refresh_token":"refresh-token","created_at":1663138844}')
);
$mResponse->expects('getStatusCode')->andReturn(422)->times(3);
$mResponse->expects('getBody')->andReturn(
(new Psr17Factory())->createStream('{"status":"error","error":{"code":422,"messages":["Message Template has 4 variable, please adjust your payload."]}} ')
);

$httpClient = Mockery::mock(HttpMethodsClientInterface::class)->makePartial();
$httpClient->expects('post')->andReturn($mResponse)->twice();

$client = new Client($credential, $httpClient);
$client->send('templateId', 'channelId', $message);
}
}

0 comments on commit 118d729

Please sign in to comment.