Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b228dbc

Browse filesBrowse files
committed
Fix
1 parent dccc8aa commit b228dbc
Copy full SHA for b228dbc

File tree

6 files changed

+191
-54
lines changed
Filter options

6 files changed

+191
-54
lines changed

‎src/Symfony/Component/Cache/Tests/Adapter/PdoDbalAdapterTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Tests/Adapter/PdoDbalAdapterTest.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
1616
use Doctrine\DBAL\DriverManager;
1717
use Doctrine\DBAL\Schema\Schema;
18-
use Doctrine\DBAL\Version;
1918
use Psr\Cache\CacheItemPoolInterface;
2019
use Symfony\Component\Cache\Adapter\PdoAdapter;
2120
use Symfony\Component\Cache\Tests\Traits\PdoPruneableTrait;
+80Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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\GatewayApi;
13+
14+
use Symfony\Component\Notifier\Exception\TransportException;
15+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\SentMessage;
18+
use Symfony\Component\Notifier\Message\SmsMessage;
19+
use Symfony\Component\Notifier\Transport\AbstractTransport;
20+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
23+
/**
24+
* @author Piergiuseppe Longo <piergiuseppe.longo@gmail.com>
25+
*/
26+
final class GatewayApiTransport extends AbstractTransport
27+
{
28+
protected const HOST = 'gatewayapi.com';
29+
30+
private $authToken;
31+
private $from;
32+
33+
public function __construct(string $authToken, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
34+
{
35+
$this->authToken = $authToken;
36+
$this->from = $from;
37+
38+
parent::__construct($client, $dispatcher);
39+
}
40+
41+
public function __toString(): string
42+
{
43+
return sprintf('gatewayapi://%s?from=%s', $this->getEndpoint(), $this->from);
44+
}
45+
46+
public function supports(MessageInterface $message): bool
47+
{
48+
return $message instanceof SmsMessage;
49+
}
50+
51+
protected function doSend(MessageInterface $message): SentMessage
52+
{
53+
if (!$message instanceof SmsMessage) {
54+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
55+
}
56+
57+
$endpoint = sprintf('https://%s/rest/mtsms', $this->getEndpoint());
58+
59+
$response = $this->client->request('POST', $endpoint, [
60+
'auth_basic' => [$this->authToken, ''],
61+
'json' => [
62+
'sender' => $this->from,
63+
'recipients' => [['msisdn' => $message->getPhone()]],
64+
'message' => $message->getSubject(),
65+
],
66+
]);
67+
68+
$statusCode = $response->getStatusCode();
69+
if (200 !== $statusCode) {
70+
throw new TransportException(sprintf('Unable to send the SMS: error %d.', $statusCode), $response);
71+
}
72+
73+
$content = $response->toArray();
74+
75+
$sentMessage = new SentMessage($message, (string) $this);
76+
$sentMessage->setMessageId((string) $content['ids'][0]);
77+
78+
return $sentMessage;
79+
}
80+
}
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\GatewayApi;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
use Symfony\Component\Notifier\Transport\TransportInterface;
18+
19+
/**
20+
* @author Piergiuseppe Longo <piergiuseppe.longo@gmail.com>
21+
*/
22+
final class GatewayApiTransportFactory extends AbstractTransportFactory
23+
{
24+
/**
25+
* @return GatewayApiTransport
26+
*/
27+
public function create(Dsn $dsn): TransportInterface
28+
{
29+
$scheme = $dsn->getScheme();
30+
31+
if ('gatewayapi' !== $scheme) {
32+
throw new UnsupportedSchemeException($dsn, 'gatewayapi', $this->getSupportedSchemes());
33+
}
34+
35+
$authToken = $dsn->getUser();
36+
$from = $dsn->getRequiredOption('from');
37+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
38+
$port = $dsn->getPort();
39+
40+
return (new GatewayApiTransport($authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
41+
}
42+
43+
protected function getSupportedSchemes(): array
44+
{
45+
return ['gatewayapi'];
46+
}
47+
}
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
GatewayApi Notifier
2+
===============
3+
4+
Provides GatewayApi integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
GATEWAYAPI_DSN=gatewayapi://TOKEN@default?from=FROM
11+
```
12+
13+
where:
14+
- `TOKEN` is API Token (OAuth)
15+
- `FROM` is sender name
16+
17+
See your account info at https://gatewayapi.com
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)

‎src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiTransportFactoryTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiTransportFactoryTest.php
+21-30Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,45 @@
22

33
namespace Symfony\Component\Notifier\Bridge\GatewayApi\Tests;
44

5-
use PHPUnit\Framework\TestCase;
65
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory;
7-
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
8-
use Symfony\Component\Notifier\Exception\MissingRequiredOptionException;
9-
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
10-
use Symfony\Component\Notifier\Transport\Dsn;
6+
use Symfony\Component\Notifier\Tests\TransportFactoryTestCase;
7+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
118

129
/**
1310
* @author Piergiuseppe Longo <piergiuseppe.longo@gmail.com>
11+
* @author Oskar Stark <oskarstark@googlemail.com>
1412
*/
15-
final class GatewayApiTransportFactoryTest extends TestCase
13+
final class GatewayApiTransportFactoryTest extends TransportFactoryTestCase
1614
{
17-
public function testSupportsGatewayApiScheme()
15+
/**
16+
* @return GatewayApiTransportFactory
17+
*/
18+
public function createFactory(): TransportFactoryInterface
1819
{
19-
$factory = $this->createFactory();
20-
21-
$this->assertTrue($factory->supports(Dsn::fromString('gatewayapi://token@host.test?from=Symfony')));
20+
return new GatewayApiTransportFactory();
2221
}
2322

24-
public function testUnSupportedGatewayShouldThrowsUnsupportedSchemeException()
23+
public function createProvider(): iterable
2524
{
26-
$factory = $this->createFactory();
27-
28-
$this->expectException(UnsupportedSchemeException::class);
29-
30-
$factory->create(Dsn::fromString('somethingElse://token@default?from=Symfony'));
25+
yield [
26+
'gatewayapi://gatewayapi.com?from=Symfony',
27+
'gatewayapi://token@default?from=Symfony',
28+
];
3129
}
3230

33-
public function testCreateWithNoTokenThrowsIncompleteDsnException()
31+
public function supportsProvider(): iterable
3432
{
35-
$factory = $this->createFactory();
36-
37-
$this->expectException(IncompleteDsnException::class);
38-
39-
$factory->create(Dsn::fromString('gatewayapi://host.test?from=Symfony'));
33+
yield [true, 'gatewayapi://token@host.test?from=Symfony'];
34+
yield [false, 'somethingElse://token@default?from=Symfony'];
4035
}
4136

42-
public function testCreateWithNoFromShouldThrowsMissingRequiredOptionException()
37+
public function incompleteDsnProvider(): iterable
4338
{
44-
$factory = $this->createFactory();
45-
46-
$this->expectException(MissingRequiredOptionException::class);
47-
48-
$factory->create(Dsn::fromString('gatewayapi://token@host.test'));
39+
yield 'missing token' => ['gatewayapi://host.test?from=Symfony'];
4940
}
5041

51-
private function createFactory(): GatewayApiTransportFactory
42+
public function missingRequiredOptionProvider(): iterable
5243
{
53-
return new GatewayApiTransportFactory();
44+
yield 'missing option' => ['gatewayapi://token@host.test'];
5445
}
5546
}

‎src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiTransportTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiTransportTest.php
+18-23Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,45 @@
22

33
namespace Symfony\Component\Notifier\Bridge\GatewayApi\Tests;
44

5-
use PHPUnit\Framework\TestCase;
65
use Symfony\Component\HttpClient\MockHttpClient;
76
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransport;
8-
use Symfony\Component\Notifier\Exception\LogicException;
97
use Symfony\Component\Notifier\Message\ChatMessage;
108
use Symfony\Component\Notifier\Message\MessageInterface;
119
use Symfony\Component\Notifier\Message\SentMessage;
1210
use Symfony\Component\Notifier\Message\SmsMessage;
11+
use Symfony\Component\Notifier\Tests\TransportTestCase;
12+
use Symfony\Component\Notifier\Transport\TransportInterface;
1313
use Symfony\Contracts\HttpClient\HttpClientInterface;
1414
use Symfony\Contracts\HttpClient\ResponseInterface;
1515

1616
/**
1717
* @author Piergiuseppe Longo <piergiuseppe.longo@gmail.com>
18+
* @author Oskar Stark <oskarstark@googlemail.com>
1819
*/
19-
final class GatewayApiTransportTest extends TestCase
20+
final class GatewayApiTransportTest extends TransportTestCase
2021
{
21-
public function testToStringContainsProperties()
22+
/**
23+
* @return GatewayApiTransport
24+
*/
25+
public function createTransport(?HttpClientInterface $client = null): TransportInterface
2226
{
23-
$transport = $this->createTransport();
24-
25-
$this->assertSame('gatewayapi://host.test?from=Symfony', (string) $transport);
27+
return new GatewayApiTransport('authtoken', 'Symfony', $client ?: $this->createMock(HttpClientInterface::class));
2628
}
2729

28-
public function testSupportsMessageInterface()
30+
public function toStringProvider(): iterable
2931
{
30-
$transport = $this->createTransport();
31-
32-
$this->assertTrue($transport->supports(new SmsMessage('3333333333', 'Hello!')));
33-
$this->assertFalse($transport->supports(new ChatMessage('Hello!')));
34-
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class)));
32+
yield ['gatewayapi://host.test?from=Symfony', $this->createTransport()];
3533
}
3634

37-
public function testSendNonSmsMessageThrowsLogicException()
35+
public function supportedMessagesProvider(): iterable
3836
{
39-
$transport = $this->createTransport();
40-
41-
$this->expectException(LogicException::class);
37+
yield [new SmsMessage('0611223344', 'Hello!')];
38+
}
4239

43-
$transport->send($this->createMock(MessageInterface::class));
40+
public function unsupportedMessagesProvider(): iterable
41+
{
42+
yield [new ChatMessage('Hello!')];
43+
yield [$this->createMock(MessageInterface::class)];
4444
}
4545

4646
public function testSend()
@@ -64,9 +64,4 @@ public function testSend()
6464
$this->assertInstanceOf(SentMessage::class, $sentMessage);
6565
$this->assertSame('42', $sentMessage->getMessageId());
6666
}
67-
68-
private function createTransport(?HttpClientInterface $client = null): GatewayApiTransport
69-
{
70-
return (new GatewayApiTransport('authtoken', 'Symfony', $client))->setHost('host.test');
71-
}
7267
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.