Skip to content

[Notifier] [Slack] Throw error if maximum block limit is reached for slack message options #42094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Slack/SlackOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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());
}
}