8000 feature #39300 [Notifier] Check for maximum number of buttons in slac… · symfony/symfony@53e1bc0 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 53e1bc0

Browse files
committed
feature #39300 [Notifier] Check for maximum number of buttons in slack action block (malteschlueter)
This PR was merged into the 5.3-dev branch. Discussion ---------- [Notifier] Check for maximum number of buttons in slack action block | Q | A | ------------- | --- | Branch? | 5.x | | Bug fix? | no | | New feature? | yes | | Deprecations? | no | | Tickets | - | | License | MIT | | Doc PR | - | After having problems with the SlackSectionBlock (#39236) i thought it would be helpful to also have a check for the maximum elements in the SlackActionsBlock and a test. https://api.slack.com/reference/block-kit/blocks#actions Edit: The actual documentation says that the maximum are 5 elements but this is outdated. The actual number is 25. The slack support confirmed that. Can this be added to 5.2 or better to the 5.x branch? There are also some other implementations of slack blocks like the SlackDividerBlock but they have only a constructor and no additional methods. Should we also add some tests for them even if they have no extra logic? Commits ------- a7936d2 [Notifier] Check for maximum number of buttons in slack action block
2 parents 31ee43a + a7936d2 commit 53e1bc0

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackActionsBlock.php

Lines changed: 4 additions & 0 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public function __construct()
2626
*/
2727
public function button(string $text, string $url, string $style = null): self
2828
{
29+
if (25 === \count($this->options['elements'] ?? [])) {
30+
throw new \LogicException('Maximum number of buttons should not exceed 25.');
31+
}
32+
2933
$element = [
3034
'type' => 'button',
3135
'text' => [

src/Symfony/Component/Notifier/Bridge/Slack/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.3.0
5+
-----
6+
7+
* Check for maximum number of buttons in Slack action block
8+
49
5.2.0
510
-----
611

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\Bridge\Slack\Tests\Block;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackActionsBlock;
16+
17+
final class SlackActionsBlockTest extends TestCase
18+
{
19+
public function testCanBeInstantiated(): void
20+
{
21+
$actions = new SlackActionsBlock();
22+
$actions->button('first button text', 'https://example.org')
23+
->button('second button text', 'https://example.org/slack', 'danger')
24+
;
25+
26+
$this->assertSame([
27+
'type' => 'actions',
28+
'elements' => [
29+
[
30+
'type' => 'button',
31+
'text' => [
32+
'type' => 'plain_text',
33+
'text' => 'first button text',
34+
],
35+
'url' => 'https://example.org',
36+
],
37+
[
38+
'type' => 'button',
39+
'text' => [
40+
'type' => 'plain_text',
41+
'text' => 'second button text',
42+
],
43+
'url' => 'https://example.org/slack',
44+
'style' => 'danger',
45+
],
46+
],
47+
], $actions->toArray());
48+
}
49+
50+
public function testThrowsWhenFieldsLimitReached(): void
51+
{
52+
$section = new SlackActionsBlock();
53+
for ($i = 0; $i < 25; ++$i) {
54+
$section->button($i, $i);
55+
}
56+
57+
$this->expectException(\LogicException::class);
58+
$this->expectExceptionMessage('Maximum number of buttons should not exceed 25.');
59+
60+
$section->button('fail', 'fail');
61+
}
62+
}

0 commit comments

Comments
 (0)
0