From a7936d2b0e4be96392100b24aa5d2bd4de9ab5e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20Schlu=CC=88ter?= Date: Thu, 3 Dec 2020 12:10:35 +0100 Subject: [PATCH] [Notifier] Check for maximum number of buttons in slack action block --- .../Bridge/Slack/Block/SlackActionsBlock.php | 4 ++ .../Notifier/Bridge/Slack/CHANGELOG.md | 5 ++ .../Tests/Block/SlackActionsBlockTest.php | 62 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackActionsBlockTest.php diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackActionsBlock.php b/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackActionsBlock.php index 85b63d2109..25c4c06338 100644 --- a/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackActionsBlock.php +++ b/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackActionsBlock.php @@ -26,6 +26,10 @@ final class SlackActionsBlock extends AbstractSlackBlock */ public function button(string $text, string $url, string $style = null): self { + if (25 === \count($this->options['elements'] ?? [])) { + throw new \LogicException('Maximum number of buttons should not exceed 25.'); + } + $element = [ 'type' => 'button', 'text' => [ diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/CHANGELOG.md b/src/Symfony/Component/Notifier/Bridge/Slack/CHANGELOG.md index fcc1eb79f8..b860fa08db 100644 --- a/src/Symfony/Component/Notifier/Bridge/Slack/CHANGELOG.md +++ b/src/Symfony/Component/Notifier/Bridge/Slack/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +5.3.0 +----- + + * Check for maximum number of buttons in Slack action block + 5.2.0 ----- diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackActionsBlockTest.php b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackActionsBlockTest.php new file mode 100644 index 0000000000..fba5ccf7c5 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackActionsBlockTest.php @@ -0,0 +1,62 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Bridge\Slack\Tests\Block; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Notifier\Bridge\Slack\Block\SlackActionsBlock; + +final class SlackActionsBlockTest extends TestCase +{ + public function testCanBeInstantiated(): void + { + $actions = new SlackActionsBlock(); + $actions->button('first button text', 'https://example.org') + ->button('second button text', 'https://example.org/slack', 'danger') + ; + + $this->assertSame([ + 'type' => 'actions', + 'elements' => [ + [ + 'type' => 'button', + 'text' => [ + 'type' => 'plain_text', + 'text' => 'first button text', + ], + 'url' => 'https://example.org', + ], + [ + 'type' => 'button', + 'text' => [ + 'type' => 'plain_text', + 'text' => 'second button text', + ], + 'url' => 'https://example.org/slack', + 'style' => 'danger', + ], + ], + ], $actions->toArray()); + } + + public function testThrowsWhenFieldsLimitReached(): void + { + $section = new SlackActionsBlock(); + for ($i = 0; $i < 25; ++$i) { + $section->button($i, $i); + } + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Maximum number of buttons should not exceed 25.'); + + $section->button('fail', 'fail'); + } +}