Skip to content

[Notifier] Add Esendex message ID to SentMessage object #42748

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
merged 1 commit into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Esendex/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
CHANGELOG
=========

* Add returned message ID to `SentMessage`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Add returned message ID to `SentMessage`
5.4
---
* Add returned message ID to `SentMessage`


5.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ protected function doSend(MessageInterface $message): SentMessage

$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/v1.0/messagedispatcher', [
'auth_basic' => sprintf('%s:%s', $this->email, $this->password),
'headers' => [
'Accept' => 'application/json',
],
'json' => [
'accountreference' => $this->accountReference,
'messages' => [$messageData],
Expand All @@ -82,7 +85,15 @@ protected function doSend(MessageInterface $message): SentMessage
}

if (200 === $statusCode) {
return new SentMessage($message, (string) $this);
$result = $response->toArray();
$sentMessage = new SentMessage($message, (string) $this);

$messageId = $result['batch']['messageheaders'][0]['id'] ?? null;
if ($messageId) {
$sentMessage->setMessageId($messageId);
}

return $sentMessage;
}

$message = sprintf('Unable to send the SMS: error %d.', $statusCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Test\TransportTestCase;
use Symfony\Component\Notifier\Transport\TransportInterface;
use Symfony\Component\Uid\Uuid;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

Expand Down Expand Up @@ -88,4 +89,26 @@ public function testSendWithErrorResponseContainingDetailsThrowsTransportExcepti

$transport->send(new SmsMessage('phone', 'testMessage'));
}

public function testSendWithSuccessfulResponseDispatchesMessageEvent()
{
$messageId = Uuid::v4()->toRfc4122();
$response = $this->createMock(ResponseInterface::class);
$response->expects($this->exactly(2))
->method('getStatusCode')
->willReturn(200);
$response->expects($this->once())
->method('getContent')
->willReturn(json_encode(['batch' => ['messageheaders' => [['id' => $messageId]]]]));

$client = new MockHttpClient(static function () use ($response): ResponseInterface {
return $response;
});

$transport = $this->createTransport($client);

$sentMessage = $transport->send(new SmsMessage('phone', 'testMessage'));

$this->assertSame($messageId, $sentMessage->getMessageId());
}
}