SlackSectionBlock example


        $this->expectException(LogicException::class);
        $this->expectExceptionMessage('Maximum number of "blocks" has been reached (50).');

        new SlackOptions(['blocks' => array_map(static fn () => ['type' => 'divider']range(0, 50))]);
    }

    public function testAddMaximumBlocks()
    {
        $options = new SlackOptions();
        for ($i = 0; $i < 50; ++$i) {
            $options->block(new SlackSectionBlock());
        }

        $this->assertCount(50, $options->toArray()['blocks']);
    }

    public function testThrowsWhenBlocksLimitReached()
    {
        $options = new SlackOptions();
        for ($i = 0; $i < 50; ++$i) {
            $options->block(new SlackSectionBlock());
        }

        
$this->options = $options;

        if (\count($this->options['blocks'] ?? []) > self::MAX_BLOCKS) {
            throw new LogicException(sprintf('Maximum number of "blocks" has been reached (%d).', self::MAX_BLOCKS));
        }
    }

    public static function fromNotification(Notification $notification): self
    {
        $options = new self();
        $options->iconEmoji($notification->getEmoji());
        $options->block((new SlackSectionBlock())->text($notification->getSubject()));
        if ($notification->getContent()) {
            $options->block((new SlackSectionBlock())->text($notification->getContent()));
        }
        if ($exception = $notification->getExceptionAsString()) {
            $options->block(new SlackDividerBlock());
            $options->block((new SlackSectionBlock())->text($exception));
        }

        return $options;
    }

    
namespace Symfony\Component\Notifier\Bridge\Slack\Tests\Block;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackImageBlockElement;
use Symfony\Component\Notifier\Bridge\Slack\Block\SlackSectionBlock;

final class SlackSectionBlockTest extends TestCase
{
    public function testCanBeInstantiated()
    {
        $section = new SlackSectionBlock();
        $section->text('section text');
        $section->field('section field');
        $section->accessory(new SlackImageBlockElement('https://example.com/image.jpg', 'an image'));

        $this->assertSame([
            'type' => 'section',
            'text' => [
                'type' => 'mrkdwn',
                'text' => 'section text',
            ],
            'fields' => [
                [
Home | Imprint | This part of the site doesn't use cookies.