8000 feature #48565 [Notifier] [FakeChat] Allow missing optional dependenc… · symfony/symfony@0f24e41 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f24e41

Browse files
committed
feature #48565 [Notifier] [FakeChat] Allow missing optional dependency (Benjamin Schoch)
This PR was merged into the 6.3 branch. Discussion ---------- [Notifier] [FakeChat] Allow missing optional dependency | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? |no | New feature? | kind of | Deprecations? | no | Tickets | see #48441 | License | MIT Nearly the same as in this PR: #48546 This allows the `FakeChatTransportFactory` to be used without providing an implementation of `MailerInterface` or `LoggerInterface` if one of them is actually not required during runtime. I did not share any of the implementation now between both components. I could think of pulling out the Exception as well as Parts of the test to something like a `FakeTransportFactoryTestCase` for example. What do you think? Commits ------- 3f312b8 [Notifier] [FakeChat] Allow missing optional dependency
2 parents 45113ba + 3f312b8 commit 0f24e41

File tree

3 files changed

+79
-6
lines changed

3 files changed

+79
-6
lines changed

src/Symfony/Component/Notifier/Bridge/FakeChat/FakeChatTransportFactory.php

Lines changed: 17 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\LogicException;
1617
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
1718
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
1819
use Symfony\Component\Notifier\Transport\Dsn;
@@ -23,10 +24,10 @@
2324
*/
2425
final class FakeChatTransportFactory extends AbstractTransportFactory
2526
{
26-
private MailerInterface $mailer;
27-
private LoggerInterface $logger;
27+
private ?MailerInterface $mailer;
28+
private ?LoggerInterface $logger;
2829

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

@@ -39,6 +40,10 @@ public function create(Dsn $dsn): FakeChatEmailTransport|FakeChatLoggerTransport
3940
$scheme = $dsn->getScheme();
4041

4142
if ('fakechat+email' === $scheme) {
43+
if (null === $this->mailer) {
44+
$this->throwMissingDependencyException($scheme, MailerInterface::class, 'symfony/mailer');
45+
}
46+
4247
$mailerTransport = $dsn->getHost();
4348
$to = $dsn->getRequiredOption('to');
4449
$from = $dsn->getRequiredOption('from');
@@ -47,6 +52,10 @@ public function create(Dsn $dsn): FakeChatEmailTransport|FakeChatLoggerTransport
4752
}
4853

4954
if ('fakechat+logger' === $scheme) {
55+
if (null === $this->logger) {
56+
$this->throwMissingDependencyException($scheme, LoggerInterface::class, 'psr/log');
57+
}
58+
5059
return new FakeChatLoggerTransport($this->logger);
5160
}
5261

@@ -57,4 +66,9 @@ protected function getSupportedSchemes(): array
5766
{
5867
return ['fakechat+email', 'fakechat+logger'];
5968
}
69+
70+
private function throwMissingDependencyException(string $scheme, string $missingDependency, string $suggestedPackage): void
71+
{
72+
throw new LogicException(sprintf('Cannot create a transport for scheme "%s" without providing an implementation of "%s". Try running "composer require "%s"".', $scheme, $missingDependency, $suggestedPackage));
73+
}
6074
}

src/Symfony/Component/Notifier/Bridge/FakeChat/Tests/FakeChatTransportFactoryTest.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\FakeChat\FakeChatTransportFactory;
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 FakeChatTransportFactoryTest 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 FakeChatTransportFactory($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 FakeChatTransportFactory($mailer, $logger);
41+
$transport = $factory->create(new Dsn($dsn));
42+
43+
$this->assertSame($dsn, (string) $transport);
44+
}
45+
2146
public function createFactory(): FakeChatTransportFactory
2247
{
2348
return new FakeChatTransportFactory($this->createMock(MailerInterface::class), $this->createMock(LoggerInterface::class));
@@ -63,4 +88,35 @@ public function unsupportedSchemeProvider(): iterable
6388
{
6489
yield ['somethingElse://default?to=recipient@email.net&from=sender@email.net'];
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+
'fakechat+email://default?to=recipient@email.net&from=sender@email.net',
99+
sprintf($exceptionMessage, 'fakechat+email', MailerInterface::class),
100+
];
101+
yield 'missing logger' => [
102+
$this->createMock(MailerInterface::class),
103+
null,
104+
'fakechat+logger://default',
105+
sprintf($exceptionMessage, 'fakechat+logger', LoggerInterface::class),
106+
];
107+
}
108+
109+
public function missingOptionalDependencyProvider(): iterable
110+
{
111+
yield 'missing logger' => [
112+
$this->createMock(MailerInterface::class),
113+
null,
114+
'fakechat+email://default?to=recipient@email.net&from=sender@email.net',
115+
];
116+
yield 'missing mailer' => [
117+
null,
118+
$this->createMock(LoggerInterface::class),
119+
'fakechat+logger://default',
120+
];
121+
}
66122
}

src/Symfony/Component/Notifier/Bridge/FakeChat/composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
"require": {
2424
"php": ">=8.1",
2525
"symfony/http-client": "^5.4|^6.0",
26-
"symfony/notifier": "^6.2",
27-
"symfony/event-dispatcher-contracts": "^2|^3",
28-
"symfony/mailer": "^5.4|^6.0"
26+
"symfony/notifier": "^6.2"
27+
},
28+
"require-dev": {
29+
"symfony/mailer": "^5.4|^6.0",
30+
"symfony/event-dispatcher": "^5.4|^6.0",
31+
"psr/log": "^1|^2|^3"
2932
},
3033
"autoload": {
3134
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\FakeChat\\": "" },

0 commit comments

Comments
 (0)
0