Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Notifier] [Smsapi] fixed checking whether message is sent #42053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

Expand Down Expand Up @@ -72,10 +73,14 @@ protected function doSend(MessageInterface $message): SentMessage
throw new TransportException('Could not reach the remote Smsapi server.', $response, 0, $e);
}

if (200 !== $statusCode) {
$error = $response->toArray(false);
try {
$content = $response->toArray(false);
} catch (DecodingExceptionInterface $e) {
throw new TransportException('Could not decode body to an array.', $response, 0, $e);
}

throw new TransportException(sprintf('Unable to send the SMS: "%s".', $error['message']), $response);
if ((isset($content['error']) && null !== $content['error']) || (200 !== $statusCode)) {
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $content['message'] ?? 'unknown error'), $response);
}

return new SentMessage($message, (string) $this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace Symfony\Component\Notifier\Bridge\Smsapi\Tests;

use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransport;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SmsMessage;
Expand Down Expand Up @@ -44,4 +47,40 @@ public function unsupportedMessagesProvider(): iterable
yield [new ChatMessage('Hello!')];
yield [$this->createMock(MessageInterface::class)];
}

public function createClient(int $statusCode, string $content): HttpClientInterface
{
return new MockHttpClient(new MockResponse($content, ['http_code' => $statusCode]));
}

public function responseProvider(): iterable
{
$responses = [
['status' => 200, 'content' => '{"error":101,"message":"Authorization failed"}', 'errorMessage' => 'Unable to send the SMS: "Authorization failed".'],
['status' => 500, 'content' => '{}', 'errorMessage' => 'Unable to send the SMS: "unknown error".'],
fabpot marked this conversation as resolved.
Show resolved Hide resolved
['status' => 500, 'content' => '{"error":null,"message":"Unknown"}', 'errorMessage' => 'Unable to send the SMS: "Unknown".'],
['status' => 500, 'content' => '{"error":null,"message":null}', 'errorMessage' => 'Unable to send the SMS: "unknown error".'],
['status' => 500, 'content' => 'Internal error', 'errorMessage' => 'Could not decode body to an array.'],
['status' => 200, 'content' => 'Internal error', 'errorMessage' => 'Could not decode body to an array.'],
];

foreach ($responses as $response) {
yield [$response['status'], $response['content'], $response['errorMessage']];
}
}

/**
* @dataProvider responseProvider
*/
public function testThrowExceptionWhenMessageWasNotSent(int $statusCode, string $content, string $errorMessage)
{
$client = $this->createClient($statusCode, $content);
$transport = $this->createTransport($client);
$message = new SmsMessage('0611223344', 'Hello!');

$this->expectException(TransportException::class);
OskarStark marked this conversation as resolved.
Show resolved Hide resolved
$this->expectExceptionMessage($errorMessage);

$transport->send($message);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.