Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Notifier] [Lox24] Fix request body format to JSON string #57447

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
Jun 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected function doSend(MessageInterface $message): SentMessage
'Content-Type' => 'application/json',
'User-Agent' => 'LOX24 Symfony Notifier',
],
'body' => $body,
'json' => $body,
]);

try {
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Component/Notifier/Bridge/Lox24/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ DSN example
-----------

```
LOX24_DSN=lox24://USER:TOKEN@default?from=FROM&type=SERVICE_CODE&voice_lang=VOICE_LANGUAGE&delete_text=DELETE_TEXT&callback_data=CALLBACK_DATA
LOX24_DSN=lox24://USER:TOKEN@default?from=FROM&type=TYPE&voice_lang=VOICE_LANGUAGE&delete_text=DELETE_TEXT&callback_data=CALLBACK_DATA
```

where:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
* file that was distributed with this source code.
*/

use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\JsonMockResponse;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Notifier\Bridge\Lox24\Lox24Options;
use Symfony\Component\Notifier\Bridge\Lox24\Lox24Transport;
use Symfony\Component\Notifier\Bridge\Lox24\Type;
Expand All @@ -25,7 +26,6 @@
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Test\TransportTestCase;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

/**
* @author Andrei Lebedev <andrew.lebedev@gmail.com>
Expand All @@ -48,13 +48,6 @@ class Lox24TransportTest extends TransportTestCase
'service_code' => 'direct',
];

private MockObject|HttpClientInterface $client;

protected function setUp(): void
{
$this->client = $this->createMock(HttpClientInterface::class);
}

public static function createTransport(?HttpClientInterface $client = null): Lox24Transport
{
return (new Lox24Transport('user', 'token', 'sender', ['type' => 'voice'], $client ?? new MockHttpClient()))->setHost('host.test');
Expand Down Expand Up @@ -102,30 +95,31 @@ public function testSendWithInvalidMessageType()

public function testMessageFromNotEmpty()
{
$this->assertRequestBody([
$client = $this->mockHttpClient([
'sender_id' => 'testFrom2',
'phone' => '+1411111111',
'text' => 'test text',
'is_text_deleted' => false,
'delivery_at' => 0,
'service_code' => 'direct',
], [], 201, ['uuid' => '123456']);
$transport = new Lox24Transport('user', 'token', 'testFrom', [], $this->client);

$transport = new Lox24Transport('user', 'token', 'testFrom', [], $client);
$message = new SmsMessage('+1411111111', 'test text', 'testFrom2');
$transport->send($message);
}

public function testMessageFromEmpty()
{
$this->assertRequestBody([
$client = $this->mockHttpClient([
'sender_id' => 'testFrom',
'phone' => '+1411111111',
'text' => 'test text',
'is_text_deleted' => false,
'delivery_at' => 0,
'service_code' => 'direct',
], [], 201, ['uuid' => '123456']);
$transport = new Lox24Transport('user', 'token', 'testFrom', [], $this->client);
$transport = new Lox24Transport('user', 'token', 'testFrom', [], $client);
$message = new SmsMessage('+1411111111', 'test text');
$transport->send($message);
}
Expand All @@ -143,15 +137,15 @@ public function testMessageFromInvalid()

public function testOptionIsTextDeleted()
{
$this->assertRequestBody([
$client = $this->mockHttpClient([
'sender_id' => 'testFrom',
'phone' => '+1411111111',
'text' => 'test text',
'is_text_deleted' => true,
'delivery_at' => 0,
'service_code' => 'direct',
], [], 201, ['uuid' => '123456']);
$transport = new Lox24Transport('user', 'token', 'testFrom', [], $this->client);
$transport = new Lox24Transport('user', 'token', 'testFrom', [], $client);

$options = (new Lox24Options())->deleteTextAfterSending(true);
$message = new SmsMessage('+1411111111', 'test text');
Expand All @@ -162,15 +156,15 @@ public function testOptionIsTextDeleted()

public function testOptionDeliveryAtGreaterThanZero()
{
$this->assertRequestBody([
$client = $this->mockHttpClient([
'sender_id' => 'testFrom',
'phone' => '+1411111111',
'text' => 'test text',
'is_text_deleted' => false,
'delivery_at' => 1000000000,
'service_code' => 'direct',
], [], 201, ['uuid' => '123456']);
$transport = new Lox24Transport('user', 'token', 'testFrom', [], $this->client);
$transport = new Lox24Transport('user', 'token', 'testFrom', [], $client);

$options = (new Lox24Options())->deliveryAt((new DateTimeImmutable())->setTimestamp(1000000000));
$message = new SmsMessage('+1411111111', 'test text');
Expand All @@ -181,7 +175,7 @@ public function testOptionDeliveryAtGreaterThanZero()

public function testOptionVoiceLanguageSpanish()
{
$this->assertRequestBody([
$client = $this->mockHttpClient([
'sender_id' => 'testFrom',
'phone' => '+1411111111',
'text' => 'test text',
Expand All @@ -190,7 +184,7 @@ public function testOptionVoiceLanguageSpanish()
'service_code' => 'text2speech',
'voice_lang' => 'ES',
], [], 201, ['uuid' => '123456']);
$transport = new Lox24Transport('user', 'token', 'testFrom', [], $this->client);
$transport = new Lox24Transport('user', 'token', 'testFrom', [], $client);

$options = (new Lox24Options())
->voiceLanguage(VoiceLanguage::Spanish)
Expand All @@ -203,15 +197,15 @@ public function testOptionVoiceLanguageSpanish()

public function testOptionVoiceLanguageAuto()
{
$this->assertRequestBody([
$client = $this->mockHttpClient([
'sender_id' => 'testFrom',
'phone' => '+1411111111',
'text' => 'test text',
'is_text_deleted' => false,
'delivery_at' => 0,
'service_code' => 'text2speech',
], [], 201, ['uuid' => '123456']);
$transport = new Lox24Transport('user', 'token', 'testFrom', [], $this->client);
$transport = new Lox24Transport('user', 'token', 'testFrom', [], $client);

$options = (new Lox24Options())
->voiceLanguage(VoiceLanguage::Auto)
Expand All @@ -224,7 +218,7 @@ public function testOptionVoiceLanguageAuto()

public function testOptionType()
{
$this->assertRequestBody([
$client = $this->mockHttpClient([
'sender_id' => 'testFrom',
'phone' => '+1411111111',
'text' => 'test text',
Expand All @@ -233,7 +227,7 @@ public function testOptionType()
'service_code' => 'direct',
], [], 201, ['uuid' => '123456']);

$transport = new Lox24Transport('user', 'token', 'testFrom', ['type' => 'voice'], $this->client);
$transport = new Lox24Transport('user', 'token', 'testFrom', ['type' => 'voice'], $client);

$options = (new Lox24Options())->type(Type::Sms);
$message = new SmsMessage('+1411111111', 'test text');
Expand All @@ -244,7 +238,7 @@ public function testOptionType()

public function testOptionCallbackData()
{
$this->assertRequestBody([
$client = $this->mockHttpClient([
'sender_id' => 'testFrom',
'phone' => '+1411111111',
'text' => 'test text',
Expand All @@ -254,7 +248,7 @@ public function testOptionCallbackData()
'callback_data' => 'callback_data',
], [], 201, ['uuid' => '123456']);

$transport = new Lox24Transport('user', 'token', 'testFrom', ['type' => 'voice'], $this->client);
$transport = new Lox24Transport('user', 'token', 'testFrom', ['type' => 'voice'], $client);

$options = (new Lox24Options())->callbackData('callback_data');
$message = new SmsMessage('+1411111111', 'test text');
Expand All @@ -270,7 +264,7 @@ public function testResponseStatusCodeNotEqual201()
'Unable to send the SMS: "service_code: Service\'s code is invalid or unavailable.".'
);

$this->assertRequestBody([
$client = $this->mockHttpClient([
'sender_id' => 'testFrom',
'phone' => '+1411111111',
'text' => 'test text',
Expand All @@ -294,28 +288,56 @@ public function testResponseStatusCodeNotEqual201()
],
);

$transport = new Lox24Transport('user', 'token', 'testFrom', [], $this->client);
$transport = new Lox24Transport('user', 'token', 'testFrom', [], $client);

$message = new SmsMessage('+1411111111', 'test text');
$transport->send($message);
}

private function assertRequestBody(
public function mockHttpClient(
array $bodyOverride = [],
array $headersOverride = [],
int $responseStatus = 200,
array $responseContent = [],
): void {
$body = array_merge(self::REQUEST_BODY, $bodyOverride);
): MockHttpClient {
$body = json_encode(array_merge(self::REQUEST_BODY, $bodyOverride));
$headers = array_merge(self::REQUEST_HEADERS, $headersOverride);
$response = $this->createMock(ResponseInterface::class);
$response->expects($this->once())->method('getStatusCode')->willReturn($responseStatus);
$response->expects($this->once())->method('toArray')->willReturn($responseContent);
$this->client->expects($this->once())
->method('request')
->with('POST', 'https://api.lox24.eu/sms', [
'body' => $body,
'headers' => $headers,
])->willReturn($response);

$factory = function ($method, $url, $options) use (
$body,
$headers,
$responseStatus,
$responseContent
): MockResponse {
$this->assertSame('POST', $method);
$this->assertSame('https://api.lox24.eu/sms', $url);
$this->assertHeaders($headers, $options['headers']);
$this->assertJsonStringEqualsJsonString($body, $options['body']);

return new JsonMockResponse($responseContent, [
'http_code' => $responseStatus,
'headers' => ['content-type' => 'application/json'],
]);
};

return new MockHttpClient($factory);
}

private function assertHeaders(array $expected, array $headers): void
{
foreach ($this->normalizeHeaders($expected) as $expectedHeader) {
$headerExists = in_array($expectedHeader, $headers, true);
$this->assertTrue($headerExists, "Header '$expectedHeader' not found in request's headers");
}
}

private function normalizeHeaders(array $headers): array
{
$normalized = [];
foreach ($headers as $key => $value) {
$normalized[] = $key.': '.$value;
}

return $normalized;
}
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.