<?php

namespace airmoi\FileMaker;

use airmoi\FileMaker\FileMaker;

/**
 * Generated by PHPUnit_SkeletonGenerator on 2016-09-09 at 20:40:06.
 */
class FileMakerValidationExceptionTest extends \PHPUnit_Framework_TestCase
{

    /**
     * @var FileMakerValidationException
     */
    protected $object;


    /**
     * 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']);
        $this->locales = require(dirname(__FILE__) . '/../../../src/Error/en.php');
    }

    /**
     * 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\FileMakerValidationException::addError
     */
    public function testAddError()
    {
        $exception = new FileMakerValidationException($this->fm);
        $field = new Object\Field(new Object\Layout($this->fm));
        $field->name = "sample_field";

        $exception->addError($field, FileMaker::RULE_DATE_FIELD, "Incorrect date value");
        $this->assertEquals(1, $exception->numErrors());

        $field2 = new Object\Field(new Object\Layout($this->fm));
        $field2->name = "sample_field2";

        $exception->addError($field2, FileMaker::RULE_NOTEMPTY, null);
        $this->assertEquals(2, $exception->numErrors());

        return $exception;
    }

    /**
     * @covers \airmoi\FileMaker\FileMakerValidationException::isValidationError
     */
    public function testIsValidationError()
    {
        $exception = new FileMakerValidationException($this->fm);
        $this->assertTrue($exception->isValidationError());
    }

    /**
     * @covers \airmoi\FileMaker\FileMakerValidationException::getErrors
     * @depends testAddError
     */
    public function testGetErrors(FileMakerValidationException $exception)
    {
        $this->assertEquals(2, sizeof($exception->getErrors()));

        $this->assertEquals(FileMaker::RULE_DATE_FIELD, $exception->getErrors('sample_field')[0][1]);
    }

    /**
     * @covers \airmoi\FileMaker\FileMakerValidationException::getErrors
     * @depends testAddError
     */
    public function testGetMessage(FileMakerValidationException $exception)
    {
        $this->assertEquals(2, sizeof(explode("\n", $exception->getMessage())));
    }
}
