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

Skip to content

Commit c8353be

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

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
final class SlackHeaderBlock extends AbstractSlackBlock
15+
{
16+
private const TEXT_LIMIT = 150;
17+
private const ID_LIMIT = 255;
18+
19+
public function __construct(string $text)
20+
{
21+
if (\strlen($text) > self::TEXT_LIMIT) {
22+
throw new \LogicException(sprintf('Maximum length for the text is %d characters.', self::TEXT_LIMIT));
23+
}
24+
25+
$this->options = [
26+
'type' => 'header',
27+
'text' => [
28+
'type' => 'plain_text',
29+
'text' => $text,
30+
],
31+
];
32+
}
33+
34+
public function id(string $id): self
35+
{
36+
if (\strlen($id) > self::ID_LIMIT) {
37+
throw new \LogicException(sprintf('Maximum length for the block id is %d characters.', self::ID_LIMIT));
38+
}
39+
40+
$this->options['block_id'] = $id;
41+
42+
return $this;
43+
}
44+
}

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 for slack notifier
89

910
5.2.0
1011
-----
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
17+
final class SlackHeaderBlockTest extends TestCase
18+
{
19+
public function testCanBeInstantiated(): void
20+
{
21+
$header = new SlackHeaderBlock('header text');
22+
$header->id('header_id');
23+
24+
$this->assertSame([
25+
'type' => 'header',
26+
'text' => [
27+
'type' => 'plain_text',
28+
'text' => 'header text',
29+
],
30+
'block_id' => 'header_id',
31+
], $header->toArray());
32+
}
33+
34+
public function testThrowsWhenTextExceedsCharacterLimit(): void
35+
{
36+
$this->expectException(\LogicException::class);
37+
$this->expectExceptionMessage('Maximum length for the text is 150 characters.');
38+
39+
new SlackHeaderBlock(str_repeat('h', 151));
40+
}
41+
42+
public function testThrowsWhenBlockIdExceedsCharacterLimit(): void
43+
{
44+
$this->expectException(\LogicException::class);
45+
$this->expectExceptionMessage('Maximum length for the block id is 255 characters.');
46+
47+
$header = new SlackHeaderBlock('header');
48+
$header->id(str_repeat('h', 256));
49+
}
50+
}

0 commit comments

Comments
 (0)
0