From d99db3eeb67ae72b5930b0ff220009ceaf6fddf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20Schlu=CC=88ter?= Date: Wed, 2 Dec 2020 10:58:35 +0100 Subject: [PATCH] Fix undefined index in field method --- .../Bridge/Slack/Block/SlackSectionBlock.php | 1 + .../Slack/Tests/SlackSectionBlockTest.php | 119 ++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackSectionBlockTest.php diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackSectionBlock.php b/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackSectionBlock.php index 8d37da429ed14..ab8a9af09ba6c 100644 --- a/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackSectionBlock.php +++ b/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackSectionBlock.php @@ -19,6 +19,7 @@ final class SlackSectionBlock extends AbstractSlackBlock public function __construct() { $this->options['type'] = 'section'; + $this->options['fields'] = []; } /** diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackSectionBlockTest.php b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackSectionBlockTest.php new file mode 100644 index 0000000000000..ce57a7a2e5418 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackSectionBlockTest.php @@ -0,0 +1,119 @@ + + * + * 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; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock; + +final class SlackSectionBlockTest extends TestCase +{ + /** + * @dataProvider provideTextData + */ + public function testSetText(array $expectedBlockArray, string $text, bool $markdown): void + { + $block = new SlackSectionBlock(); + $block->text($text, $markdown); + + $this->assertSame($expectedBlockArray, $block->toArray()); + } + + public function provideTextData(): iterable + { + yield [['type' => 'section', 'fields' => [], 'text' => ['type' => 'mrkdwn', 'text' => 'FooText']], 'FooText', true]; + yield [['type' => 'section', 'fields' => [], 'text' => ['type' => 'plain_text', 'text' => 'FooText']], 'FooText', false]; + } + + public function testSetTextTwice(): void + { + $block = new SlackSectionBlock(); + $block->text('FooText') + ->text('BarText', false) + ; + + $expectedBlockArray = [ + 'type' => 'section', + 'fields' => [], + 'text' => [ + 'type' => 'plain_text', + 'text' => 'BarText', + ], + ]; + + $this->assertSame($expectedBlockArray, $block->toArray()); + } + + public function testAddField(): void + { + $block = new SlackSectionBlock(); + + $block->field('FooText'); + + $expectedBlockArray = [ + 'type' => 'section', + 'fields' => [ + [ + 'type' => 'mrkdwn', + 'text' => 'FooText', + ], + ], + ]; + + $this->assertSame($expectedBlockArray, $block->toArray()); + } + + public function testAddMultipleFields(): void + { + $block = new SlackSectionBlock(); + + $block->field('FooText') + ->field('BarText', false) + ; + + $expectedBlockArray = [ + 'type' => 'section', + 'fields' => [ + [ + 'type' => 'mrkdwn', + 'text' => 'FooText', + ], + [ + 'type' => 'plain_text', + 'text' => 'BarText', + ], + ], + ]; + + $this->assertSame($expectedBlockArray, $block->toArray()); + } + + public function testAddTooManyFields(): void + { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Maximum number of fields should not exceed 10.'); + + $block = new SlackSectionBlock(); + + $block->field('FooText1') + ->field('FooText2') + ->field('FooText3') + ->field('FooText4') + ->field('FooText5') + ->field('FooText6') + ->field('FooText7') + ->field('FooText8') + ->field('FooText9') + ->field('FooText10') + ->field('FooText11') + ; + } +}