<?php

namespace App\Tests\Unit\Service\Export\Encoder;

use App\Service\Export\Encoder\PdfEncoder;
use Knp\Snappy\Pdf;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class PdfEncoderTest extends TestCase
{
    private MockObject | Pdf $knpSnappy;

    public function setUp(): void
    {
        $this->knpSnappy = $this->getMockBuilder(Pdf::class)->getMock();
    }

    /**
     * @see PdfEncoder::support()
     */
    public function testSupport(): void
    {
        $encoder = $this->getMockBuilder(PdfEncoder::class)
            ->setConstructorArgs([$this->knpSnappy])
            ->setMethodsExcept(['support'])
            ->getMock();

        $this->assertTrue($encoder->support('pdf'));
        $this->assertFalse($encoder->support('txt'));
    }

    /**
     * @see PdfEncoder::getDefaultOptions()
     */
    public function testGetDefaultOptions(): void
    {
        $encoder = $this->getMockBuilder(PdfEncoder::class)
            ->setConstructorArgs([$this->knpSnappy])
            ->setMethodsExcept(['getDefaultOptions'])
            ->getMock();

        $this->assertIsArray($encoder->getDefaultOptions());
    }

    /**
     * @see PdfEncoder::encode()
     */
    public function testEncode(): void
    {
        $encoder = $this->getMockBuilder(PdfEncoder::class)
            ->setConstructorArgs([$this->knpSnappy])
            ->setMethodsExcept(['encode'])
            ->getMock();

        $encoder->method('getDefaultOptions')->willReturn(['defaultOption' => 1]);

        $this->knpSnappy
            ->expects(self::once())
            ->method('getOutputFromHtml')
            ->with('<div>content</div>', ['defaultOption' => 1, 'additionalOption' => 2])
            ->willReturn('%%encoded%%');

        $this->assertEquals(
            '%%encoded%%',
            $encoder->encode('<div>content</div>', ['additionalOption' => 2])
        );
    }
}
