<?php

namespace Tests\unit\workflow\engine\src\ProcessMaker\Model;

use ProcessMaker\Model\EmailEvent;
use ProcessMaker\Model\EmailServerModel;
use Tests\TestCase;

/**
 * Class EmailEventTest
 *
 * @coversDefaultClass \ProcessMaker\Model\EmailEvent
 */
class EmailEventTest extends TestCase
{
    /**
     * Call the setUp parent method
     */
    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
    }

    /**
     * Call the tearDown parent method
     */
    public function tearDown(): void
    {
        parent::tearDown(); // TODO: Change the autogenerated stub
    }

    /**
     * It tests the updateServerAndFromToDefaultOrEmpty method
     * 
     * @covers \ProcessMaker\Model\EmailEvent::updateServerAndFromToDefaultOrEmpty()
     * @test
     */
    public function it_should_test_the_update_event_method()
    {
        EmailServerModel::query()->delete();
        $emailServer = EmailServerModel::factory()->create();
        $emailEventFactory = EmailEvent::factory()->create([
            'EMAIL_SERVER_UID' => $emailServer['MESS_UID']
        ]);

        $emailEvent = new EmailEvent();
        $emailEvent->updateServerAndFromToDefaultOrEmpty($emailServer['MESS_UID']);

        $query = EmailEvent::query()->select();
        $query->where('EMAIL_EVENT_UID', $emailEventFactory['EMAIL_EVENT_UID']);
        $updatedEmailEvent = $query->get()->values()->toArray();

        $this->assertEquals($updatedEmailEvent[0]['EMAIL_SERVER_UID'], '');
    }

    /**
     * It tests the updateServerAndFromToDefaultOrEmpty method with a default email server
     * 
     * @covers \ProcessMaker\Model\EmailEvent::updateServerAndFromToDefaultOrEmpty()
     * @test
     */
    public function it_should_test_the_update_event_method_with_a_default_email_server()
    {
        EmailServerModel::query()->delete();
        $emailServer = EmailServerModel::factory()->create();
        $emailServerDefault = EmailServerModel::factory()->create([
            'MESS_DEFAULT' => 1
        ]);
        $emailEventFactory = EmailEvent::factory()->create([
            'EMAIL_SERVER_UID' => $emailServer['MESS_UID']
        ]);

        $emailEvent = new EmailEvent();
        $emailEvent->updateServerAndFromToDefaultOrEmpty($emailServer['MESS_UID']);

        $query = EmailEvent::query()->select();
        $query->where('EMAIL_EVENT_UID', $emailEventFactory['EMAIL_EVENT_UID']);
        $updatedEmailEvent = $query->get()->values()->toArray();

        $this->assertEquals($updatedEmailEvent[0]['EMAIL_SERVER_UID'], $emailServerDefault['MESS_UID']);
    }
}