Add ContextBlock for slack notifier
This commit is contained in:
parent
ff97b5f17b
commit
6687e23c7d
@ -0,0 +1,66 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
<?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\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');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user