<?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\CoreBundle\Tests\Twig;

use Ferienpass\CoreBundle\Twig\NullSafeMarkdownRuntime;
use PHPUnit\Framework\TestCase;
use Twig\Extra\Markdown\MarkdownInterface;

final class NullSafeMarkdownRuntimeTest extends TestCase
{
    public function testConvertNullReturnsEmptyStringInsteadOfThrowing(): void
    {
        // The stock runtime would TypeError here (Sentry 7251148302).
        self::assertSame('', $this->runtime()->convert(null));
    }

    public function testConvertStringStillPassesThrough(): void
    {
        self::assertSame('hello', $this->runtime()->convert('hello'));
    }

    private function runtime(): NullSafeMarkdownRuntime
    {
        // Echoing converter so we can observe exactly what reaches it.
        $converter = new class implements MarkdownInterface {
            public function convert(string $body): string
            {
                return $body;
            }
        };

        return NullSafeMarkdownRuntime::fromConverter($converter);
    }
}
