<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Helper;

use PhpOffice\PhpSpreadsheet\Helper\Sample;
use PHPUnit\Framework\TestCase;
use RuntimeException;

#[\PHPUnit\Framework\Attributes\CoversClass(Sample::class)]
class SampleCoverageTest extends TestCase
{
    public function testSample(): void
    {
        $helper = new Sample();
        $samples = $helper->getSamples();
        self::assertArrayHasKey('Basic', $samples);
        $basic = $samples['Basic'];
        self::assertArrayHasKey('02 Types', $basic);
        self::assertSame('Basic/02_Types.php', $basic['02 Types']);
        self::assertSame('phpunit', $helper->getPageTitle());
        self::assertSame('<h1>phpunit</h1>', $helper->getPageHeading());
    }

    public function testDirectoryFail(): void
    {
        $this->expectException(RuntimeException::class);

        $helper = $this->getMockBuilder(Sample::class)
            ->onlyMethods(['isDirOrMkdir'])
            ->getMock();
        $helper->expects(self::once())
            ->method('isDirOrMkdir')
            ->willReturn(false);
        $helper->getFilename('a.xlsx');
    }

    public function testTitles(): void
    {
        $helper = new Sample();
        ob_start();
        $helper->titles('Category', 'FunctionName');
        $output = (string) ob_get_clean();
        $output = str_replace("\r", '', $output);
        self::assertStringContainsString("Function: FunctionName()\n", $output);
        ob_start();
        $helper->titles('Category', 'FunctionName', 'Description');
        $output = (string) ob_get_clean();
        $output = str_replace("\r", '', $output);
        self::assertStringContainsString("Function: FunctionName() - Description.\n", $output);
    }
}
