<?php

namespace airmoi\FileMaker;

use airmoi\FileMaker\Object\Record;
use PHPUnit\Framework\TestCase;

/**
 * Generated by PHPUnit_SkeletonGenerator on 2016-09-09 at 19:41:41.
 * @SuppressWarnings(PHPMD.Superglobals)
 * @SuppressWarnings(PHPMD.ShortVariable)
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 *
 */
class DateFormatTest extends TestCase
{
    /**
     * @var FileMaker
     */
    protected $fm;

    /**
     * @var Record
     */
    protected $record;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        $this->fm = new FileMaker(
            $GLOBALS['DB_FILE'],
            $GLOBALS['DB_HOST'],
            $GLOBALS['DB_USER'],
            $GLOBALS['DB_PASSWD'],
            ['errorHandling' => 'exception']
        );
        $this->fm->newPerformScriptCommand('sample', 'create sample data', 50)->execute();


        $layout = $this->fm->getLayout('sample');
        $this->record = new Record($layout);
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown()
    {
    }

    public function testDefaultDateFormatInput()
    {
        //default date format handling
        $this->record->fm->dateFormat = null;

        $this->record->setField('date_field', date('m/d/Y'));
        $this->assertTrue($this->record->validate('date_field'));

        //Test invalid date input
        $this->expectException(FileMakerValidationException::class);
        $this->record->setField('date_field', date('Y-m-d'));
        $this->record->validate('date_field');
    }

    public function testIsoDateFormatInput()
    {
        //Custom date format (ISO)
        $this->record->fm->dateFormat = 'Y-m-d';

        $this->record->setField('date_field', date('Y-m-d'));
        $this->assertTrue($this->record->validate('date_field'));
        $this->assertEquals(date('Y-m-d'), $this->record->getField('date_field'));

        //test invalid input
        $this->expectException(FileMakerException::class);
        $this->record->setField('date_field', date('m/d/Y'));
    }


    public function testCommonDateFormatInput()
    {
        //Custom date format (International format)
        $this->record->fm->dateFormat = 'd/m/Y';

        $this->record->setField('date_field', date('d/m/Y'));
        $this->assertTrue($this->record->validate('date_field'));
        $this->assertEquals(date('d/m/Y'), $this->record->getField('date_field'));
    }

    public function testCommonToDefaultDateInversion()
    {
        //Custom date format (International format)
        $this->record->fm->dateFormat = 'd/m/Y';
        
        //inject invalid m/d/Y date
        $this->expectException(FileMakerException::class);
        $this->record->setField('date_field', date('06/18/2016'));
    }

    public function test1digitDate()
    {
        //Custom date format (International format)
        $this->record->fm->dateFormat = 'd/m/Y';
        $this->record->setField('date_field', date('12/2/2016'));
        $this->assertTrue($this->record->validate('date_field'));
    }

    public function testDateFormatRequetsActivation()
    {
        $request = $this->fm->newFindRequest('sample');
        $request->addFindCriterion('date_field', '2016-02-12');
        $this->assertEquals('2016-02-12', $request->findCriteria['date_field']);

        $this->fm->useDateFormatInRequests = true;
        $this->fm->dateFormat = 'Y-m-d';
        $request->addFindCriterion('date_field', '2016-02-12');
        $this->assertEquals('02/12/2016', $request->findCriteria['date_field']);
    }
}
