<?php

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

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

/**
 * Class EmailServerModelTest
 *
 * @coversDefaultClass \ProcessMaker\Model\EmailServerModel
 */
class EmailServerModelTest 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
    }

    /**
     * Test the getEmailServer method
     *
     * @covers \ProcessMaker\Model\EmailServerModel::getEmailServer()
     * @test
     */
    public function it_should_test_the_get_email_server_method()
    {
        //Create the EmailServerModel factory
        $emailServer = EmailServerModel::factory()->create();

        //Create the EmailServerModel object
        $email = new EmailServerModel();
        //Call the getEmailServer method
        $res = $email->getEmailServer($emailServer->MESS_UID);

        //Assert the result es not empty
        $this->assertNotEmpty($res);
        //Assert the result is the one looked for
        $this->assertEquals($res['MESS_UID'], $emailServer->MESS_UID);
    }

    /**
     * Test the getEmailServer method with an empty result
     *
     * @covers \ProcessMaker\Model\EmailServerModel::getEmailServer()
     * @test
     */
    public function it_should_test_the_get_email_server_method_with_an_empty_result()
    {
        //Create the EmailServerModel factory
        $emailServer = EmailServerModel::factory()->create();

        //Create the EmailServerModel object
        $email = new EmailServerModel();
        //Call the getEmailServer method
        $res = $email->getEmailServer('');

        //Assert the result is empty
        $this->assertEmpty($res);
    }

    /**
     * Test the getEmailServerDefault method
     *
     * @covers \ProcessMaker\Model\EmailServerModel::getEmailServerDefault()
     * @test
     */
    public function it_should_test_the_get_email_server_default()
    {
        //Create the EmailServer factory
        $emailServer = EmailServerModel::factory()->create([
            'MESS_DEFAULT' => 1
        ]);

        //Create the EmailServerModel object
        $email = new EmailServerModel();
        //Call the getEmailServerDefault method
        $res = $email->getEmailServerDefault();

        //Assert the result es not empty
        $this->assertNotEmpty($res);
    }

    /**
     * It tests the isImap method when there is an IMAP server
     * 
     * @covers \ProcessMaker\Model\EmailServerModel::isImap()
     * @test
     */
    public function it_should_test_the_is_imap_method_when_there_is_an_imap_server()
    {
        EmailServerModel::query()->delete();
        $emailServer = EmailServerModel::factory()->create([
            'MESS_ENGINE' => 'IMAP'
        ]);
        $emailServerModel = new EmailServerModel();
        $r = $emailServerModel->isImap($emailServer['MESS_UID']);
        
        $this->assertTrue($r);
    }

    /**
     * It tests the isImap method when there is not an IMAP server
     * 
     * @covers \ProcessMaker\Model\EmailServerModel::isImap()
     * @test
     */
    public function it_should_test_the_is_imap_method_when_there_is_not_an_imap_server()
    {
        EmailServerModel::query()->delete();
        $emailServer = EmailServerModel::factory()->create();
        $emailServerModel = new EmailServerModel();
        $r = $emailServerModel->isImap($emailServer['MESS_UID']);
        
        $this->assertFalse($r);
    }
}
