8000 [Notifier] Add TurboSms Bridge · symfony/symfony@12bacf4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 12bacf4

Browse files
fre5hOskarStark
authored andcommitted
[Notifier] Add TurboSms Bridge
1 parent 34e1972 commit 12bacf4

File tree

16 files changed

+513
-0
lines changed

16 files changed

+513
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
use Symfony\Component\Notifier\Bridge\SpotHit\SpotHitTransportFactory;
146146
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
147147
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
148+
use Symfony\Component\Notifier\Bridge\TurboSms\TurboSmsTransport;
148149
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
149150
use Symfony\Component\Notifier\Bridge\Yunpian\YunpianTransportFactory;
150151
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
@@ -2462,6 +2463,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24622463
SpotHitTransportFactory::class => 'notifier.transport_factory.spothit',
24632464
TelegramTransportFactory::class => 'notifier.transport_factory.telegram',
24642465
TelnyxTransportFactory::class => 'notifier.transport_factory.telnyx',
2466+
TurboSmsTransport::class => 'notifier.transport_factory.turbosms',
24652467
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
24662468
YunpianTransportFactory::class => 'notifier.transport_factory.yunpian',
24672469
ZulipTransportFactory::class => 'notifier.transport_factory.zulip',
@@ -2485,6 +2487,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24852487
case 'rocketchat': $package = 'rocket-chat'; break;
24862488
case 'smsbiuras': $package = 'sms-biuras'; break;
24872489
case 'spothit': $package = 'spot-hit'; break;
2490+
case 'turbosms': $package = 'turbo-sms'; break;
24882491
}
24892492

