From 6687e23c7d49eb9f873e93a7bd02978913b9a86e Mon Sep 17 00:00:00 2001 From: Tomas Date: Tue, 1 Dec 2020 06:41:25 +0200 Subject: [PATCH] Add ContextBlock for slack notifier --- .../Bridge/Slack/Block/SlackContextBlock.php | 66 +++++++++++++++++++ .../Tests/Block/SlackContextBlockTest.php | 66 +++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackContextBlock.php create mode 100644 src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackContextBlockTest.php diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackContextBlock.php b/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackContextBlock.php new file mode 100644 index 0000000000..8c6434321a --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackContextBlock.php @@ -0,0 +1,66 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Symfony\Component\Notifier\Bridge\Slack\Block; + +final class SlackContextBlock extends AbstractSlackBlock +{ + private const ELEMENT_LIMIT = 10; + + public function __construct() + { + $this->options['type'] = 'context'; + } + + public function text(string $text, bool $markdown = true, bool $emoji = true, bool $verbatim = false): self + { + if (self::ELEMENT_LIMIT === \count($this->options['elements'] ?? [])) { + throw new \LogicException(sprintf('Maximum number of elements should not exceed %d.', self::ELEMENT_LIMIT)); + } + + $element = [ + 'type' => $markdown ? 'mrkdwn' : 'plain_text', + 'text' => $text, + ]; + if ($markdown) { + $element['verbatim'] = $verbatim; + } else { + $element['emoji'] = $emoji; + } + $this->options['elements'][] = $element; + + return $this; + } + + public function image(string $url, string $text): self + { + if (self::ELEMENT_LIMIT === \count($this->options['elements'] ?? [])) { + throw new \LogicException(sprintf('Maximum number of elements should not exceed %d.', self::ELEMENT_LIMIT)); + } + + $this->options['elements'][] = [ + 'type' => 'image', + 'image_url' => $url, + 'alt_text' => $text, + ]; + + return $this; + } + + public function id(string $id): self + { + $this->options['block_id'] = $id; + + return $this; + } +} diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackContextBlockTest.php b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackContextBlockTest.php new file mode 100644 index 0000000000..2df45a4907 --- /dev/null +++ b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackContextBlockTest.php @@ -0,0 +1,66 @@ + + * + * 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\SlackContextBlock; + +final class SlackContextBlockTest extends TestCase +{ + public function testCanBeInstantiated(): void + { + $context = new SlackContextBlock(); + $context->text('context text without emoji', false, false); + $context->text('context text verbatim', true, false, true); + $context->image('https://example.com/image.jpg', 'an image'); + $context->id('context_id'); + + $this->assertSame([ + 'type' => 'context', + 'elements' => [ + [ + 'type' => 'plain_text', + 'text' => 'context text without emoji', + 'emoji' => false, + ], + [ + 'type' => 'mrkdwn', + 'text' => 'context text verbatim', + 'verbatim' => true, + ], + [ + 'type' => 'image', + 'image_url' => 'https://example.com/image.jpg', + 'alt_text' => 'an image', + ], + ], + 'block_id' => 'context_id', + ], $context->toArray()); + } + + public function testThrowsWhenElementsLimitReached(): void + { + $context = new SlackContextBlock(); + for ($i = 0; $i < 10; ++$i) { + if (0 === $i % 2) { + $context->text($i); + } else { + $context->image($i, $i); + } + } + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Maximum number of elements should not exceed 10.'); + + $context->text('fail'); + } +}