<?php

declare(strict_types=1);

namespace Autogedal\FeedMonitor\Test\Unit\Plugin;

use Autogedal\FeedMonitor\Model\Adapter\FirebearJobMonitor;
use Autogedal\FeedMonitor\Plugin\Firebear\RecordImportResult;
use Firebear\ImportExport\Api\Data\ImportInterface;
use Firebear\ImportExport\Model\Job\Handler\SummaryHandler;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

class RecordImportResultTest extends TestCase
{
    public function testTheRunThatEndedIsRecordedWithItsFileAndStatus(): void
    {
        $monitor = $this->createMock(FirebearJobMonitor::class);
        $monitor->expects(self::once())->method('recordImport')->with(44, '44-1793781768', false);
        $job = $this->createMock(ImportInterface::class);
        $job->method('getId')->willReturn(44);

        $result = (new RecordImportResult($monitor, $this->createMock(LoggerInterface::class)))
            ->afterExecute($this->createMock(SummaryHandler::class), null, $job, '44-1793781768', 0);

        self::assertNull($result);
    }

    public function testAMonitorErrorDoesNotStopTheImport(): void
    {
        $monitor = $this->createMock(FirebearJobMonitor::class);
        $monitor->method('recordImport')->willThrowException(new \RuntimeException('db down'));
        $logger = $this->createMock(LoggerInterface::class);
        $logger->expects(self::once())->method('error')->with(self::stringContains('db down'));
        $job = $this->createMock(ImportInterface::class);
        $job->method('getId')->willReturn(44);

        (new RecordImportResult($monitor, $logger))->afterExecute($this->createMock(SummaryHandler::class), null, $job, '44-1', 1);
    }
}
