<?php

namespace App\Tests\Unit\Service\ServiceIterator;

use App\ApiResources\Export\ExportRequest;
use App\Service\Export\ExporterInterface;
use App\Service\ServiceIterator\ExporterIterator;
use Exception;
use PHPUnit\Framework\TestCase;

class ExporterIteratorTest extends TestCase
{
    /**
     * @see ExporterIterator::getExporterFor()
     */
    public function testGetExporterFor(): void
    {
        $exportRequest = $this->getMockBuilder(ExportRequest::class)
            ->disableOriginalConstructor()
            ->getMock();

        $mocker = $this->getMockBuilder(ExporterInterface::class);

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

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

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

        $exporters = [$exporter1, $exporter2, $exporter3];

        $iterator = $this->getMockBuilder(ExporterIterator::class)
            ->setConstructorArgs([$exporters])
            ->setMethodsExcept(['getExporterFor'])
            ->getMock();

        $actualExporter = $iterator->getExporterFor($exportRequest);

        $this->assertEquals($exporter2, $actualExporter);
    }

    /**
     * @see ExporterIterator::getExporterFor()
     */
    public function testGetExporterForError(): void
    {
        $exportRequest = $this->getMockBuilder(ExportRequest::class)
            ->disableOriginalConstructor()
            ->getMock();

        $mocker = $this->getMockBuilder(ExporterInterface::class);

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

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

        $exporters = [$exporter1, $exporter2];

        $iterator = $this->getMockBuilder(ExporterIterator::class)
            ->setConstructorArgs([$exporters])
            ->setMethodsExcept(['getExporterFor'])
            ->getMock();

        $this->expectException(Exception::class);
        $iterator->getExporterFor($exportRequest);
    }
}
