<?php

namespace App\Tests\Unit\Service\ServiceIterator;

use App\Service\Export\Encoder\EncoderInterface;
use App\Service\ServiceIterator\EncoderIterator;
use Exception;
use PHPUnit\Framework\TestCase;

class EncoderIteratorTest extends TestCase
{
    /**
     * @see EncoderIterator::getEncoderFor()
     */
    public function testGetEncoderFor(): void
    {
        $mocker = $this->getMockBuilder(EncoderInterface::class);

        $encoder1 = $mocker->getMock();
        $encoder1->method('support')->willReturn(false);

        $encoder2 = $mocker->getMock();
        $encoder2->expects($this->once())->method('support')->with('pdf')->willReturn(true);

        $encoder3 = $mocker->getMock();
        $encoder3->method('support')->willReturn(false);

        $encoders = [$encoder1, $encoder2, $encoder3];

        $iterator = $this->getMockBuilder(EncoderIterator::class)
            ->setConstructorArgs([$encoders])
            ->setMethodsExcept(['getEncoderFor'])
            ->getMock();

        $actualEncoder = $iterator->getEncoderFor('pdf');

        $this->assertEquals($encoder2, $actualEncoder);
    }

    /**
     * @see EncoderIterator::getEncoderFor()
     */
    public function testGetEncoderForError(): void
    {
        $mocker = $this->getMockBuilder(EncoderInterface::class);

        $encoder1 = $mocker->getMock();
        $encoder1->method('support')->willReturn(false);

        $encoder2 = $mocker->getMock();
        $encoder2->method('support')->willReturn(false);

        $encoders = [$encoder1, $encoder2];

        $iterator = $this->getMockBuilder(EncoderIterator::class)
            ->setConstructorArgs([$encoders])
            ->setMethodsExcept(['getEncoderFor'])
            ->getMock();

        $this->expectException(Exception::class);
        $iterator->getEncoderFor('gif');
    }
}
