|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\Twitter\Tests; |
| 13 | + |
| 14 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 15 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 16 | +use Symfony\Component\Mime\Part\File; |
| 17 | +use Symfony\Component\Notifier\Bridge\Twitter\TwitterOptions; |
| 18 | +use Symfony\Component\Notifier\Bridge\Twitter\TwitterTransport; |
| 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\Test\TransportTestCase; |
| 23 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 24 | + |
| 25 | +class TwitterTransportTest extends TransportTestCase |
| 26 | +{ |
| 27 | + public function createTransport(HttpClientInterface $client = null): TwitterTransport |
| 28 | + { |
| 29 | + return new TwitterTransport('APIK', 'APIS', 'TOKEN', 'SECRET', $client ?? $this->createMock(HttpClientInterface::class)); |
| 30 | + } |
| 31 | + |
| 32 | + public function toStringProvider(): iterable |
| 33 | + { |
| 34 | + yield ['twitter://api.twitter.com', $this->createTransport()]; |
| 35 | + } |
| 36 | + |
| 37 | + public function supportedMessagesProvider(): iterable |
| 38 | + { |
| 39 | + yield [new ChatMessage('Hello!')]; |
| 40 | + } |
| 41 | + |
| 42 | + public function unsupportedMessagesProvider(): iterable |
| 43 | + { |
| 44 | + yield [new SmsMessage('0611223344', 'Hello!')]; |
| 45 | + yield [$this->createMock(MessageInterface::class)]; |
| 46 | + } |
| 47 | + |
| 48 | + public function testBasicTweet() |
| 49 | + { |
| 50 | + $transport = $this->createTransport(new MockHttpClient(function (string $method, string $url, array $options) { |
| 51 | + $this->assertSame('POST', $method); |
| 52 | + $this->assertSame('https://api.twitter.com/2/tweets', $url); |
| 53 | + $this->assertSame('{"text":"Hello World!"}', $options['body']); |
| 54 | + $this->assertArrayHasKey('authorization', $options['normalized_headers']); |
| 55 | + |
| 56 | + return new MockResponse('{"data":{"id":"abc123"}}'); |
| 57 | + })); |
| 58 | + |
| 59 | + $result = $transport->send(new ChatMessage('Hello World!')); |
| 60 | + |
| 61 | + $this->assertSame('abc123', $result->getMessageId()); |
| 62 | + } |
| 63 | + |
| 64 | + public function testTweetImage() |
| 65 | + { |
| 66 | + $transport = $this->createTransport(new MockHttpClient((function () { |
| 67 | + yield function (string $method, string $url, array $options) { |
| 68 | + $this->assertSame('POST', $method); |
| 69 | + $this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=INIT&total_bytes=185&media_type=image%2Fgif&media_category=tweet_image', $url); |
| 70 | + $this->assertArrayHasKey('authorization', $options['normalized_headers']); |
| 71 | + |
| 72 | + return new MockResponse('{"media_id_string":"gif123"}'); |
| 73 | + }; |
| 74 | + |
| 75 | + yield function (string $method, string $url, array $options) { |
| 76 | + $this->assertSame('POST', $method); |
| 77 | + $this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=APPEND&media_id=gif123&segment_index=0', $url); |
| 78 | + $this->assertArrayHasKey('authorization', $options['normalized_headers']); |
| 79 | + |
| 80 | + return new MockResponse(); |
| 81 | + }; |
| 82 | + |
| 83 | + yield function (string $method, string $url, array $options) { |
| 84 | + $this->assertSame('POST', $method); |
| 85 | + $this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=FINALIZE&media_id=gif123', $url); |
| 86 | + $this->assertArrayHasKey('authorization', $options['normalized_headers']); |
| 87 | + |
| 88 | + return new MockResponse('{"processing_info":{"state":"pending","check_after_secs": 0}}'); |
| 89 | + }; |
| 90 | + |
| 91 | + yield function (string $method, string $url, array $options) { |
| 92 | + $this->assertSame('GET', $method); |
| 93 | + $this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=STATUS&media_id=gif123', $url); |
| 94 | + $this->assertArrayHasKey('authorization', $options['normalized_headers']); |
| 95 | + |
| 96 | + return new MockResponse('{"processing_info":{"state":"succeeded"}}'); |
| 97 | + }; |
| 98 | + |
| 99 | + yield function (string $method, string $url, array $options) { |
| 100 | + $this->assertSame('POST', $method); |
| 101 | + $this->assertSame('https://upload.twitter.com/1.1/media/metadata/create.json', $url); |
| 102 | + $this->assertSame('{"media_id":"gif123","alt_text":{"text":"A fixture"}}', $options['body']); |
| 103 | + $this->assertArrayHasKey('authorization', $options['normalized_headers']); |
| 104 | + |
| 105 | + return new MockResponse('{"processing_info":{"state":"succeeded"}}'); |
| 106 | + }; |
| 107 | + |
| 108 | + yield function (string $method, string $url, array $options) { |
| 109 | + $this->assertSame('POST', $method); |
| 110 | + $this->assertSame('https://api.twitter.com/2/tweets', $url); |
| 111 | + $this->assertSame('{"text":"Hello World!","media":{"media_ids":["gif123"]}}', $options['body']); |
| 112 | + $this->assertArrayHasKey('authorization', $options['normalized_headers']); |
| 113 | + |
| 114 | + return new MockResponse('{"data":{"id":"abc123"}}'); |
| 115 | + }; |
| 116 | + })())); |
| 117 | + |
| 118 | + $result = $transport->send(new ChatMessage('Hello World!', (new TwitterOptions()) |
| 119 | + ->attachImage(new File(__DIR__.'/fixtures.gif'), 'A fixture')) |
| 120 | + ); |
| 121 | + |
| 122 | + $this->assertSame('abc123', $result->getMessageId()); |
| 123 | + } |
| 124 | + |
| 125 | + public function testTweetVideo() |
| 126 | + { |
| 127 | + $transport = $this->createTransport(new MockHttpClient((function () { |
| 128 | + yield function (string $method, string $url, array $options) { |
| 129 | + $this->assertSame('POST', $method); |
| 130 | + $this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=INIT&total_bytes=185&media_type=image%2Fgif&media_category=tweet_video', $url); |
| 131 | + $this->assertArrayHasKey('authorization', $options['normalized_headers']); |
| 132 | + |
| 133 | + return new MockResponse('{"media_id_string":"gif123"}'); |
| 134 | + }; |
| 135 | + |
| 136 | + yield function (string $method, string $url, array $options) { |
| 137 | + $this->assertSame('POST', $method); |
| 138 | + $this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=INIT&total_bytes=185&media_type=image%2Fgif&media_category=subtitles', $url); |
| 139 | + $this->assertArrayHasKey('authorization', $options['normalized_headers']); |
| 140 | + |
| 141 | + return new MockResponse('{"media_id_string":"sub234"}'); |
| 142 | + }; |
| 143 | + |
| 144 | + yield function (string $method, string $url, array $options) { |
| 145 | + $this->assertSame('POST', $method); |
| 146 | + $this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=APPEND&media_id=gif123&segment_index=0', $url); |
| 147 | + $this->assertArrayHasKey('authorization', $options['normalized_headers']); |
| 148 | + |
| 149 | + return new MockResponse(); |
| 150 | + }; |
| 151 | + |
| 152 | + yield function (string $method, string $url, array $options) { |
| 153 | + $this->assertSame('POST', $method); |
| 154 | + $this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=APPEND&media_id=sub234&segment_index=0', $url); |
| 155 | + $this->assertArrayHasKey('authorization', $options['normalized_headers']); |
| 156 | + |
| 157 | + return new MockResponse(); |
| 158 | + }; |
| 159 | + |
| 160 | + yield function (string $method, string $url, array $options) { |
| 161 | + $this->assertSame('POST', $method); |
| 162 | + $this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=FINALIZE&media_id=gif123', $url); |
| 163 | + $this->assertArrayHasKey('authorization', $options['normalized_headers']); |
| 164 | + |
| 165 | + return new MockResponse('{}'); |
| 166 | + }; |
| 167 | + |
| 168 | + yield function (string $method, string $url, array $options) { |
| 169 | + $this->assertSame('POST', $method); |
| 170 | + $this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=FINALIZE&media_id=sub234', $url); |
| 171 | + $this->assertArrayHasKey('authorization', $options['normalized_headers']); |
| 172 | + |
| 173 | + return new MockResponse('{}'); |
| 174 | + }; |
| 175 | + |
| 176 | + yield function (string $method, string $url, array $options) { |
| 177 | + $this->assertSame('POST', $method); |
| 178 | + $this->assertSame('https://upload.twitter.com/1.1/media/subtitles/create.json', $url); |
| 179 | + $this->assertSame('{"media_id":"gif123","media_category":"tweet_video","subtitle_info":{"subtitles":[{"media_id":"sub234","language_code":"en","display_name":"English"}]}}', $options['body']); |
| 180 | + $this->assertArrayHasKey('authorization', $options['normalized_headers']); |
| 181 | + |
| 182 | + return new MockResponse(); |
| 183 | + }; |
| 184 | + |
| 185 | + yield function (string $method, string $url, array $options) { |
| 186 | + $this->assertSame('POST', $method); |
| 187 | + $this->assertSame('https://api.twitter.com/2/tweets', $url); |
| 188 | + $this->assertSame('{"text":"Hello World!","media":{"media_ids":["gif123"]}}', $options['body']); |
| 189 | + $this->assertArrayHasKey('authorization', $options['normalized_headers']); |
| 190 | + |
| 191 | + return new MockResponse('{"data":{"id":"abc123"}}'); |
| 192 | + }; |
| 193 | + })())); |
| 194 | + |
| 195 | + $result = $transport->send(new ChatMessage('Hello World!', (new TwitterOptions()) |
| 196 | + ->attachVideo(new File(__DIR__.'/fixtures.gif'), '', new File(__DIR__.'/fixtures.gif', 'English.en.srt'))) |
| 197 | + ); |
| 198 | + |
| 199 | + $this->assertSame('abc123', $result->getMessageId()); |
| 200 | + } |
| 201 | +} |
0 commit comments