<?php

namespace App\Tests\Unit\Service\ServiceIterator\Mailer;

use App\Service\Mailer\Builder\BuilderInterface;
use App\Service\Mailer\Model\MailerModelInterface;
use App\Service\ServiceIterator\Mailer\BuilderIterator;
use PHPUnit\Framework\TestCase;

class BuilderIteratorTest extends TestCase
{
    public function testGetBuilderFor(): void {
        $mailModel = $this->getMockBuilder(MailerModelInterface::class)->getMock();

        $builder1 = $this->getMockBuilder(BuilderInterface::class)->getMock();
        $builder1->expects(self::once())->method('support')->with($mailModel)->willReturn(false);

        $builder2 = $this->getMockBuilder(BuilderInterface::class)->getMock();
        $builder2->expects(self::once())->method('support')->with($mailModel)->willReturn(true);

        $builder3 = $this->getMockBuilder(BuilderInterface::class)->getMock();
        $builder3->expects(self::never())->method('support');

        $builderIterator = $this->getMockBuilder(BuilderIterator::class)
            ->setConstructorArgs([[$builder1, $builder2, $builder3]])
            ->setMethodsExcept(['getBuilderFor'])
            ->getMock();

        $this->assertEquals(
            $builder2,
            $builderIterator->getBuilderFor($mailModel)
        );
    }

    public function testGetBuilderForNotFound(): void {
        $mailModel = $this->getMockBuilder(MailerModelInterface::class)->getMock();

        $builder1 = $this->getMockBuilder(BuilderInterface::class)->getMock();
        $builder1->expects(self::once())->method('support')->with($mailModel)->willReturn(false);

        $builder2 = $this->getMockBuilder(BuilderInterface::class)->getMock();
        $builder2->expects(self::once())->method('support')->with($mailModel)->willReturn(false);

        $builderIterator = $this->getMockBuilder(BuilderIterator::class)
            ->setConstructorArgs([[$builder1, $builder2]])
            ->setMethodsExcept(['getBuilderFor'])
            ->getMock();

        $this->expectException(\Exception::class);
        $builderIterator->getBuilderFor($mailModel);
    }
}
