<?php
namespace App\Tests\Unit\Service\Cron;

use App\Service\Cron\BaseCronJob;
use App\Service\Cron\UI\ConsoleUI;
use App\Service\Cron\UI\CronUIInterface;
use App\Service\Cron\UI\SilentUI;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

class TestableBaseCronJob extends BaseCronJob {
    public function getUI(): CronUIInterface { return $this->ui; }
    public function getLogger(): LoggerInterface { return $this->logger; }
}

class BaseCronJobTest extends TestCase
{
    public function testName(): void
    {
        $job = new TestableBaseCronJob(); // can't use a mock here, because it'll mess up with the class's name

        $this->assertEquals(
            'testable-base-cron-job',
            $job->name()
        );
    }

    public function testSetLogger(): void {
        $job = $this->getMockBuilder(TestableBaseCronJob::class)
            ->setMethodsExcept(['setLoggerInterface', 'getLogger'])
            ->getMock();

        $logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();

        $job->setLoggerInterface($logger);

        $this->assertEquals(
            $logger,
            $job->getLogger()
        );
    }

    public function testDefaultUi(): void {
        $job = new TestableBaseCronJob(); // can't use a mock here, because we need the constructor to run

        $this->assertInstanceOf(
            SilentUI::class,
            $job->getUI()
        );
    }

    public function testSetUi(): void {
        $job = $this->getMockBuilder(TestableBaseCronJob::class)
            ->setMethodsExcept(['setUI', 'getUI'])
            ->getMock();

        $ui = $this->getMockBuilder(ConsoleUI::class)
            ->disableOriginalConstructor()
            ->getMock();

        $job->setUI($ui);

        $this->assertEquals(
            $ui,
            $job->getUI()
        );
    }
}
