<?php

namespace App\Tests\Unit\Service\ServiceIterator;

use App\Service\Cron\CronjobInterface;
use App\Service\ServiceIterator\CronjobIterator;
use PHPUnit\Framework\TestCase;

class CronjobIteratorTest extends TestCase
{
    public function testGetByNameExisting(): void
    {

        $cronjob1 = $this->getMockBuilder(CronjobInterface::class)->getMock();
        $cronjob1->expects(self::once())->method('name')->willReturn('foo');

        $cronjob2 = $this->getMockBuilder(CronjobInterface::class)->getMock();
        $cronjob2->expects(self::once())->method('name')->willReturn('bar');

        $cronjob3 = $this->getMockBuilder(CronjobInterface::class)->getMock();
        $cronjob3->expects(self::never())->method('name')->willReturn('zou');

        $cronjobIterator = $this->getMockBuilder(CronjobIterator::class)
            ->setConstructorArgs([[$cronjob1, $cronjob2, $cronjob3]])
            ->setMethodsExcept(['getByName'])
            ->getMock();

        $this->assertEquals(
            $cronjob2,
            $cronjobIterator->getByName('bar')
        );
    }

    public function testGetByNameNotExisting(): void
    {

        $cronjob1 = $this->getMockBuilder(CronjobInterface::class)->getMock();
        $cronjob1->expects(self::once())->method('name')->willReturn('foo');

        $cronjob2 = $this->getMockBuilder(CronjobInterface::class)->getMock();
        $cronjob2->expects(self::once())->method('name')->willReturn('bar');

        $cronjobIterator = $this->getMockBuilder(CronjobIterator::class)
            ->setConstructorArgs([[$cronjob1, $cronjob2]])
            ->setMethodsExcept(['getByName'])
            ->getMock();

        $this->expectException(\RuntimeException::class);

        $cronjobIterator->getByName('other');
    }

    public function testGetAll(): void
    {
        $cronjob1 = $this->getMockBuilder(CronjobInterface::class)->getMock();
        $cronjob2 = $this->getMockBuilder(CronjobInterface::class)->getMock();

        $cronjobIterator = $this->getMockBuilder(CronjobIterator::class)
            ->setConstructorArgs([[$cronjob1, $cronjob2]])
            ->setMethodsExcept(['getAll'])
            ->getMock();

        $this->assertEquals(
            [$cronjob1, $cronjob2],
            $cronjobIterator->getAll()
        );
    }
}
