Skip to content

Commit 472fa3b

Browse files
committed
Fix checking slack section fields limit
1 parent 6440b70 commit 472fa3b

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackSectionBlock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function text(string $text, bool $markdown = true): self
3939
*/
4040
public function field(string $text, bool $markdown = true): self
4141
{
42-
if (10 === \count($this->options['fields'])) {
42+
if (10 === \count($this->options['fields'] ?? [])) {
4343
throw new \LogicException('Maximum number of fields should not exceed 10.');
4444
}
4545

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\SlackImageBlockElement;
16+
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;
17+
18+
final class SlackSectionBlockTest extends TestCase
19+
{
20+
public function testCanBeInstantiated(): void
21+
{
22+
$section = new SlackSectionBlock();
23+
$section->text('section text');
24+
$section->field('section field');
25+
$section->accessory(new SlackImageBlockElement('https://example.com/image.jpg', 'an image'));
26+
27+
$this->assertSame([
28+
'type' => 'section',
29+
'text' => [
30+
'type' => 'mrkdwn',
31+
'text' => 'section text',
32+
],
33+
'fields' => [
34+
[
35+
'type' => 'mrkdwn',
36+
'text' => 'section field',
37+
],
38+
],
39+
'accessory' => [
40+
'type' => 'image',
41+
'image_url' => 'https://example.com/image.jpg',
42+
'alt_text' => 'an image',
43+
],
44+
], $section->toArray());
45+
}
46+
47+
public function testThrowsWhenFieldsLimitReached(): void
48+
{
49+
$section = new SlackSectionBlock();
50+
for ($i = 0; $i < 10; ++$i) {
51+
$section->field($i);
52+
}
53+
54+
$this->expectException(\LogicException::class);
55+
$this->expectExceptionMessage('Maximum number of fields should not exceed 10.');
56+
57+
$section->field('fail');
58+
}
59+
}

0 commit comments

Comments
 (0)