[Notifier] Check for maximum number of buttons in slack action block

This commit is contained in:
Malte Schlüter 2020-12-03 12:10:35 +01:00 committed by Fabien Potencier
parent 2b36487983
commit a7936d2b0e
3 changed files with 71 additions and 0 deletions

View File

@ -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' => [

View File

@ -1,6 +1,11 @@
CHANGELOG
=========
5.3.0
-----
* Check for maximum number of buttons in Slack action block
5.2.0
-----

View File

@ -0,0 +1,62 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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');
}
}