<?php

declare(strict_types=1);

namespace KupShop\AutomationConfiguratorBundle\Tests;

use KupShop\DevelopmentBundle\MailArchive;
use KupShop\SalesBundle\Event\SaleCreatedEvent;
use KupShop\SalesBundle\Util\SalesUtil;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class SaleAutomationConfigurationsTest extends \DatabaseTestCase
{
    private SalesUtil $salesUtil;
    private MailArchive $mailArchive;
    private EventDispatcherInterface $eventDispatcher;

    protected function setUp(): void
    {
        parent::setUp();

        $this->salesUtil = $this->get(SalesUtil::class);
        $this->mailArchive = $this->get(MailArchive::class);
        $this->eventDispatcher = $this->get(EventDispatcherInterface::class);
    }

    public function testSystemMailIsSentOnSaleCreate(): void
    {
        $sale = $this->salesUtil->getSale(1);

        $this->eventDispatcher->dispatch(new SaleCreatedEvent($sale));
        // dvojity dispatch, abych overil, ze se autopilot spusti jen 1x
        $this->eventDispatcher->dispatch(new SaleCreatedEvent($sale));

        $archive = $this->mailArchive->getArchive();

        $this->assertCount(1, $archive);
        $this->assertEquals('Děkujeme za Váš nákup 123456', $archive[0]['subject']);
    }

    public function getDataSet()
    {
        return $this->getJsonDataSetFromFile();
    }
}
