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

Commit 350674e

Browse filesBrowse files
committed
feature #42123 [Notifier] Add FakeSMS Logger transport (noniagriconomie)
This PR was merged into the 5.4 branch. Discussion ---------- [Notifier] Add FakeSMS Logger transport | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Sub part of #40625 | License | MIT | Doc PR | WIP Friendly ping `@OskarStark` As commented [here](#40625 (comment)) I use mainly the sms transport, thus wanted to work on the fake sms. This PR adds the `logger`. For the part `an optional channel`, how can we get to here? dymanically retreiving the proper logger based on the dsn config? Commits ------- 2596a72 Add FakeSMS Logger transport
2 parents f1353d5 + 2596a72 commit 350674e
Copy full SHA for 350674e

File tree

Expand file treeCollapse file tree

9 files changed

+202
-15
lines changed
Filter options
Expand file treeCollapse file tree

9 files changed

+202
-15
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2506,7 +2506,8 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
25062506

25072507
if (ContainerBuilder::willBeAvailable('symfony/fake-sms-notifier', FakeSmsTransportFactory::class, ['symfony/framework-bundle', 'symfony/notifier', 'symfony/mailer'])) {
25082508
$container->getDefinition($classToServices[FakeSmsTransportFactory::class])
2509-
->replaceArgument('$mailer', new Reference('mailer'));
2509+
->replaceArgument('$mailer', new Reference('mailer'))
2510+
->replaceArgument('$logger', new Reference('logger'));
25102511
}
25112512

