From 4faa677f223a1d8d1a6148a0ad6b445413c57af7 Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 14 Jul 2021 06:30:50 +0300 Subject: [PATCH] Throw error if maximum block limit is reached for slack message options --- .../Notifier/Bridge/Slack/SlackOptions.php | 11 +++++ .../Bridge/Slack/Tests/SlackOptionsTest.php | 40 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/SlackOptions.php b/src/Symfony/Component/Notifier/Bridge/Slack/SlackOptions.php index 4073d480ebe68..86c391f2cda90 100644 --- a/src/Symfony/Component/Notifier/Bridge/Slack/SlackOptions.php +++ b/src/Symfony/Component/Notifier/Bridge/Slack/SlackOptions.php @@ -14,6 +14,7 @@ use Symfony\Component\Notifier\Bridge\Slack\Block\SlackBlockInterface; use Symfony\Component\Notifier\Bridge\Slack\Block\SlackDividerBlock; use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock; +use Symfony\Component\Notifier\Exception\LogicException; use Symfony\Component\Notifier\Message\MessageOptionsInterface; use Symfony\Component\Notifier\Notification\Notification; @@ -22,11 +23,17 @@ */ final class SlackOptions implements MessageOptionsInterface { + private const MAX_BLOCKS = 50; + private $options; public function __construct(array $options = []) { $this->options = $options; + + if (\count($this->options['blocks'] ?? []) > self::MAX_BLOCKS) { + throw new LogicException(sprintf('Maximum number of "blocks" has been reached (%d).', self::MAX_BLOCKS)); + } } public static function fromNotification(Notification $notification): self @@ -97,6 +104,10 @@ public function asUser(bool $bool): self */ public function block(SlackBlockInterface $block): self { + if (\count($this->options['blocks'] ?? []) >= self::MAX_BLOCKS) { + throw new LogicException(sprintf('Maximum number of "blocks" has been reached (%d).', self::MAX_BLOCKS)); + } + $this->options['blocks'][] = $block->toArray(); return $this; diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackOptionsTest.php b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackOptionsTest.php index ffe6440ad6d94..82900d6d7e136 100644 --- a/src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackOptionsTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackOptionsTest.php @@ -14,7 +14,9 @@ use PHPUnit\Framework\TestCase; use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Symfony\Component\Notifier\Bridge\Slack\Block\SlackDividerBlock; +use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock; use Symfony\Component\Notifier\Bridge\Slack\SlackOptions; +use Symfony\Component\Notifier\Exception\LogicException; use Symfony\Component\Notifier\Notification\Notification; /** @@ -187,4 +189,42 @@ public function fromNotificationProvider(): iterable (new Notification($subject))->emoji($emoji)->content($content), ]; } + + public function testConstructWithMaximumBlocks() + { + $options = new SlackOptions(['blocks' => array_map(static function () { return ['type' => 'divider']; }, range(0, 49))]); + + $this->assertCount(50, $options->toArray()['blocks']); + } + + public function testConstructThrowsWithTooManyBlocks() + { + $this->expectException(LogicException::class); + $this->expectExceptionMessage('Maximum number of "blocks" has been reached (50).'); + + new SlackOptions(['blocks' => array_map(static function () { return ['type' => 'divider']; }, range(0, 50))]); + } + + public function testAddMaximumBlocks() + { + $options = new SlackOptions(); + for ($i = 0; $i < 50; ++$i) { + $options->block(new SlackSectionBlock()); + } + + $this->assertCount(50, $options->toArray()['blocks']); + } + + public function testThrowsWhenBlocksLimitReached() + { + $options = new SlackOptions(); + for ($i = 0; $i < 50; ++$i) { + $options->block(new SlackSectionBlock()); + } + + $this->expectException(LogicException::class); + $this->expectExceptionMessage('Maximum number of "blocks" has been reached (50).'); + + $options->block(new SlackSectionBlock()); + } }