8000 notifier smsapi - fixed checking whether message is sent · symfony/symfony@b663ea3 · GitHub
[go: up one dir, main page]

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

Commit b663ea3

Browse files
committed
notifier smsapi - fixed checking whether message is sent
1 parent e39ee06 commit b663ea3

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/Symfony/Component/Notifier/Bridge/Smsapi/SmsapiTransport.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Notifier\Message\SmsMessage;
1919
use Symfony\Component\Notifier\Transport\AbstractTransport;
2020
use Symfony\Contracts 10000 \EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
2122
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2223
use Symfony\Contracts\HttpClient\HttpClientInterface;
2324

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

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

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

8186
return new SentMessage($message, (string) $this);

src/Symfony/Component/Notifier/Bridge/Smsapi/Tests/SmsapiTransportTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

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

14+
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\HttpClient\Response\MockResponse;
1416
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransport;
17+
use Symfony\Component\Notifier\Exception\TransportException;
1518
use Symfony\Component\Notifier\Message\ChatMessage;
1619
use Symfony\Component\Notifier\Message\MessageInterface;
1720
use Symfony\Component\Notifier\Message\SmsMessage;
@@ -44,4 +47,40 @@ public function unsupportedMessagesProvider(): iterable
4447
yield [new ChatMessage('Hello!')];
4548
yield [$this->createMock(MessageInterface::class)];
4649
}
10BC0 50+
51+
public function createClient(int $statusCode, string $content): HttpClientInterface
52+
{
53+
return new MockHttpClient(new MockResponse($content, ['http_code' => $statusCode]));
54+
}
55+
56+
public function responseProvider(): iterable
57+
{
58+
$responses = [
59+
['status' => 200, 'content' => '{"error":101,"message":"Authorization failed"}', 'errorMessage' => 'Unable to send the SMS: "Authorization failed".'],
60+
['status' => 500, 'content' => '{}', 'errorMessage' => 'Unable to send the SMS: "unknown error".'],
61+
['status' => 500, 'content' => '{"error":null,"message":"Unknown"}', 'errorMessage' => 'Unable to send the SMS: "Unknown".'],
62+
['status' => 500, 'content' => '{"error":null,"message":null}', 'errorMessage' => 'Unable to send the SMS: "unknown error".'],
63+
['status' => 500, 'content' => 'Internal error', 'errorMessage' => 'Could not decode body to an array.'],
64+
['status' => 200, 'content' => 'Internal error', 'errorMessage' => 'Could not decode body to an array.'],
65+
];
66+
67+
foreach ($responses as $response) {
68+
yield [$response['status'], $response['content'], $response['errorMessage']];
69+
}
70+
}
71+
72+
/**
73+
* @dataProvider responseProvider
74+
*/
75+
public function testThrowExceptionWhenMessageWasNotSent(int $statusCode, string $content, string $errorMessage)
76+
{
77+
$client = $this->createClient($statusCode, $content);
78+
$transport = $this->createTransport($client);
79+
$message = new SmsMessage('0611223344', 'Hello!');
80+
81+
$this->expectException(TransportException::class);
82+
$this->expectExceptionMessage($errorMessage);
83+
84+
$transport->send($message);
85+
}
4786
}

0 commit comments

Comments
 (0)
0