Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bfb2cdb

Browse filesBrowse files
committed
Add ContextBlock for slack notifier
1 parent ff97b5f commit bfb2cdb
Copy full SHA for bfb2cdb

File tree

2 files changed

+150
-0
lines changed
Filter options

2 files changed

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.