8000 Throw error if maximum block limit is reached for slack message options · symfony/symfony@db5e308 · GitHub
[go: up one dir, main page]

Skip to content

Commit db5e308

Browse files
committed
Throw error if maximum block limit is reached for slack message options
1 parent b5d4b33 commit db5e308

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackBlockInterface;
1515
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackDividerBlock;
1616
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
17+
use Symfony\Component\Notifier\Exception\LogicException;
1718
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
1819
use Symfony\Component\Notifier\Notification\Notification;
1920

@@ -22,11 +23,17 @@
2223
*/
2324
final class SlackOptions implements MessageOptionsInterface
2425
{
26+
private const MAX_BLOCKS = 50;
27+
2528
private $options;
2629

2730
public function __construct(array $options = [])
2831
{
2932
$this->options = $options;
33+
34+
if (\count($this->options['blocks'] ?? []) > self::MAX_BLOCKS) {
35+
throw new LogicException(sprintf('Maximum number of "blocks" has been reached (%d).', self::MAX_BLOCKS));
36+
}
3037
}
3138

3239
public static function fromNotification(Notification $notification): self
@@ -97,6 +104,10 @@ public function asUser(bool $bool): self
97104
*/
98105
public function block(SlackBlockInterface $block): self
99106
{
107+
if (\count($this->options['blocks'] ?? []) >= self::MAX_BLOCKS) {
108+
throw new LogicException(sprintf('Maximum number of "blocks" has been reached (%d).', self::MAX_BLOCKS));
109+
}
110+
100111
$this->options['blocks'][] = $block->toArray();
101112

102113
return $this;

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1616
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackDividerBlock;
17+
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
1718
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
19+
use Symfony\Component\Notifier\Exception\LogicException;
1820
use Symfony\Component\Notifier\Notification\Notification;
1921

2022
/**
@@ -187,4 +189,42 @@ public function fromNotificationProvider(): iterable
187189
(new Notification($subject))->emoji($emoji)->content($content),
188190
];
189191
}
192+
193+
public function testConstructWithMaximumBlocks()
194+
{
195+
$options = new SlackOptions(['blocks' => array_map(static function () { return ['type' => 'divider']; }, range(0, 49))]);
196+
197+
$this->assertCount(50, $options->toArray()['blocks']);
198+
}
199+
200+
public function testConstructThrowsWithTooManyBlocks()
201+
{
202+
$this->expectException(LogicException::class);
203+
$this->expectExceptionMessage('Maximum number of "blocks" has been reached (50).');
204+
205+
new SlackOptions(['blocks' => array_map(static function () { return ['type' => 'divider']; }, range(0, 50))]);
206+
}
207+
208+
public function testAddMaximumBlocks()
209+
{
210+
$options = new SlackOptions();
211+
for ($i = 0; $i < 50; ++$i) {
212+
$options->block(new SlackSectionBlock());
213+
}
214+
215+
$this->assertCount(50, $options->toArray()['blocks']);
216+
}
217+
218+
public function testThrowsWhenBlocksLimitReached()
219+
{
220+
$options = new SlackOptions();
221+
for ($i = 0; $i < 50; ++$i) {
222+
$options->block(new SlackSectionBlock());
223+
}
224+
225+
$this->expectException(LogicException::class);
226+
$this->expectExceptionMessage('Maximum number of "blocks" has been reached (50).');
227+
228+
$options->block(new SlackSectionBlock());
229+
}
190230
}

0 commit comments

Comments
 (0)
0