Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e5bcf6e

Browse files
[Notifier] [FakeSms] Allow missing optional dependency
1 parent f3a9a0e commit e5bcf6e

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\Mailer\MailerInterface;
16+
use Symfony\Component\Notifier\Exception\MissingFactoryDependencyException;
1617
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
1718
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
1819
use Symfony\Component\Notifier\Transport\Dsn;
@@ -24,10 +25,10 @@
2425
*/
2526
final class FakeSmsTransportFactory extends AbstractTransportFactory
2627
{
27-
private MailerInterface $mailer;
28-
private LoggerInterface $logger;
28+
private ?MailerInterface $mailer;
29+
private ?LoggerInterface $logger;
2930

30-
public function __construct(MailerInterface $mailer, LoggerInterface $logger)
31+
public function __construct(MailerInterface $mailer = null, LoggerInterface $logger = null)
3132
{
3233
parent::__construct();
3334

@@ -40,6 +41,10 @@ public function create(Dsn $dsn): FakeSmsEmailTransport|FakeSmsLoggerTransport
4041
$scheme = $dsn->getScheme();
4142

4243
if ('fakesms+email' === $scheme) {
44+
if ($this->mailer === null) {
45+
throw new MissingFactoryDependencyException($scheme, MailerInterface::class);
46+
}
47+
4348
$mailerTransport = $dsn->getHost();
4449
$to = $dsn->getRequiredOption('to');
4550
$from = $dsn->getRequiredOption('from');
@@ -48,6 +53,10 @@ public function create(Dsn $dsn): FakeSmsEmailTransport|FakeSmsLoggerTransport
4853
}
4954

5055
if ('fakesms+logger' === $scheme) {
56+
if ($this->logger === null) {
57+
throw new MissingFactoryDependencyException($scheme, LoggerInterface::class);
58+
}
59+
5160
return new FakeSmsLoggerTransport($this->logger);
5261
}
5362

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,35 @@
1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\Mailer\MailerInterface;
1616
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory;
17+
use Symfony\Component\Notifier\Exception\LogicException;
1718
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
19+
use Symfony\Component\Notifier\Transport\Dsn;
1820

1921
final class FakeSmsTransportFactoryTest extends TransportFactoryTestCase
2022
{
23+
/**
24+
* @dataProvider missingRequiredDependencyProvider
25+
*/
26+
public function testMissingRequiredDependency(?MailerInterface $mailer, ?LoggerInterface $logger, string $dsn, string $message)
27+
{
28+
$this->expectException(LogicException::class);
29+
$this->expectExceptionMessage($message);
30+
31+
$factory = new FakeSmsTransportFactory($mailer, $logger);
32+
$factory->create(new Dsn($dsn));
33+
}
34+
35+
/**
36+
* @dataProvider missingOptionalDependencyProvider
37+
*/
38+
public function testMissingOptionalDependency(?MailerInterface $mailer, ?LoggerInterface $logger, string $dsn)
39+
{
40+
$factory = new FakeSmsTransportFactory($mailer, $logger);
41+
$transport = $factory->create(new Dsn($dsn));
42+
43+
$this->assertSame($dsn, (string) $transport);
44+
}
45+
2146
public function createFactory(): FakeSmsTransportFactory
2247
{
2348
return new FakeSmsTransportFactory($this->createMock(MailerInterface::class), $this->createMock(LoggerInterface::class));
@@ -63,4 +88,35 @@ public function unsupportedSchemeProvider(): iterable
6388
{
6489
yield ['somethingElse://[email protected]&[email protected]'];
6590
}
91+
92+
public function missingRequiredDependencyProvider(): iterable
93+
{
94+
$exceptionMessage = 'Cannot create a transport for scheme "%s" without providing an implementation of "%s".';
95+
yield 'missing mailer' => [
96+
null,
97+
$this->createMock(LoggerInterface::class),
98+
99+
sprintf($exceptionMessage, 'fakesms+email', MailerInterface::class),
100+
];
101+
yield 'missing logger' => [
102+
$this->createMock(MailerInterface::class),
103+
null,
104+
'fakesms+logger://default',
105+
sprintf($exceptionMessage, 'fakesms+logger', LoggerInterface::class),
106+
];
107+
}
108+
109+
public function missingOptionalDependencyProvider(): iterable
110+
{
111+
yield 'missing logger' => [
112+
$this->createMock(MailerInterface::class),
113+
null,
114+
115+
];
116+
yield 'missing mailer' => [
117+
null,
118+
$this->createMock(LoggerInterface::class),
119+
'fakesms+logger://default',
120+
];
121+
}
66122
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Exception;
13+
14+
/**
15+
* @author Benjamin Schoch <[email protected]>
16+
*/
17+
class MissingFactoryDependencyException extends LogicException
18+
{
19+
public function __construct(string $scheme, string $missingType, \Throwable $previous = null)
20+
{
21+
$message = sprintf(
22+
'Cannot create a transport for scheme "%s" without providing an implementation of "%s".',
23+
$scheme,
24+
$missingType
25+
);
26+
27+
parent::__construct($message, 0, $previous);
28+
}
29+
}

src/Symfony/Component/Notifier/Transport/TransportFactoryInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Notifier\Transport;
1313

1414
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
15+
use Symfony\Component\Notifier\Exception\MissingFactoryDependencyException;
1516
use Symfony\Component\Notifier\Exception\MissingRequiredOptionException;
1617
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
1718

@@ -24,6 +25,7 @@ interface TransportFactoryInterface
2425
* @throws UnsupportedSchemeException
2526
* @throws IncompleteDsnException
2627
* @throws MissingRequiredOptionException
28+
* @throws MissingFactoryDependencyException
2729
*/
2830
public function create(Dsn $dsn): TransportInterface;
2931

0 commit comments

Comments
 (0)