Skip to content

Navigation Menu

Sign in
Appearance settings

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

[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 1 commit into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory;
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory;
Expand Down Expand Up @@ -2332,6 +2333,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
FirebaseTransportFactory::class => 'notifier.transport_factory.firebase',
FreeMobileTransportFactory::class => 'notifier.transport_factory.freemobile',
SpotHitTransportFactory::class => 'notifier.transport_factory.spothit',
FakeSmsTransportFactory::class => 'notifier.transport_factory.fakesms',
Copy link
Contributor

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

Copy link

@ke20 ke20 Jan 26, 2021

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?

I agree with that too.

why to rely on a mailer mail? why not a monolog log?

IMOH, I think it's will great if we can choose the destination.

For example :

  • By email if the content contains a required information for the workflow (otp code)
  • By log if the content is just an information and have no importance
  • In a database or a redis to use that in functionnals testing and we should check or get information in the content message

What do you think ?

Copy link
Contributor

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

Copy link
Contributor Author

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.

OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud',
SinchTransportFactory::class => 'notifier.transport_factory.sinch',
ZulipTransportFactory::class => 'notifier.transport_factory.zulip',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory;
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory;
Expand Down Expand Up @@ -95,6 +96,10 @@
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

->set('notifier.transport_factory.fakesms', FakeSmsTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

->set('notifier.transport_factory.ovhcloud', OvhCloudTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')
Expand Down
7 changes: 7 additions & 0 deletions 7 src/Symfony/Component/Notifier/Bridge/FakeSms/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

5.3
---

* Add the bridge
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);
}
}
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'];
}
}
23 changes: 23 additions & 0 deletions 23 src/Symfony/Component/Notifier/Bridge/FakeSms/README.md
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)
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)];
}
}
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
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class UnsupportedSchemeException extends LogicException
'class' => Bridge\SpotHit\SpotHitTransportFactory::class,
'package' => 'symfony/spot-hit-notifier',
],
'fakesms' => [
'class' => Bridge\FakeSms\FakeSmsTransportFactory::class,
'package' => 'symfony/fake-sms-notifier',
],
'ovhcloud' => [
'class' => Bridge\OvhCloud\OvhCloudTransportFactory::class,
'package' => 'symfony/ovh-cloud-notifier',
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.