24902493
if (!ContainerBuilder::willBeAvailable(sprintf('symfony/%s-notifier', $package), $class, $parentPackages)) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
use Symfony\Component\Notifier\Bridge\SpotHit\SpotHitTransportFactory;
4848
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
4949
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
50+
use Symfony\Component\Notifier\Bridge\TurboSms\TurboSmsTransportFactory;
5051
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
5152
use Symfony\Component\Notifier\Bridge\Yunpian\YunpianTransportFactory;
5253
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
@@ -220,5 +221,9 @@
220221
->set('notifier.transport_factory.yunpian', YunpianTransportFactory::class)
221222
->parent('notifier.transport_factory.abstract')
222223
->tag('texter.transport_factory')
224+
225+
->set('notifier.transport_factory.turbosms', TurboSmsTransportFactory::class)
226+
->parent('notifier.transport_factory.abstract')
227+
->tag('texter.transport_factory')
223228
;
224229
};
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+
5.4
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) 2021 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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
TurboSms Notifier
2+
=================
3+
4+
Provides [TurboSms](https://turbosms.ua/) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
TURBOSMS_DSN=turbosms://AUTH_TOKEN@default?from=FROM
11+
```
12+
13+
where:
14+
- `AUTH_TOKEN` is your TurboSms auth token
15+
- `FROM` is your sender name, should be alpha-numeral, max 20 symbols and activated in your TurboSms account
16+
17+
Resources
18+
---------
19+
20+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
21+
* [Report issues](https://github.com/symfony/symfony/issues) and
22+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
23+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\TurboSms\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\TurboSms\TurboSmsTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
17+
18+
final class TurboSmsTransportFactoryTest extends TransportFactoryTestCase
19+
{
20+
/**
21+
* @return TurboSmsTransportFactory
22+
*/
23+
public function createFactory(): TransportFactoryInterface
24+
{
25+
return new TurboSmsTransportFactory();
26+
}
27+
28+
public function createProvider(): iterable
29+
{
30+
yield [
31+
'turbosms://host.test?from=acme',
32+
'turbosms://accountSid:authToken@host.test?from=acme',
33+
];
34+
35+
yield [
36+
'turbosms://host.test?from=Symfony+Community',
37+
'turbosms://accountSid:authToken@host.test?from=Symfony Community',
38+
];
39+
}
40+
41+
public function supportsProvider(): iterable
42+
{
43+
yield [true, 'turbosms://authToken@default?from=acme'];
44+
yield [false, 'somethingElse://authToken@default?from=acme'];
45+
}
46+
47+
public function missingRequiredOptionProvider(): iterable
48+
{
49+
yield 'missing option: from' => ['turbosms://authToken@default'];
50+
}
51+
52+
public function unsupportedSchemeProvider(): iterable
53+
{
54+
yield ['somethingElse://authToken@default?from=acme'];
55+
yield ['somethingElse://authToken@default'];
56+
}
57+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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\TurboSms\Tests;
13+
14+
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\Notifier\Bridge\TurboSms\TurboSmsTransport;
16+
use Symfony\Component\Notifier\Exception\LengthException;
17+
use Symfony\Component\Notifier\Exception\TransportException;
18+
use Symfony\Component\Notifier\Message\ChatMessage;
19+
use Symfony\Component\Notifier\Message\MessageInterface;
20+
use Symfony\Component\Notifier\Message\SentMessage;
21+
use Symfony\Component\Notifier\Message\SmsMessage;
22+
use Symfony\Component\Notifier\Test\TransportTestCase;
23+
use Symfony\Component\Notifier\Transport\TransportInterface;
24+
use Symfony\Contracts\HttpClient\HttpClientInterface;
25+
use Symfony\Contracts\HttpClient\ResponseInterface;
26+
27+
final class TurboSmsTransportTest extends TransportTestCase
28+
{
29+
/**
30+
* @return TurboSmsTransport
31+
*/
32+
public function createTransport(HttpClientInterface $client = null): TransportInterface
33+
{
34+
return new TurboSmsTransport('authToken', 'sender', $client ?? $this->createMock(HttpClientInterface::class));
35+
}
36+
37+
public function toStringProvider(): iterable
38+
{
39+
yield ['turbosms://api.turbosms.ua?from=sender', $this->createTransport()];
40+
}
41+
42+
public function supportedMessagesProvider(): iterable
43+
{
44+
yield [new SmsMessage('380931234567', 'Hello!')];
45+
}
46+
47+
public function unsupportedMessagesProvider(): iterable
48+
{
49+
yield [new ChatMessage('Hello!')];
50+
yield [$this->createMock(MessageInterface::class)];
51+
}
52+
53+
public function testSuccessfulSend()
54+
{
55+
$response = $this->createMock(ResponseInterface::class);
56+
$response
57+
->expects(self::exactly(2))
58+
->method('getStatusCode')
59+
->willReturn(200)
60+
;
61+
$response
62+
->expects(self::once())
63+
->method('getContent')
64+
->willReturn(json_encode([
65+
'response_code' => 0,
66+
'response_status' => 'OK',
67+
'response_result' => [
68+
[
69+
'phone' => '380931234567',
70+
'response_code' => 0,
71+
'message_id' => 'f83f8868-5e46-c6cf-e4fb-615e5a293754',
72+
'response_status' => 'OK',
73+
],
74+
],
75+
]))
76+
;
77+
78+
$client = new MockHttpClient(static function () use ($response): ResponseInterface {
79+
return $response;
80+
});
81+
82+
$message = new SmsMessage('380931234567', 'Тест/Test');
83+
84+
$transport = $this->createTransport($client);
85+
$sentMessage = $transport->send($message);
86+
87+
self::assertInstanceOf(SentMessage::class, $sentMessage);
88+
self::assertSame('f83f8868-5e46-c6cf-e4fb-615e5a293754', $sentMessage->getMessageId());
89+
}
90+
91+
public function testFailedSend()
92+
{
93+
$response = $this->createMock(ResponseInterface::class);
94+
$response
95+
->expects(self::exactly(2))
96+
->method('getStatusCode')
97+
->willReturn(400)
98+
;
99+
$response
100+
->expects(self::once())
101+
->method('getContent')
102+
->willReturn(json_encode([
103+
'response_code' => 103,
104+
'response_status' => 'REQUIRED_TOKEN',
105+
'response_result' => null,
106+
]))
107+
;
108+
109+
$client = new MockHttpClient(static function () use ($response): ResponseInterface {
110+
retu 787C rn $response;
111+
});
112+
113+
$message = new SmsMessage('380931234567', 'Тест/Test');
114+
115+
$transport = $this->createTransport($client);
116+
117+
$this->expectException(TransportException::class);
118+
$this->expectExceptionMessage('Unable to send SMS with TurboSMS: Error code 103 with message "REQUIRED_TOKEN".');
119+
120+
$transport->send($message);
121+
}
122+
123+
public function testInvalidFrom()
124+
{
125+
$this->expectException(LengthException::class);
126+
$this->expectExceptionMessage('The sender length of a TurboSMS message must not exceed 20 characters.');
127+
128+
$message = new SmsMessage('380931234567', 'Hello!');
129+
$transport = new TurboSmsTransport('authToken', 'abcdefghijklmnopqrstu', $this->createMock(HttpClientInterface::class));
130+
131+
$transport->send($message);
132+
}
133+
134+
public function testInvalidSubjectWithLatinSymbols()
135+
{
136+
$message = new SmsMessage('380931234567', str_repeat('z', 1522));
137+
$transport = new TurboSmsTransport('authToken', 'sender', $this->createMock(HttpClientInterface::class));
138+
139+
$this->expectException(LengthException::class);
140+
$this->expectExceptionMessage('The subject length for "latin" symbols of a TurboSMS message must not exceed 1521 characters.');
141+
142+
$transport->send($message);
143+
}
144+
145+
public function testInvalidSubjectWithCyrillicSymbols()
146+
{
147+
$message = new SmsMessage('380931234567', str_repeat('z', 661).'Й');
148+
$transport = new TurboSmsTransport('authToken', 'sender', $this->createMock(HttpClientInterface::class));
149+
150+
$this->expectException(LengthException::class);
151+
$this->expectExceptionMessage('The subject length for "cyrillic" symbols of a TurboSMS message must not exceed 661 characters.');
152+
153+
$transport->send($message);
154+
}
155+
}

0 commit comments

Comments
 (0)
0