|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Notifier\Bridge\MessageMedia\Tests; |
| 13 | + |
| 14 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 15 | +use Symfony\Component\Notifier\Bridge\MessageMedia\MessageMediaTransport; |
| 16 | +use Symfony\Component\Notifier\Exception\TransportException; |
| 17 | +use Symfony\Component\Notifier\Exception\TransportExceptionInterface; |
| 18 | +use Symfony\Component\Notifier\Message\ChatMessage; |
| 19 | +use Symfony\Component\Notifier\Message\MessageInterface; |
| 20 | +use Symfony\Component\Notifier\Message\SmsMessage; |
| 21 | +use Symfony\Component\Notifier\Test\TransportTestCase; |
| 22 | +use Symfony\Component\Notifier\Transport\TransportInterface; |
| 23 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 24 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 25 | + |
| 26 | +final class MessageMediaTransportTest extends TransportTestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @return MessageMediaTransport |
| 30 | + */ |
| 31 | + public function createTransport(HttpClientInterface $client = null, string $from = null): TransportInterface |
| 32 | + { |
| 33 | + return new MessageMediaTransport('apiKey', 'apiSecret', $from, $client ?? $this->createMock(HttpClientInterface::class)); |
| 34 | + } |
| 35 | + |
| 36 | + public function toStringProvider(): iterable |
| 37 | + { |
| 38 | + yield ['messagemedia://api.messagemedia.com', $this->createTransport()]; |
| 39 | + yield ['messagemedia://api.messagemedia.com?from=TEST', $this->createTransport(null, 'TEST')]; |
| 40 | + } |
| 41 | + |
| 42 | + public function supportedMessagesProvider(): iterable |
| 43 | + { |
| 44 | + yield [new SmsMessage('0491570156', 'Hello!')]; |
| 45 | + } |
| 46 | + |
| 47 | + public function unsupportedMessagesProvider(): iterable |
| 48 | + { |
| 49 | + yield [new ChatMessage('Hello!')]; |
| 50 | + yield [$this->createMock(MessageInterface::class)]; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @dataProvider exceptionIsThrownWhenHttpSendFailedProvider |
| 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 exceptionIsThrownWhenHttpSendFailedProvider(): 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 | + } |
| 82 | +} |
0 commit comments