<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CmsBundle\Tests\Page;

use Contao\CoreBundle\EventListener\SubrequestCacheSubscriber;
use Ferienpass\CmsBundle\Page\PageBuilder;
use PHPUnit\Framework\TestCase;

final class PageBuilderCacheMergeTest extends TestCase
{
    public function testGetResponseStampsTheMergeCacheControlSentinel(): void
    {
        $source = file_get_contents(new \ReflectionClass(PageBuilder::class)->getFileName());
        self::assertIsString($source, 'Must be able to read PageBuilder source for textual regression check.');

        self::assertMatchesRegularExpression(
            '/->headers->set\s*\(\s*SubrequestCacheSubscriber::MERGE_CACHE_HEADER\s*,/',
            $source,
            \sprintf(
                'PageBuilder::getResponse() must stamp Contao\'s merge-cache-control sentinel on the page response '
                .'(mirror of PageRegular.php:52). Without it, SubrequestCacheSubscriber will silently skip merging '
                .'sub-response cache-control directives — so AbstractPrivateFragmentController-based fragments '
                .'(application form, user nav, …) can no longer flip the page to `private` when they need to. '
                .'Expected header constant: %s.',
                SubrequestCacheSubscriber::MERGE_CACHE_HEADER,
            ),
        );

        self::assertStringContainsString(
            'use Contao\\CoreBundle\\EventListener\\SubrequestCacheSubscriber;',
            $source,
            'PageBuilder must import SubrequestCacheSubscriber to reference the MERGE_CACHE_HEADER constant.',
        );
    }
}
