-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Notifier] [FakeSms] Add the bridge #39949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
5.3 | ||
--- | ||
|
||
* Add the bridge |
75 changes: 75 additions & 0 deletions
75
src/Symfony/Component/Notifier/Bridge/FakeSms/FakeSmsEmailTransport.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\FakeSms; | ||
|
||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; | ||
use Symfony\Component\Mailer\MailerInterface; | ||
use Symfony\Component\Mime\Email; | ||
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\SentMessage; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Component\Notifier\Transport\AbstractTransport; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @author James Hemery <james@yieldstudio.fr> | ||
*/ | ||
final class FakeSmsEmailTransport extends AbstractTransport | ||
{ | ||
private $mailer; | ||
private $to; | ||
private $from; | ||
|
||
public function __construct(MailerInterface $mailer, string $to, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) | ||
{ | ||
$this->mailer = $mailer; | ||
$this->to = $to; | ||
$this->from = $from; | ||
|
||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf('fakesms+email://%s?to=%s&from=%s', $this->getEndpoint(), $this->to, $this->from); | ||
} | ||
|
||
public function supports(MessageInterface $message): bool | ||
{ | ||
return $message instanceof SmsMessage; | ||
} | ||
|
||
/** | ||
* @param MessageInterface|SmsMessage $message | ||
* | ||
* @throws TransportExceptionInterface | ||
*/ | ||
protected function doSend(MessageInterface $message): SentMessage | ||
{ | ||
if (!$this->supports($message)) { | ||
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); | ||
} | ||
|
||
$email = (new Email()) | ||
->from($this->from) | ||
->to($this->to) | ||
->subject(sprintf('New SMS on phone number: %s', $message->getPhone())) | ||
->html($message->getSubject()) | ||
->text($message->getSubject()); | ||
|
||
$this->mailer->send($email); | ||
|
||
return new SentMessage($message, (string) $this); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/Symfony/Component/Notifier/Bridge/FakeSms/FakeSmsTransportFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\FakeSms; | ||
|
||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Notifier\Transport\AbstractTransportFactory; | ||
use Symfony\Component\Notifier\Transport\Dsn; | ||
use Symfony\Component\Notifier\Transport\TransportInterface; | ||
use Symfony\Contracts\Service\ServiceProviderInterface; | ||
|
||
/** | ||
* @author James Hemery <james@yieldstudio.fr> | ||
*/ | ||
final class FakeSmsTransportFactory extends AbstractTransportFactory | ||
{ | ||
protected $serviceProvider; | ||
|
||
public function __construct(ServiceProviderInterface $serviceProvider) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->serviceProvider = $serviceProvider; | ||
JamesHemery marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* @return FakeSmsEmailTransport | ||
*/ | ||
public function create(Dsn $dsn): TransportInterface | ||
{ | ||
$scheme = $dsn->getScheme(); | ||
|
||
if (!\in_array($scheme, $this->getSupportedSchemes())) { | ||
throw new UnsupportedSchemeException($dsn, 'fakesms', $this->getSupportedSchemes()); | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if ('fakesms+email' === $scheme) { | ||
$serviceId = $dsn->getHost(); | ||
$to = $dsn->getRequiredOption('to'); | ||
$from = $dsn->getRequiredOption('from'); | ||
|
||
return (new FakeSmsEmailTransport($this->serviceProvider->get($serviceId), $to, $from))->setHost($serviceId); | ||
} | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['fakesms+email']; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Fake SMS Notifier | ||
================= | ||
|
||
Provides Fake SMS (as email during development) integration for Symfony Notifier. | ||
|
||
#### DSN example | ||
|
||
``` | ||
FAKE_SMS_DSN=fakesms+email://MAILER_SERVICE_ID?to=TO&from=FROM | ||
``` | ||
|
||
where: | ||
- `MAILER_SERVICE_ID` is mailer service id (use `mailer` by default) | ||
- `TO` is email who receive SMS during development | ||
- `FROM` is email who send SMS during development | ||
|
||
Resources | ||
--------- | ||
|
||
* [Contributing](https://symfony.com/doc/current/contributing/index.html) | ||
* [Report issues](https://github.com/symfony/symfony/issues) and | ||
[send Pull Requests](https://github.com/symfony/symfony/pulls) | ||
in the [main Symfony repository](https://github.com/symfony/symfony) |
49 changes: 49 additions & 0 deletions
49
src/Symfony/Component/Notifier/Bridge/FakeSms/Tests/FakeSmsEmailTransportTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\FakeSms\Tests; | ||
|
||
use Symfony\Component\Mailer\MailerInterface; | ||
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsEmailTransport; | ||
use Symfony\Component\Notifier\Message\ChatMessage; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Component\Notifier\Tests\TransportTestCase; | ||
use Symfony\Component\Notifier\Transport\TransportInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @author James Hemery <james@yieldstudio.fr> | ||
*/ | ||
final class FakeSmsEmailTransportTest extends TransportTestCase | ||
{ | ||
public function createTransport(?HttpClientInterface $client = null): TransportInterface | ||
{ | ||
return (new FakeSmsEmailTransport($this->createMock(MailerInterface::class), 'recipient@email.net', 'from@email.net'))->setHost('mailer'); | ||
} | ||
|
||
public function toStringProvider(): iterable | ||
{ | ||
yield ['fakesms+email://mailer?to=recipient@email.net&from=from@email.net', $this->createTransport()]; | ||
} | ||
|
||
public function supportedMessagesProvider(): iterable | ||
{ | ||
yield [new SmsMessage('0611223344', 'Hello!')]; | ||
yield [new SmsMessage('+33611223344', 'Hello!')]; | ||
} | ||
|
||
public function unsupportedMessagesProvider(): iterable | ||
{ | ||
yield [new ChatMessage('Hello!')]; | ||
yield [$this->createMock(MessageInterface::class)]; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/Symfony/Component/Notifier/Bridge/FakeSms/Tests/FakeSmsTransportFactoryTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\FakeSms\Tests; | ||
|
||
use Symfony\Component\Mailer\MailerInterface; | ||
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory; | ||
use Symfony\Component\Notifier\Tests\TransportFactoryTestCase; | ||
use Symfony\Component\Notifier\Transport\TransportFactoryInterface; | ||
use Symfony\Contracts\Service\ServiceProviderInterface; | ||
|
||
/** | ||
* @author James Hemery <james@yieldstudio.fr> | ||
*/ | ||
final class FakeSmsTransportFactoryTest extends TransportFactoryTestCase | ||
{ | ||
/** | ||
* @return FakeSmsTransportFactory | ||
*/ | ||
public function createFactory(): TransportFactoryInterface | ||
{ | ||
$serviceProvider = $this->createMock(ServiceProviderInterface::class); | ||
$serviceProvider->method('has')->willReturn(true); | ||
$serviceProvider->method('get')->willReturn($this->createMock(MailerInterface::class)); | ||
|
||
return new FakeSmsTransportFactory($serviceProvider); | ||
} | ||
|
||
public function createProvider(): iterable | ||
{ | ||
yield [ | ||
'fakesms+email://mailer?to=recipient@email.net&from=sender@email.net', | ||
'fakesms+email://mailer?to=recipient@email.net&from=sender@email.net', | ||
]; | ||
} | ||
|
||
public function missingRequiredOptionProvider(): iterable | ||
{ | ||
yield 'missing option: from' => ['fakesms+email://mailer?to=recipient@email.net']; | ||
yield 'missing option: to' => ['fakesms+email://mailer?from=sender@email.net']; | ||
} | ||
|
||
public function supportsProvider(): iterable | ||
{ | ||
yield [true, 'fakesms+email://mailer?to=recipient@email.net&from=sender@email.net']; | ||
yield [false, 'somethingElse://api_token@default?from=MyCompany']; | ||
} | ||
|
||
public function incompleteDsnProvider(): iterable | ||
{ | ||
yield 'missing from' => ['fakesms+email://mailer?to=recipient@email.net']; | ||
yield 'missing to' => ['fakesms+email://mailer?from=recipient@email.net']; | ||
} | ||
|
||
public function unsupportedSchemeProvider(): iterable | ||
{ | ||
yield ['foobar://api_token@default?from=MyCompany']; | ||
yield ['foobar://api_token@default']; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Symfony/Component/Notifier/Bridge/FakeSms/composer.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "symfony/fake-sms-notifier", | ||
"type": "symfony-bridge", | ||
"description": "Fake SMS (as email during development) Notifier Bridge.", | ||
"keywords": ["sms", "development", "email", "notifier", "symfony"], | ||
"homepage": "https://symfony.com", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "James Hemery", | ||
"homepage": "https://github.com/JamesHemery" | ||
}, | ||
{ | ||
"name": "Symfony Community", | ||
"homepage": "https://symfony.com/contributors" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.2.5", | ||
"symfony/http-client": "^4.4|^5.2", | ||
"symfony/notifier": "^5.3", | ||
"symfony/event-dispatcher-contracts": "^2", | ||
"symfony/mailer": "^5.2" | ||
}, | ||
"autoload": { | ||
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\FakeSms\\": "" }, | ||
"exclude-from-classmap": [ | ||
"/Tests/" | ||
] | ||
}, | ||
"minimum-stability": "dev" | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Symfony/Component/Notifier/Bridge/FakeSms/phpunit.xml.dist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd" | ||
backupGlobals="false" | ||
colors="true" | ||
bootstrap="vendor/autoload.php" | ||
failOnRisky="true" | ||
failOnWarning="true" | ||
> | ||
<php> | ||
<ini name="error_reporting" value="-1" /> | ||
</php> | ||
|
||
<testsuites> | ||
<testsuite name="Symfony FakeSms Notifier Bridge Test Suite"> | ||
<directory>./Tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>./</directory> | ||
<exclude> | ||
<directory>./Resources</directory> | ||
<directory>./Tests</directory> | ||
<directory>./vendor</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> | ||
JamesHemery marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldnt this fake transport only be configured for
dev
env?also i really like the idea of having a fake transport, i have a question; why to rely on a mailer mail? why not a monolog log?
they are displayed in console, they are displayed in profiler, they are stored in file and it does not need another software to be installed
could not found the related issue that lead to this PR :)
thank you
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with that too.
IMOH, I think it's will great if we can choose the destination.
For example :
What do you think ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ke20
What do you think ?
I do not know the feature request/needs that initiate this PR, as written upper :)
But iiuc, the aim is to fake, in dev, the notifier sms logic to see if it works
I think that relying only on the mailer is overkill, a logger is enough => thus my previous comment :)
maybe yes a configured fake transport with multiple sublogic (log > mailer > database) can be the way, lets wait other reviews
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, it would be interesting to be able to use other distribution channels. The packet was originally developed for the development of an otp system, important in the user workflow, so the choice of emails was made to facilitate testing.
Also, support of chat notifier would be a good thing.
I could do the integration of other channels in a few weeks, if anyone would like to contribute to this PR, feel free.