8000 Add HeaderBlock for slack notifier · symfony/symfony@421d01b · GitHub
[go: up one dir, main page]

Skip to content

Commit 421d01b

Browse files
committed
Add HeaderBlock for slack notifier
1 parent fda67f5 commit 421d01b

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Slack\Block;
13+
14+
use Symfony\Component\Notifier\Exception\LogicException;
15+
16+
/**
17+
* @author Tomas Norkūnas <norkunas.tom@gmail.com>
18+
*
19+
* @experimental in 5.3
20+
*/
21+
final class SlackHeaderBlock extends AbstractSlackBlock
22+
{
23+
private const TEXT_LIMIT = 150;
24+
private const ID_LIMIT = 255;
25+
26+
public function __construct(string $text)
27+
{
28+
if (\strlen($text) > self::TEXT_LIMIT) {
29+
throw new LogicException(sprintf('Maximum length for the text is %d characters.', self::TEXT_LIMIT));
30+
}
31+
32+
$this->options = [
33+
'type' => 'header',
34+
'text' => [
35+
'type' => 'plain_text',
36+
'text' => $text,
37+
],
38+
];
39+
}
40+
41+
public function id(string $id): self
42+
{
43+
if (\strlen($id) > self::ID_LIMIT) {
44+
throw new LogicException(sprintf('Maximum length for the block id is %d characters.', self::ID_LIMIT));
45+
}
46+
47+
$this->options['block_id'] = $id;
48+
49+
return $this;
50+
}
51+
}

src/Symfony/Component/Notifier/Bridge/Slack/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* Check for maximum number of buttons in Slack action block
8+
* Add HeaderBlock
89

910
5.2.0
1011
-----
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Slack\Tests\Block;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackHeaderBlock;
16+
use Symfony\Component\Notifier\Exception\LogicException;
17+
18+
final class SlackHeaderBlockTest extends TestCase
19+
{
20+
public function testCanBeInstantiated(): void
21+
{
22+
$header = new SlackHeaderBlock('header text');
23+
$header->id('header_id');
24+
25+
$this->assertSame([
26+
'type' => 'header',
27+
'text' => [
28+
'type' => 'plain_text',
29+
'text' => 'header text',
30+
],
31+
'block_id' => 'header_id',
32+
], $header->toArray());
33+
}
34+
35+
public function testThrowsWhenTextExceedsCharacterLimit(): void
36+
{
37+
$this->expectException(LogicException::class);
38+
$this->expectExceptionMessage('Maximum length for the text is 150 characters.');
39+
40+
new SlackHeaderBlock(str_repeat('h', 151));
41+
}
42+
43+
public function testThrowsWhenBlockIdExceedsCharacterLimit(): void
44+
{
45+
$this->expectException(LogicException::class);
46+
$this->expectExceptionMessage('Maximum length for the block id is 255 characters.');
47+
48+
$header = new SlackHeaderBlock('header');
49+
$header->id(str_repeat('h', 256));
50+
}
51+
}

0 commit comments

Comments
 (0)
0