Skip to content

Commit 63bcd41

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: (31 commits) fix test Clarify goals of AbstractController cs fix [Security][Validator] Add missing translations for Indonesian (id) [Security] Deprecate legacy signatures [Notifier] fix typo firebase [SecurityBundle] Create a smooth upgrade path for security factories Add trailing Line return if last line is non empty Add trailing Line return if last line is non empty [Security] Deprecate `PassportInterface` Report mismatches between trans-unit id and source text via status script Do not add namespace argument to NullAdapter in CachePoolPass [FrameworkBundle] Update cache:clear help [HttpFoundation] Add `litespeed_finish_request` to `Response` Fix markup (minor) remove author tags from test classes [Notifier] add `SentMessageEvent` and `FailedMessageEvent` [HttpFoundation] Mark Request::get() internal Add missing to semi-colon to exception.js [FrameworkBundle] remove dead conditions in Translation Commands ...
2 parents 1763f91 + d4327a1 commit 63bcd41

File tree

8 files changed

+233
-7
lines changed

8 files changed

+233
-7
lines changed

CHANGELOG.md

Lines 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 `SentMessageEvent` and `FailedMessageEvent`
8+
49
5.3
510
---
611

Event/FailedMessageEvent.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\Event;
13+
14+
use Symfony\Component\Notifier\Message\MessageInterface;
15+
use Symfony\Contracts\EventDispatcher\Event;
16+
17+
/**
18+
* @author Smaïne Milianni <smaine.milianni@gmail.com>
19+
*/
20+
final class FailedMessageEvent extends Event
21+
{
22+
private $message;
23+
private $error;
24+
25+
public function __construct(MessageInterface $message, \Throwable $error)
26+
{
27+
$this->message = $message;
28+
$this->error = $error;
29+
}
30+
31+
public function getMessage(): MessageInterface
32+
{
33+
return $this->message;
34+
}
35+
36+
public function getError(): \Throwable
37+
{
38+
return $this->error;
39+
}
40+
}

Event/SentMessageEvent.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Event;
13+
14+
use Symfony\Component\Notifier\Message\SentMessage;
15+
use Symfony\Contracts\EventDispatcher\Event;
16+
17+
/**
18+
* @author Smaïne Milianni <smaine.milianni@gmail.com>
19+
*/
20+
final class SentMessageEvent extends Event
21+
{
22+
private $message;
23+
24+
public function __construct(SentMessage $message)
25+
{
26+
$this->message = $message;
27+
}
28+
29+
public function getMessage(): SentMessage
30+
{
31+
return $this->message;
32+
}
33+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Symfony\Component\Notifier\Tests\Event;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Notifier\Event\FailedMessageEvent;
7+
use Symfony\Component\Notifier\Event\MessageEvent;
8+
use Symfony\Component\Notifier\Message\ChatMessage;
9+
use Symfony\Component\Notifier\Message\MessageInterface;
10+
use Symfony\Component\Notifier\Message\SentMessage;
11+
use Symfony\Component\Notifier\Message\SmsMessage;
12+
use Symfony\Component\Notifier\Tests\Transport\DummyMessage;
13+
use Symfony\Component\Notifier\Transport\AbstractTransport;
14+
use Symfony\Component\Notifier\Transport\NullTransport;
15+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
16+
use Symfony\Contracts\HttpClient\HttpClientInterface;
17+
18+
final class FailedMessageEventTest extends TestCase
19+
{
20+
/**
21+
* @dataProvider messagesProvider
22+
*/
23+
public function testConstruct(MessageInterface $message, \Throwable $error, FailedMessageEvent $event)
24+
{
25+
$this->assertEquals($event, new FailedMessageEvent($message, $error));
26+
}
27+
28+
/**
29+
* @dataProvider messagesProvider
30+
*/
31+
public function testGetMessage(MessageInterface $message, \Throwable $error, FailedMessageEvent $event)
32+
{
33+
$this->assertSame($message, $event->getMessage());
34+
}
35+
36+
/**
37+
* @dataProvider messagesProvider
38+
*/
39+
public function testGetError(MessageInterface $message, \Throwable $error, FailedMessageEvent $event)
40+
{
41+
$this->assertSame($error, $event->getError());
42+
}
43+
44+
public function testFailedMessageEventIsDisptachIfError()
45+
{
46+
$eventDispatcherMock = $this->createMock(EventDispatcherInterface::class);
47+
$clientMock = $this->createMock(HttpClientInterface::class);
48+
49+
$transport = new class($clientMock, $eventDispatcherMock) extends AbstractTransport {
50+
public function __construct($client, EventDispatcherInterface $dispatcher = null)
51+
{
52+
$this->exception = new NullTransportException();
53+
parent::__construct($client, $dispatcher);
54+
}
55+
56+
public function doSend(MessageInterface $message): SentMessage
57+
{
58+
throw $this->exception;
59+
}
60+
61+
public function supports(MessageInterface $message): bool
62+
{
63+
return true;
64+
}
65+
66+
public function __toString(): string
67+
{
68+
}
69+
};
70+
71+
$message = new DummyMessage();
72+
73+
$eventDispatcherMock->expects($this->exactly(2))
74+
->method('dispatch')
75+
->withConsecutive(
76+
[new MessageEvent($message)],
77+
[new FailedMessageEvent($message, $transport->exception)]
78+
);
79+
try {
80+
$transport->send($message);
81+
} catch (NullTransportException $exception) {
82+
// catch Exception that is voluntary thrown in NullTransport::send
83+
}
84+
}
85+
86+
public function messagesProvider(): iterable
87+
{
88+
yield [$message = new ChatMessage('subject'), $error = new \RuntimeException(), new FailedMessageEvent($message, $error)];
89+
yield [$message = new SmsMessage('+3312345678', 'subject'), $error = new \Exception(), new FailedMessageEvent($message, $error)];
90+
}
91+
}
92+
93+
class NullTransportException extends \Exception
94+
{
95+
}

Tests/Event/SentMessageEventTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Symfony\Component\Notifier\Tests\Event;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Notifier\Event\SentMessageEvent;
7+
use Symfony\Component\Notifier\Message\ChatMessage;
8+
use Symfony\Component\Notifier\Message\SentMessage;
9+
use Symfony\Component\Notifier\Message\SmsMessage;
10+
11+
final class SentMessageEventTest extends TestCase
12+
{
13+
/**
14+
* @dataProvider messagesProvider
15+
*/
16+
public function testConstruct(SentMessage $message, SentMessageEvent $event)
17+
{
18+
$this->assertEquals($event, new SentMessageEvent($message));
19+
}
20+
21+
/**
22+
* @dataProvider messagesProvider
23+
*/
24+
public function testGetMessage(SentMessage $message, SentMessageEvent $event)
25+
{
26+
$this->assertSame($message, $event->getMessage());
27+
}
28+
29+
public function messagesProvider(): iterable
30+
{
31+
yield [$message = new SentMessage(new ChatMessage('subject'), 'null_transport'), new SentMessageEvent($message)];
32+
yield [$message = new SentMessage(new SmsMessage('+3312345678', 'subject'), 'null_transport'), new SentMessageEvent($message)];
33+
}
34+
}

Tests/Transport/NullTransportTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testSend()
3131
$eventDispatcherMock = $this->createMock(EventDispatcherInterface::class)
3232
);
3333

