-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Notifier] add SentMessageEvent
and FailedMessageEvent
#39601
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
chalasr
merged 1 commit into
symfony:5.4
from
ismail1432:notifier-add-sent-message-event
Aug 5, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
CHANGELOG | ||
========= | ||
|
||
5.4 | ||
--- | ||
* Add `SentMessageEvent` and `FailedMessageEvent` | ||
|
||
5.3 | ||
--- | ||
|
||
|
40 changes: 40 additions & 0 deletions
40
src/Symfony/Component/Notifier/Event/FailedMessageEvent.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?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\Event; | ||
|
||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Contracts\EventDispatcher\Event; | ||
|
||
/** | ||
* @author Smaïne Milianni <smaine.milianni@gmail.com> | ||
*/ | ||
final class FailedMessageEvent extends Event | ||
{ | ||
private $message; | ||
private $error; | ||
|
||
public function __construct(MessageInterface $message, \Throwable $error) | ||
{ | ||
$this->message = $message; | ||
$this->error = $error; | ||
} | ||
|
||
public function getMessage(): MessageInterface | ||
{ | ||
return $this->message; | ||
} | ||
|
||
public function getError(): \Throwable | ||
{ | ||
return $this->error; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?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\Event; | ||
|
||
use Symfony\Component\Notifier\Message\SentMessage; | ||
use Symfony\Contracts\EventDispatcher\Event; | ||
|
||
/** | ||
* @author Smaïne Milianni <smaine.milianni@gmail.com> | ||
*/ | ||
final class SentMessageEvent extends Event | ||
{ | ||
private $message; | ||
|
||
public function __construct(SentMessage $message) | ||
{ | ||
$this->message = $message; | ||
} | ||
|
||
public function getMessage(): SentMessage | ||
{ | ||
return $this->message; | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
src/Symfony/Component/Notifier/Tests/Event/FailedMessageEventTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Notifier\Tests\Event; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Notifier\Event\FailedMessageEvent; | ||
use Symfony\Component\Notifier\Event\MessageEvent; | ||
use Symfony\Component\Notifier\Message\ChatMessage; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\SentMessage; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Component\Notifier\Tests\Transport\DummyMessage; | ||
use Symfony\Component\Notifier\Transport\AbstractTransport; | ||
use Symfony\Component\Notifier\Transport\NullTransport; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @author Smaïne Milianni <smaine.milianni@gmail.com> | ||
*/ | ||
final class FailedMessageEventTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider messagesProvider | ||
*/ | ||
public function testConstruct(MessageInterface $message, \Throwable $error, FailedMessageEvent $event) | ||
{ | ||
$this->assertEquals($event, new FailedMessageEvent($message, $error)); | ||
} | ||
|
||
/** | ||
* @dataProvider messagesProvider | ||
*/ | ||
public function testGetMessage(MessageInterface $message, \Throwable $error, FailedMessageEvent $event) | ||
{ | ||
$this->assertSame($message, $event->getMessage()); | ||
} | ||
|
||
/** | ||
* @dataProvider messagesProvider | ||
*/ | ||
public function testGetError(MessageInterface $message, \Throwable $error, FailedMessageEvent $event) | ||
{ | ||
$this->assertSame($error, $event->getError()); | ||
} | ||
|
||
public function testFailedMessageEventIsDisptachIfError() | ||
{ | ||
$eventDispatcherMock = $this->createMock(EventDispatcherInterface::class); | ||
$clientMock = $this->createMock(HttpClientInterface::class); | ||
|
||
$transport = new class($clientMock, $eventDispatcherMock) extends AbstractTransport { | ||
public function __construct($client, EventDispatcherInterface $dispatcher = null) | ||
{ | ||
$this->exception = new NullTransportException(); | ||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function doSend(MessageInterface $message): SentMessage | ||
{ | ||
throw $this->exception; | ||
} | ||
|
||
public function supports(MessageInterface $message): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
} | ||
}; | ||
|
||
$message = new DummyMessage(); | ||
|
||
$eventDispatcherMock->expects($this->exactly(2)) | ||
->method('dispatch') | ||
->withConsecutive( | ||
[new MessageEvent($message)], | ||
[new FailedMessageEvent($message, $transport->exception)] | ||
); | ||
try { | ||
$transport->send($message); | ||
} catch (NullTransportException $exception) { | ||
// catch Exception that is voluntary thrown in NullTransport::send | ||
} | ||
} | ||
|
||
public function messagesProvider(): iterable | ||
{ | ||
yield [$message = new ChatMessage('subject'), $error = new \RuntimeException(), new FailedMessageEvent($message, $error)]; | ||
yield [$message = new SmsMessage('+3312345678', 'subject'), $error = new \Exception(), new FailedMessageEvent($message, $error)]; | ||
} | ||
} | ||
|
||
class NullTransportException extends \Exception | ||
{ | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Symfony/Component/Notifier/Tests/Event/SentMessageEventTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Notifier\Tests\Event; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Notifier\Event\SentMessageEvent; | ||
use Symfony\Component\Notifier\Message\ChatMessage; | ||
use Symfony\Component\Notifier\Message\SentMessage; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
|
||
/** | ||
* @author Smaïne Milianni <smaine.milianni@gmail.com> | ||
*/ | ||
final class SentMessageEventTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider messagesProvider | ||
*/ | ||
public function testConstruct(SentMessage $message, SentMessageEvent $event) | ||
{ | ||
$this->assertEquals($event, new SentMessageEvent($message)); | ||
} | ||
|
||
/** | ||
* @dataProvider messagesProvider | ||
*/ | ||
public function testGetMessage(SentMessage $message, SentMessageEvent $event) | ||
{ | ||
$this->assertSame($message, $event->getMessage()); | ||
} | ||
|
||
public function messagesProvider(): iterable | ||
{ | ||
yield [$message = new SentMessage(new ChatMessage('subject'), 'null_transport'), new SentMessageEvent($message)]; | ||
yield [$message = new SentMessage(new SmsMessage('+3312345678', 'subject'), 'null_transport'), new SentMessageEvent($message)]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.