25122513
if (isset($config['admin_recipients'])) {

‎src/Symfony/Component/Notifier/Bridge/FakeSms/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/FakeSms/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.4
5+
---
6+
7+
* Add the ``FakeSmsLoggerTransport``
8+
49
5.3
510
---
611

+62Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\FakeSms;
13+
14+
use Psr\Log\LoggerInterface;
15+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SentMessage;
19+
use Symfony\Component\Notifier\Message\SmsMessage;
20+
use Symfony\Component\Notifier\Transport\AbstractTransport;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
23+
/**
24+
* @author Antoine Makdessi <amakdessi@me.com>
25+
*/
26+
final class FakeSmsLoggerTransport extends AbstractTransport
27+
{
28+
protected const HOST = 'default';
29+
30+
private $logger;
31+
32+
public function __construct(LoggerInterface $logger, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
33+
{
34+
$this->logger = $logger;
35+
36+
parent::__construct($client, $dispatcher);
37+
}
38+
39+
public function __toString(): string
40+
{
41+
return sprintf('fakesms+logger://%s', $this->getEndpoint());
42+
}
43+
44+
public function supports(MessageInterface $message): bool
45+
{
46+
return $message instanceof SmsMessage;
47+
}
48+
49+
/**
50+
* @param MessageInterface|SmsMessage $message
51+
*/
52+
protected function doSend(MessageInterface $message): SentMessage
53+
{
54+
if (!$this->supports($message)) {
55+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
56+
}
57+
58+
$this->logger->info(sprintf('New SMS on phone number: %s', $message->getPhone()));
59+
60+
return new SentMessage($message, (string) $this);
61+
}
62+
}

‎src/Symfony/Component/Notifier/Bridge/FakeSms/FakeSmsTransportFactory.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/FakeSms/FakeSmsTransportFactory.php
+17-10Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\FakeSms;
1313

14+
use Psr\Log\LoggerInterface;
1415
use Symfony\Component\Mailer\MailerInterface;
1516
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
1617
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
@@ -20,38 +21,44 @@
2021
/**
2122
* @author James Hemery <james@yieldstudio.fr>
2223
* @author Oskar Stark <oskarstark@googlemail.com>
24+
* @author Antoine Makdessi <amakdessi@me.com>
2325
*/
2426
final class FakeSmsTransportFactory extends AbstractTransportFactory
2527
{
2628
protected $mailer;
29+
protected $logger;
2730

28-
public function __construct(MailerInterface $mailer)
31+
public function __construct(MailerInterface $mailer, LoggerInterface $logger)
2932
{
3033
parent::__construct();
3134

3235
$this->mailer = $mailer;
36+
$this->logger = $logger;
3337
}
3438

3539
/**
36-
* @return FakeSmsEmailTransport
40+
* @return FakeSmsEmailTransport|FakeSmsLoggerTransport
3741
*/
3842
public function create(Dsn $dsn): TransportInterface
3943
{
4044
$scheme = $dsn->getScheme();
4145

42-
if ('fakesms+email' !== $scheme) {
43-
throw new UnsupportedSchemeException($dsn, 'fakesms', $this->getSupportedSchemes());
44-
}
46+
switch ($scheme) {
47+
case 'fakesms+email':
48+
$mailerTransport = $dsn->getHost();
49+
$to = $dsn->getRequiredOption('to');
50+
$from = $dsn->getRequiredOption('from');
4551

46-
$mailerTransport = $dsn->getHost();
47-
$to = $dsn->getRequiredOption('to');
48-
$from = $dsn->getRequiredOption('from');
52+
return (new FakeSmsEmailTransport($this->mailer, $to, $from))->setHost($mailerTransport);
53+
case 'fakesms+logger':
54+
return new FakeSmsLoggerTransport($this->logger);
55+
}
4956

50-
return (new FakeSmsEmailTransport($this->mailer, $to, $from))->setHost($mailerTransport);
57+
throw new UnsupportedSchemeException($dsn, 'fakesms', $this->getSupportedSchemes());
5158
}
5259

5360
protected function getSupportedSchemes(): array
5461
{
55-
return ['fakesms+email'];
62+
return ['fakesms+email', 'fakesms+logger'];
5663
}
5764
}

‎src/Symfony/Component/Notifier/Bridge/FakeSms/README.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/FakeSms/README.md
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Fake SMS Notifier
22
=================
33

4-
Provides Fake SMS (as email during development) integration for Symfony Notifier.
4+
Provides Fake SMS (as email or log during development) integration for Symfony Notifier.
55

6-
#### DSN example
6+
#### DSN example for email
77

88
```
99
FAKE_SMS_DSN=fakesms+email://default?to=TO&from=FROM
@@ -14,10 +14,17 @@ where:
1414
- `FROM` is email who send SMS during development
1515

1616
To use a custom mailer transport:
17+
1718
```
1819
FAKE_SMS_DSN=fakesms+email://mailchimp?to=TO&from=FROM
1920
```
2021

22+
#### DSN example for logger
23+
24+
```
25+
FAKE_SMS_DSN=fakesms+logger://default
26+
```
27+
2128
Resources
2229
---------
2330

+66Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\FakeSms\Tests;
13+
14+
use Psr\Log\LoggerInterface;
15+
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsLoggerTransport;
16+
use Symfony\Component\Notifier\Message\ChatMessage;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SmsMessage;
19+
use Symfony\Component\Notifier\Test\TransportTestCase;
20+
use Symfony\Component\Notifier\Transport\TransportInterface;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
23+
final class FakeSmsLoggerTransportTest extends TransportTestCase
24+
{
25+
public function createTransport(HttpClientInterface $client = null, LoggerInterface $logger = null): TransportInterface
26+
{
27+
$transport = (new FakeSmsLoggerTransport($logger ?? $this->createMock(LoggerInterface::class), $client ?? $this->createMock(HttpClientInterface::class)));
28+
29+
return $transport;
30+
}
31+
32+
public function toStringProvider(): iterable
33+
{
34+
yield ['fakesms+logger://default', $this->createTransport()];
35+
}
36+
37+
public function supportedMessagesProvider(): iterable
38+
{
39+
yield [new SmsMessage('0611223344', 'Hello!')];
40+
yield [new SmsMessage('+33611223344', 'Hello!')];
41+
}
42+
43+
public function unsupportedMessagesProvider(): iterable
44+
{
45+
yield [new ChatMessage('Hello!')];
46+
yield [$this->createMock(MessageInterface::class)];
47+
}
48+
49+
public function testSendWithDefaultTransport()
50+
{
51+
$message = new SmsMessage($phone = '0611223344', 'Hello!');
52+
53+
$logger = new TestLogger();
54+
55+
$transport = $this->createTransport(null, $logger);
56+
57+
$transport->send($message);
58+
59+
$logs = $logger->logs;
60+
$this->assertNotEmpty($logs);
61+
62+
$log = $logs[0];
63+
$this->assertSame(sprintf('New SMS on phone number: %s', $phone), $log['message']);
64+
$this->assertSame('info', $log['level']);
65+
}
66+
}

‎src/Symfony/Component/Notifier/Bridge/FakeSms/Tests/FakeSmsTransportFactoryTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/FakeSms/Tests/FakeSmsTransportFactoryTest.php
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\FakeSms\Tests;
1313

14+
use Psr\Log\LoggerInterface;
1415
use Symfony\Component\Mailer\MailerInterface;
1516
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory;
1617
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
@@ -23,7 +24,7 @@ final class FakeSmsTransportFactoryTest extends TransportFactoryTestCase
2324
*/
2425
public function createFactory(): TransportFactoryInterface
2526
{
26-
return new FakeSmsTransportFactory($this->createMock(MailerInterface::class));
27+
return new FakeSmsTransportFactory($this->createMock(MailerInterface::class), $this->createMock(LoggerInterface::class));
2728
}
2829

2930
public function createProvider(): iterable
@@ -37,6 +38,11 @@ public function createProvider(): iterable
3738
'fakesms+email://mailchimp?to=recipient@email.net&from=sender@email.net',
3839
'fakesms+email://mailchimp?to=recipient@email.net&from=sender@email.net',
3940
];
41+
42+
yield [
43+
'fakesms+logger://default',
44+
'fakesms+logger://default',
45+
];
4046
}
4147

4248
public function missingRequiredOptionProvider(): iterable
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\FakeSms\Tests;
13+
14+
use Psr\Log\AbstractLogger;
15+
16+
final class TestLogger extends AbstractLogger
17+
{
18+
public $logs = [];
19+
20+
public function log($level, $message, array $context = []): void
21+
{
22+
$this->logs[] = [
23+
'level' => $level,
24+
'message' => $message,
25+
'context' => $context,
26+
];
27+
}
28+
}

‎src/Symfony/Component/Notifier/Bridge/FakeSms/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/FakeSms/composer.json
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "symfony/fake-sms-notifier",
33
"type": "symfony-bridge",
4-
"description": "Fake SMS (as email during development) Notifier Bridge.",
4+
"description": "Fake SMS (as email or log during development) Notifier Bridge.",
55
"keywords": ["sms", "development", "email", "notifier", "symfony"],
66
"homepage": "https://symfony.com",
77
"license": "MIT",
@@ -10,6 +10,11 @@
1010
"name": "James Hemery",
1111
"homepage": "https://github.com/JamesHemery"
1212
},
13+
{
14+
"name": "Antoine Makdessi",
15+
"email": "amakdessi@me.com",
16+
"homepage": "http://antoine.makdessi.free.fr"
17+
},
1318
{
1419
"name": "Symfony Community",
1520
"homepage": "https://symfony.com/contributors"

0 commit comments

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