34-
$eventDispatcherMock->expects($this->once())->method('dispatch');
34+
$eventDispatcherMock->expects($this->exactly(2))->method('dispatch');
3535
$nullTransport->send(new DummyMessage());
3636
}
3737
}

Transport/AbstractTransport.php

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

1414
use Symfony\Component\HttpClient\HttpClient;
15+
use Symfony\Component\Notifier\Event\FailedMessageEvent;
1516
use Symfony\Component\Notifier\Event\MessageEvent;
17+
use Symfony\Component\Notifier\Event\SentMessageEvent;
1618
use Symfony\Component\Notifier\Exception\LogicException;
1719
use Symfony\Component\Notifier\Message\MessageInterface;
1820
use Symfony\Component\Notifier\Message\SentMessage;
@@ -68,11 +70,23 @@ public function setPort(?int $port): self
6870

6971
public function send(MessageInterface $message): SentMessage
7072
{
71-
if (null !== $this->dispatcher) {
72-
$this->dispatcher->dispatch(new MessageEvent($message));
73+
if (null === $this->dispatcher) {
74+
return $this->doSend($message);
7375
}
7476

75-
return $this->doSend($message);
77+
$this->dispatcher->dispatch(new MessageEvent($message));
78+
79+
try {
80+
$sentMessage = $this->doSend($message);
81+
} catch (\Throwable $error) {
82+
$this->dispatcher->dispatch(new FailedMessageEvent($message, $error));
83+
84+
throw $error;
85+
}
86+
87+
$this->dispatcher->dispatch(new SentMessageEvent($sentMessage));
88+
89+
return $sentMessage;
7690
}
7791

7892
abstract protected function doSend(MessageInterface $message): SentMessage;

Transport/NullTransport.php

Lines changed: 8 additions & 3 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\Event\MessageEvent;
15+
use Symfony\Component\Notifier\Event\SentMessageEvent;
1516
use Symfony\Component\Notifier\Message\MessageInterface;
1617
use Symfony\Component\Notifier\Message\NullMessage;
1718
use Symfony\Component\Notifier\Message\SentMessage;
@@ -32,12 +33,16 @@ public function __construct(EventDispatcherInterface $dispatcher = null)
3233
public function send(MessageInterface $message): SentMessage
3334
{
3435
$message = new NullMessage($message);
36+
$sentMessage = new SentMessage($message, (string) $this);
3537

36-
if (null !== $this->dispatcher) {
37-
$this->dispatcher->dispatch(new MessageEvent($message));
38+
if (null === $this->dispatcher) {
39+
return $sentMessage;
3840
}
3941

40-
return new SentMessage($message, (string) $this);
42+
$this->dispatcher->dispatch(new MessageEvent($message));
43+
$this->dispatcher->dispatch(new SentMessageEvent($sentMessage));
44+
45+
return $sentMessage;
4146
}
4247

4348
public function __toString(): string

0 commit comments

Comments
 (0)