<?php

namespace KupShop\ContentBundle\Tests;

use KupShop\ContentBundle\Util\Block;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

class ComponentPlaceholderTest extends \DatabaseTestCase
{
    public static function setUpBeforeClass(): void
    {
        if (!findModule(\Modules::COMPONENTS)) {
            self::markTestSkipped('Modul COMPONENTS je vypnutý.');
        }
    }

    public function testReplaceComponentPlaceholder()
    {
        $block = ServiceContainer::getService(Block::class);

        $blocks[] = [
            'content' => $this->getAlertPlaceholder('ALERT_TOP_LEVEL', 'success'),
            'children' => [
                ['content' => $this->getAlertPlaceholder('ALERT_CHILD', 'error')],
            ],
        ];

        $block->replaceComponentPlaceholders($blocks);

        // Check TOP level block
        $this->assertRegExp('/<div data-test=\'TEST-DIV\'><div\s+class="c-alert-success c-alert"\s+data-component="alert"\s+data-component-info="[^"]*">\s*ALERT_TOP_LEVEL\s*<\/div>\s*<\/div>/i', $blocks[0]['content']);

        // Check CHILD level block
        $this->assertRegExp('/<div data-test=\'TEST-DIV\'><div\s+class="c-alert-error c-alert"\s+data-component="alert"\s+data-component-info="[^"]*">\s*ALERT_CHILD\s*<\/div>\s*<\/div>/i', $blocks[0]['children'][0]['content']);
    }

    private function getAlertPlaceholder(string $text, string $type): string
    {
        return "<div data-test='TEST-DIV'><div data-block-component='{\"name\":\"Alert\", \"params\":{\"message\":\"{$text}\", \"type\": \"{$type}\"}}'></div></div>";
    }
}
