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

Skip to content

Commit 4c8bade

Browse files
committed
Add ContextBlock for slack notifier
1 parent ff97b5f commit 4c8bade

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
declare(strict_types=1);
13+
14+
namespace Symfony\Component\Notifier\Bridge\Slack\Block;
15+
16+
final class SlackContextBlock extends AbstractSlackBlock
17+
{
18+
public function __construct()
19+
{
20+
$this->options['type'] = 'context';
21+
}
22+
23+
/**
24+
* @return $this
25+
*/
26+
public function text(string $text, bool $markdown = true, bool $emoji = true, bool $verbatim = false): self
27+
{
28+
if (10 === \count($this->options['elements'] ?? [])) {
29+
throw new \LogicException('Maximum number of elements should not exceed 10.');
30+
}
31+
32+
$element = [
33+
'type' => $markdown ? 'mrkdwn' : 'plain_text',
34+
'text' => $text,
35+
];
36+
if (!$markdown) {
37+
$element['emoji'] = $emoji;
38+
$element['verbatim'] = $verbatim;
39+
}
40+
$this->options['elements'][] = $element;
41+
42+
return $this;
43+
}
44+
45+
/**
46+
* @return $this
47+
*/
48+
public function image(string $url, string $text): self
49+
{
50+
if (10 === \count($this->options['elements'] ?? [])) {
51+
throw new \LogicException('Maximum number of elements should not exceed 10.');
52+
}
53+
54+
$this->options['elements'][] = [
55+
'type' => 'image',
56+
'image_url' => $url,
57+
'alt_text' => $text,
58+
];
59+
60+
return $this;
61+
}
62+
63+
/**
64+
* @return $this
65+
*/
66+
public function id(string $id): self
67+
{
68+
$this->options['block_id'] = $id;
69+
70+
return $this;
71+
}
72+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
< 8000 /td>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\SlackContextBlock;
16+
17+
final class SlackContextBlockTest extends TestCase
18+
{
19+
public function testCanBeInstantiated(): void
20+
{
21+
$context = new SlackContextBlock();
22+
$context->text('context text');
23+
$context->image('https://example.com/image.jpg', 'an image');
24+
$context->id('context_id');
25+
26+
$this->assertSame([
27+
'type' => 'context',
28+
'elements' => [
29+
[
30+
'type' => 'mrkdwn',
31+
'text' => 'context text',
32+
],
33+
[
34+
'type' => 'image',
35+
'image_url' => 'https://example.com/image.jpg',
36+
'alt_text' => 'an image',
37+
],
38+
],
39+
'block_id' => 'context_id',
40+
], $context->toArray());
41+
}
42+
43+
public function testSetsPropertiesForPlainText(): void
44+
{
45+
$context = new SlackContextBlock();
46+
$context->text('plain context text', false, false, true);
47+
48+
$this->assertSame([
49+
'type' => 'context',
50+
'elements' => [
51+
[
52+
'type' => 'plain_text',
53+
'text' => 'plain context text',
54+
'emoji' => false,
55+
'verbatim' => true,
56+
],
57+
],
58+
], $context->toArray());
59+
}
60+
61+
public function testThrowsWhenElementsLimitReached(): void
62+
{
63+
$context = new SlackContextBlock();
64+
for ($i = 0; $i < 10; ++$i) {
65+
if ($i % 2 === 0) {
66+
$context->text($i);
67+
} else {
68+
$context->image($i, $i);
69+
}
70+
}
71+
72+
$this->expectException(\LogicException::class);
73+
$this->expectExceptionMessage('Maximum number of elements should not exceed 10.');
74+
75+
$context->text('fail');
76+
}
77+
}

0 commit comments

Comments
 (0)
0