|
| 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\MicrosoftTeams\Tests; |
| 13 | + |
| 14 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 15 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 16 | +use Symfony\Component\Notifier\Bridge\MicrosoftTeams\MicrosoftTeamsOptions; |
| 17 | +use Symfony\Component\Notifier\Bridge\MicrosoftTeams\MicrosoftTeamsTransport; |
| 18 | +use Symfony\Component\Notifier\Exception\TransportException; |
| 19 | +use Symfony\Component\Notifier\Message\ChatMessage; |
| 20 | +use Symfony\Component\Notifier\Message\MessageInterface; |
| 21 | +use Symfony\Component\Notifier\Message\SmsMessage; |
| 22 | +use Symfony\Component\Notifier\Notification\Notification; |
| 23 | +use Symfony\Component\Notifier\Test\TransportTestCase; |
| 24 | +use Symfony\Component\Notifier\Transport\TransportInterface; |
| 25 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 26 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 27 | + |
| 28 | +final class MicrosoftTeamsTransportTest extends TransportTestCase |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @return MicrosoftTeamsTransport |
| 32 | + */ |
| 33 | + public function createTransport(?HttpClientInterface $client = null): TransportInterface |
| 34 | + { |
| 35 | + return (new MicrosoftTeamsTransport('/testPath', $client ?: $this->createMock(HttpClientInterface::class)))->setHost('host.test'); |
| 36 | + } |
| 37 | + |
| 38 | + public function toStringProvider(): iterable |
| 39 | + { |
| 40 | + yield ['microsoftteams://host.test/testPath', $this->createTransport()]; |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + public function supportedMessagesProvider(): iterable |
| 45 | + { |
| 46 | + yield [new ChatMessage('Hello!')]; |
| 47 | + } |
| 48 | + |
| 49 | + public function unsupportedMessagesProvider(): iterable |
| 50 | + { |
| 51 | + yield [new SmsMessage('0611223344', 'Hello!')]; |
| 52 | + yield [$this->createMock(MessageInterface::class)]; |
| 53 | + } |
| 54 | + |
| 55 | + public function testSendWithErrorResponseThrows() |
| 56 | + { |
| 57 | + $client = new MockHttpClient(function (string $method, string $url, array $options = []): ResponseInterface { |
| 58 | + return new MockResponse('testErrorMessage', ['response_headers' => ['request-id' => ['testRequestId']], 'http_code' => 400]); |
| 59 | + }); |
| 60 | + |
| 61 | + $transport = $this->createTransport($client); |
| 62 | + |
| 63 | + $this->expectException(TransportException::class); |
| 64 | + $this->expectExceptionMessageMatches('/testErrorMessage/'); |
| 65 | + |
| 66 | + $transport->send(new ChatMessage('testMessage')); |
| 67 | + } |
| 68 | + |
| 69 | + public function testSendWithErrorRequestIdThrows() |
| 70 | + { |
| 71 | + $client = new MockHttpClient(new MockResponse()); |
| 72 | + |
| 73 | + $transport = $this->createTransport($client); |
| 74 | + |
| 75 | + $this->expectException(TransportException::class); |
| 76 | + $this->expectExceptionMessageMatches('/request-id not found/'); |
| 77 | + |
| 78 | + $transport->send(new ChatMessage('testMessage')); |
| 79 | + } |
| 80 | + |
| 81 | + public function testSendWithOptions() |
| 82 | + { |
| 83 | + $message = 'testMessage'; |
| 84 | + |
| 85 | + $expectedBody = json_encode(['title' => $message]); |
| 86 | + |
| 87 | + $client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($expectedBody): ResponseInterface { |
| 88 | + $this->assertJsonStringEqualsJsonString($expectedBody, $options['body']); |
| 89 | + |
| 90 | + return new MockResponse('1', ['response_headers' => ['request-id' => ['testRequestId']], 'http_code' => 200]); |
| 91 | + }); |
| 92 | + |
| 93 | + $transport = $this->createTransport($client); |
| 94 | + |
| 95 | + $transport->send(new ChatMessage($message)); |
| 96 | + } |
| 97 | + |
| 98 | + public function testSendWithNotification() |
| 99 | + { |
| 100 | + $notification = new Notification('testMessage'); |
| 101 | + $chatMessage = ChatMessage::fromNotification($notification); |
| 102 | + $options = MicrosoftTeamsOptions::fromNotification($notification); |
| 103 | + |
| 104 | + $expectedBody = json_encode([ |
| 105 | + 'title' => $options->toArray()['title'], |
| 106 | + ]); |
| 107 | + |
| 108 | + $client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($expectedBody): ResponseInterface { |
| 109 | + $this->assertJsonStringEqualsJsonString($expectedBody, $options['body']); |
| 110 | + |
| 111 | + return new MockResponse('1', ['response_headers' => ['request-id' => ['testRequestId']], 'http_code' => 200]); |
| 112 | + }); |
| 113 | + |
| 114 | + $transport = $this->createTransport($client); |
| 115 | + |
| 116 | + $transport->send($chatMessage); |
| 117 | + } |
| 118 | +} |
0 commit comments