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 9f3e345

Browse filesBrowse files
author
Adrian Nguyen
committed
[Notifier] Add MessageMedia Bridge
handle exception & add more tests
1 parent 2aeb640 commit 9f3e345
Copy full SHA for 9f3e345

File tree

Expand file treeCollapse file tree

2 files changed

+43
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+43
-2
lines changed

‎src/Symfony/Component/Notifier/Bridge/MessageMedia/MessageMediaTransport.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/MessageMedia/MessageMediaTransport.php
+9-2Lines changed: 9 additions & 2 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\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
2122
use Symfony\Contracts\HttpClient\HttpClientInterface;
2223

2324
/**
@@ -86,8 +87,14 @@ protected function doSend(MessageInterface $message): SentMessage
8687
return $sentMessage;
8788
}
8889

89-
$error = $response->toArray(false);
90+
try {
91+
$error = $response->toArray(false);
9092

91-
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $error['details'][0] ?? $error['message']), $response);
93+
$errorMessage = $error['details'][0] ?? ($error['message'] ?? 'Unknown reason');
94+
} catch (DecodingExceptionInterface $e) {
95+
$errorMessage = 'Unknown reason';
96+
}
97+
98+
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $errorMessage), $response);
9299
}
93100
}

‎src/Symfony/Component/Notifier/Bridge/MessageMedia/Tests/MessageMediaTransportTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/MessageMedia/Tests/MessageMediaTransportTest.php
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111

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

14+
use Symfony\Component\HttpClient\MockHttpClient;
1415
use Symfony\Component\Notifier\Bridge\MessageMedia\MessageMediaTransport;
16+
use Symfony\Component\Notifier\Exception\TransportException;
17+
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
1518
use Symfony\Component\Notifier\Message\ChatMessage;
1619
use Symfony\Component\Notifier\Message\MessageInterface;
1720
use Symfony\Component\Notifier\Message\SmsMessage;
1821
use Symfony\Component\Notifier\Test\TransportTestCase;
1922
use Symfony\Component\Notifier\Transport\TransportInterface;
2023
use Symfony\Contracts\HttpClient\HttpClientInterface;
24+
use Symfony\Contracts\HttpClient\ResponseInterface;
2125

2226
final class MessageMediaTransportTest extends TransportTestCase
2327
{
@@ -45,4 +49,34 @@ public function unsupportedMessagesProvider(): iterable
4549
yield [new ChatMessage('Hello!')];
4650
yield [$this->createMock(MessageInterface::class)];
4751
}
52+
53+
/**
54+
* @dataProvider testExceptionIsThrownWhenHttpSendFailedProvider
55+
*
56+
* @throws TransportExceptionInterface
57+
*/
58+
public function testExceptionIsThrownWhenHttpSendFailed(int $statusCode, string $content, string $expectedExceptionMessage)
59+
{
60+
$response = $this->createMock(ResponseInterface::class);
61+
$response->method('getStatusCode')
62+
->willReturn($statusCode);
63+
$response->method('getContent')
64+
->willReturn($content);
65+
66+
$client = new MockHttpClient($response);
67+
68+
$transport = new MessageMediaTransport('apiKey', 'apiSecret', null, $client);
69+
$this->expectException(TransportException::class);
70+
$this->expectExceptionMessage($expectedExceptionMessage);
71+
72+
$transport->send(new SmsMessage('+61491570156', 'Hello!'));
73+
}
74+
75+
public function testExceptionIsThrownWhenHttpSendFailedProvider(): iterable
76+
{
77+
yield [503, '', 'Unable to send the SMS: "Unknown reason".'];
78+
yield [500, '{"details": ["Something went wrong."]}', 'Unable to send the SMS: "Something went wrong.".'];
79+
yield [403, '{"message": "Forbidden."}', 'Unable to send the SMS: "Forbidden.'];
80+
yield [401, '{"Unauthenticated"}', 'Unable to send the SMS: "Unknown reason".'];
81+
}
4882
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.