Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b664d75

Browse files
committed
keep boolean options when their value is false
1 parent 48d093b commit b664d75

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/Symfony/Component/Notifier/Bridge/Slack/SlackTransport.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected function doSend(MessageInterface $message): SentMessage
8383
}
8484
$options['text'] = $message->getSubject();
8585
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/api/chat.postMessage', [
86-
'json' => array_filter($options),
86+
'json' => array_filter($options, function ($value): bool { return !\in_array($value, ['', [], null], true); }),
8787
'auth_bearer' => $this->accessToken,
8888
'headers' => [
8989
'Content-Type' => 'application/json; charset=utf-8',

src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackTransportTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,56 @@ public function testSendWithNotification()
174174
$this->assertSame('1503435956.000247', $sentMessage->getMessageId());
175175
}
176176

177+
/**
178+
* @testWith [true]
179+
* [false]
180+
*/
181+
public function testSendWithBooleanOptionValue(bool $value)
182+
{
183+
$channel = 'testChannel';
184+
$message = 'testMessage';
185+
186+
$response = $this->createMock(ResponseInterface::class);
187+
188+
$response->expects($this->exactly(2))
189+
->method('getStatusCode')
190+
->willReturn(200);
191+
192+
$response->expects($this->once())
193+
->method('getContent')
194+
->willReturn(json_encode(['ok' => true, 'ts' => '1503435956.000247']));
195+
196+
$options = new SlackOptions();
197+
$options->asUser($value);
198+
$options->linkNames($value);
199+
$options->mrkdwn($value);
200+
$options->unfurlLinks($value);
201+
$options->unfurlMedia($value);
202+
$notification = new Notification($message);
203+
$chatMessage = ChatMessage::fromNotification($notification);
204+
$chatMessage->options($options);
205+
206+
$expectedBody = json_encode([
207+
'as_user' => $value,
208+
'channel' => $channel,
209+
'link_names' => $value,
210+
'mrkdwn' => $value,
211+
'text' => $message,
212+
'unfurl_links' => $value,
213+
'unfurl_media' => $value,
214+
]);
215+
216+
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface {
217+
$this->assertJsonStringEqualsJsonString($expectedBody, $options['body']);
218+
219+
return $response;
220+
});
221+
222+
$transport = self::createTransport($client, $channel);
223+
224+
$transport->send($chatMessage);
225+
}
226+
177227
public function testSendWithInvalidOptions()
178228
{
179229
$this->expectException(LogicException::class);

0 commit comments

Comments
 (0)