Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c026c2a

Browse files
[Notifier] Add Twitter notifier
1 parent ff56fe8 commit c026c2a

18 files changed

+930
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
173173
use Symfony\Component\Notifier\Bridge\TurboSms\TurboSmsTransport;
174174
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
175+
use Symfony\Component\Notifier\Bridge\Twitter\TwitterTransportFactory;
175176
use Symfony\Component\Notifier\Bridge\Vonage\VonageTransportFactory;
176177
use Symfony\Component\Notifier\Bridge\Yunpian\YunpianTransportFactory;
177178
use Symfony\Component\Notifier\Bridge\Zendesk\ZendeskTransportFactory;
@@ -2593,6 +2594,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
25932594
TelnyxTransportFactory::class => 'notifier.transport_factory.telnyx',
25942595
TurboSmsTransport::class => 'notifier.transport_factory.turbo-sms',
25952596
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
2597+
TwitterTransportFactory::class => 'notifier.transport_factory.twitter',
25962598
VonageTransportFactory::class => 'notifier.transport_factory.vonage',
25972599
YunpianTransportFactory::class => 'notifier.transport_factory.yunpian',
25982600
ZendeskTransportFactory::class => 'notifier.transport_factory.zendesk',

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
6060
use Symfony\Component\Notifier\Bridge\TurboSms\TurboSmsTransportFactory;
6161
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
62+
use Symfony\Component\Notifier\Bridge\Twitter\TwitterTransportFactory;
6263
use Symfony\Component\Notifier\Bridge\Vonage\VonageTransportFactory;
6364
use Symfony\Component\Notifier\Bridge\Yunpian\YunpianTransportFactory;
6465
use Symfony\Component\Notifier\Bridge\Zendesk\ZendeskTransportFactory;
@@ -105,6 +106,10 @@
105106
->parent('notifier.transport_factory.abstract')
106107
->tag('texter.transport_factory')
107108

109+
->set('notifier.transport_factory.twitter', TwitterTransportFactory::class)
110+
->parent('notifier.transport_factory.abstract')
111+
->tag('chatter.transport_factory')
112+
108113
->set('notifier.transport_factory.all-my-sms', AllMySmsTransportFactory::class)
109114
->parent('notifier.transport_factory.abstract')
110115
->tag('texter.transport_factory')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
6.3
5+
---
6+
7+
* Add the bridge
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2022 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Twitter Notifier
2+
===============
3+
4+
Provides [Twitter](https://twitter.com) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
TWITTER_DSN=twitter://API_KEY:API_SECRET:ACCESS_TOKEN:ACCESS_SECRET@default
11+
```
12+
13+
where:
14+
- `API_KEY` is your Twitter API Key
15+
- `API_SECRET` is your Twitter API Key Secret
16+
- `ACCESS_TOKEN` is your read+write Twitter Access Token
17+
- `ACCESS_SECRET` is your read+write Twitter Access Token Secret
18+
19+
Resources
20+
---------
21+
22+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
23+
* [Report issues](https://github.com/symfony/symfony/issues) and
24+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
25+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\Notifier\Bridge\Twitter\TwitterTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
17+
class TwitterTransportFactoryTest extends TransportFactoryTestCase
18+
{
19+
public function createFactory(): TwitterTransportFactory
20+
{
21+
return new TwitterTransportFactory();
22+
}
23+
24+
public function createProvider(): iterable
25+
{
26+
yield ['twitter://host.test', 'twitter://A:B:C:[email protected]'];
27+
}
28+
29+
public function supportsProvider(): iterable
30+
{
31+
yield [true, 'twitter://default'];
32+
yield [false, 'somethingElse://default'];
33+
}
34+
35+
public function unsupportedSchemeProvider(): iterable
36+
{
37+
yield ['somethingElse://default'];
38+
}
39+
40+
public function incompleteDsnProvider(): iterable
41+
{
42+
yield ['twitter://A:B@default', 'Invalid "twitter://A:B@default" notifier DSN: Access Token is missing'];
43+
}
44+
}
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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

Comments
 (0)