feature #39410 [Notifier] Add HeaderBlock for slack notifier (norkunas)
This PR was merged into the 5.3-dev branch.
Discussion
----------
[Notifier] Add HeaderBlock for slack notifier
| Q | A
| ------------- | ---
| Branch? | 5.x
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Tickets | Fix #...
| License | MIT
| Doc PR | symfony/symfony-docs#...
Commits
-------
421d01b872
Add HeaderBlock for slack notifier
This commit is contained in:
commit
e9729550b2
@ -0,0 +1,51 @@
|
||||
<?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\Block;
|
||||
|
||||
use Symfony\Component\Notifier\Exception\LogicException;
|
||||
|
||||
/**
|
||||
* @author Tomas Norkūnas <norkunas.tom@gmail.com>
|
||||
*
|
||||
* @experimental in 5.3
|
||||
*/
|
||||
final class SlackHeaderBlock extends AbstractSlackBlock
|
||||
{
|
||||
private const TEXT_LIMIT = 150;
|
||||
private const ID_LIMIT = 255;
|
||||
|
||||
public function __construct(string $text)
|
||||
{
|
||||
if (\strlen($text) > self::TEXT_LIMIT) {
|
||||
throw new LogicException(sprintf('Maximum length for the text is %d characters.', self::TEXT_LIMIT));
|
||||
}
|
||||
|
||||
$this->options = [
|
||||
'type' => 'header',
|
||||
'text' => [
|
||||
'type' => 'plain_text',
|
||||
'text' => $text,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function id(string $id): self
|
||||
{
|
||||
if (\strlen($id) > self::ID_LIMIT) {
|
||||
throw new LogicException(sprintf('Maximum length for the block id is %d characters.', self::ID_LIMIT));
|
||||
}
|
||||
|
||||
$this->options['block_id'] = $id;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ CHANGELOG
|
||||
-----
|
||||
|
||||
* Check for maximum number of buttons in Slack action block
|
||||
* Add HeaderBlock
|
||||
|
||||
5.2.0
|
||||
-----
|
||||
|
@ -0,0 +1,51 @@
|
||||
<?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\SlackHeaderBlock;
|
||||
use Symfony\Component\Notifier\Exception\LogicException;
|
||||
|
||||
final class SlackHeaderBlockTest extends TestCase
|
||||
{
|
||||
public function testCanBeInstantiated(): void
|
||||
{
|
||||
$header = new SlackHeaderBlock('header text');
|
||||
$header->id('header_id');
|
||||
|
||||
$this->assertSame([
|
||||
'type' => 'header',
|
||||
'text' => [
|
||||
'type' => 'plain_text',
|
||||
'text' => 'header text',
|
||||
],
|
||||
'block_id' => 'header_id',
|
||||
], $header->toArray());
|
||||
}
|
||||
|
||||
public function testThrowsWhenTextExceedsCharacterLimit(): void
|
||||
{
|
||||
$this->expectException(LogicException::class);
|
||||
$this->expectExceptionMessage('Maximum length for the text is 150 characters.');
|
||||
|
||||
new SlackHeaderBlock(str_repeat('h', 151));
|
||||
}
|
||||
|
||||
public function testThrowsWhenBlockIdExceedsCharacterLimit(): void
|
||||
{
|
||||
$this->expectException(LogicException::class);
|
||||
$this->expectExceptionMessage('Maximum length for the block id is 255 characters.');
|
||||
|
||||
$header = new SlackHeaderBlock('header');
|
||||
$header->id(str_repeat('h', 256));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user