<?php
namespace airmoi\FileMaker\Command;

use airmoi\FileMaker\FileMaker;

/**
 * Generated by PHPUnit_SkeletonGenerator on 2016-09-09 at 20:40:05.
 */
class AddTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var FileMaker
     */
    protected $fm;

    public static function setUpBeforeClass()
    {
        parent::setUpBeforeClass();
        $fm = new FileMaker($GLOBALS['DB_FILE'], $GLOBALS['DB_HOST'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']);
        $fm->newPerformScriptCommand('sample', 'create sample data', 10)->execute();
    }

    /**
     * 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']);
    }

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

    /**
     * @covers \airmoi\FileMaker\Command\Add::execute
     * @todo   Implement testExecute().
     */
    public function testExecute()
    {
        $addCmd = $this->fm->newAddCommand('sample', [
            'text_field' => [
                'sample 1',
                'sample 2',
                'sample 3',
                'sample 4',
            ]
        ]);

        $result = $addCmd->execute();

        $this->assertEquals(11, $result->getTableRecordCount());
    }

    /**
     * @covers \airmoi\FileMaker\Command\Add::setField
     */
    public function testSetField()
    {
        $addCmd = $this->fm->newAddCommand('sample');
        $this->assertEquals('Sample text', $addCmd->setField('text_field', 'Sample text'));
        $this->assertEquals('Sample text', $addCmd->setField('related_sample::text_field', 'Sample text'));

        $date = date('m/d/Y');
        $this->assertEquals($date, $addCmd->setField('date_field', $date));

        $time = date('H:i:s');
        $this->assertEquals($time, $addCmd->setField('time_field', $time));

        $timeStamp = date('m/d/Y H:i:s');
        $this->assertEquals($timeStamp, $addCmd->setField('timestamp_field', $timeStamp));
    }

    /**
     * @covers \airmoi\FileMaker\Command\Add::setFieldFromTimestamp
     */
    public function testSetFieldFromTimestamp()
    {

        $addCmd = $this->fm->newAddCommand('sample');

        $time = time();
        $this->assertEquals(date('m/d/Y', $time), $addCmd->setFieldFromTimestamp('date_field', $time));
        $this->assertEquals(date('m/d/Y H:i:s', $time), $addCmd->setFieldFromTimestamp('timestamp_field', $time));
        $this->assertEquals(date('H:i:s', $time), $addCmd->setFieldFromTimestamp('time_field', $time));
    }

    public function testUseRawData()
    {
        $this->fm->dateFormat = 'Y-m-d';

        $time = time();
        $date = date('m/d/Y', $time);
        $addCmd = $this->fm->newAddCommand('sample', [
            'text_field' => [
                'sample 1',
                'sample 2',
                'sample 3',
                'sample 4',
            ],
            'date_field' => $date
        ], true);

        $record = $addCmd->execute()->getFirstRecord();
        $this->assertEquals(date('Y-m-d', $time), $record->getField('date_field'));
    }
}
