Skip to content

Commit 1e91b1a

Browse files
committed
bug #42053 [Notifier] [Smsapi] fixed checking whether message is sent (damlox)
This PR was merged into the 5.3 branch. Discussion ---------- [Notifier] [Smsapi] fixed checking whether message is sent | Q | A | ------------- | --- | Branch? | 5.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - SmsAPI always return 200 status code even if we enter incorrect token. The content is: `{"error":101,"message":"Authorization failed"}`. Commits ------- b663ea3 notifier smsapi - fixed checking whether message is sent
2 parents 0010a3c + b663ea3 commit 1e91b1a

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

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

+8-3
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\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

+39
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
}
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)