<?php

namespace PeytzMail\Tests\Entity;

use PeytzMail\Tests\TestCase;
use PeytzMail\Entity\FieldEvents;

class FieldEventsTest extends TestCase
{
    public function testCheckEntitysPropertiesCreatedWithAJsonString()
    {
        $json = <<<JSON
{
    "current_page": 1,
    "total_records": 4,
    "total_pages": 1,
    "next_page": null,
    "events": [
        {
            "event_id": "533e82a75d832a1dbe000002",
            "created": "2014-04-04T12:00:07+02:00",
            "change": "update",
            "source": "api",
            "subscriber_token": "kkjtnl",
            "extra_fields": {
                "email": "jens_lind@dadlnet.dk",
                "foreign_id": "b47a5a7f58e24f39bca74719dec16548",
                "first_name": "Jens",
                "last_name": "Lind Hansen",
                "region": "WEST",
                "zip": "5772"
            },
            "extra_fields_were": {
                "foreign_id": "",
                "first_name": null,
                "last_name": null,
                "zip": null,
                "region": null,
                "country": null,
                "has_brobizz": null,
                "brobizz_number": null
            }
        },
        {
            "event_id": "533e82a75d832a1dbe000003",
            "created": "2014-04-04T12:00:07+02:00",
            "change": "update",
            "source": "api",
            "subscriber_token": "yboqsa",
            "extra_fields": {
                "email": "pdp1712@yahoo.dk",
                "foreign_id": "6784e2ca805f46b185da9afb18c4fea9",
                "first_name": "Peter",
                "last_name": "Dai Petersen",
                "region": "EAST",
                "zip": "4780"
            },
            "extra_fields_were": {
                "foreign_id": "",
                "first_name": null,
                "last_name": null,
                "zip": null,
                "region": null,
                "country": null,
                "has_brobizz": null,
                "brobizz_number": null
            }
        },
        {
            "event_id": "533e94a71fc4650767000005",
            "created": "2014-04-04T13:16:55+02:00",
            "change": "update",
            "source": "backend",
            "subscriber_token": "capykq",
            "extra_fields": {
                "email": "foobar@anc.test.peytz.dk",
                "foreign_id": "a7dd516beb494d719092382804768017",
                "first_name": "foo",
                "last_name": "bar",
                "region": "EAST",
                "zip": "2000",
                "has_brobizz": "0"
            },
            "extra_fields_were": {
                "first_name": "fooooo",
                "last_name": "barrrrr",
                "has_brobizz": false
            }
        },
        {
            "event_id": "533e97731fc4657eaf000009",
            "created": "2014-04-04T13:28:51+02:00",
            "change": "create",
            "source": "api",
            "subscriber_token": "yrbfmv",
            "extra_fields": {
                "email": "bendt.andersen@gmail.com",
                "first_name": "Bendt",
                "last_name": "Vaarsø Andersen",
                "region": "WEST",
                "zip": "6092",
                "brobizz_number": "9208606200139330",
                "country": "dk",
                "has_brobizz": true
            },
            "extra_fields_were": {
                "first_name": null,
                "last_name": null,
                "email": null,
                "zip": null,
                "region": null,
                "has_brobizz": null,
                "brobizz_number": null,
                "country": null,
                "foreign_id": null
            }
        }
    ]
}
JSON;

        $fieldEvents = new FieldEvents;
        $fieldEvents->createFromJson($json);

        $this->assertSame(1, $fieldEvents->currentPage);
        $this->assertSame(4, $fieldEvents->totalRecords);
        $this->assertSame(1, $fieldEvents->totalPages);
        $this->assertNull($fieldEvents->nextPage);

        $this->assertTrue(is_array($fieldEvents->events));
        $this->assertCount($fieldEvents->totalRecords, $fieldEvents->events);

        $this->assertInstanceOf('\\PeytzMail\\Entity\\FieldEvent', $fieldEvent = $fieldEvents->events[0]);
        $this->assertSame('533e82a75d832a1dbe000002', $fieldEvent->eventId);
        $this->assertInstanceOf('\\DateTime', $fieldEvent->created);
        $this->assertSame('2014-04-04T12:00:07+02:00', $fieldEvent->created->format(DATE_RFC3339));
        $this->assertSame('update', $fieldEvent->change);
        $this->assertSame('api', $fieldEvent->source);
        $this->assertSame('kkjtnl', $fieldEvent->subscriberToken);
        $this->assertSame('jens_lind@dadlnet.dk', $fieldEvent->subscriber->email);
        $this->assertSame('b47a5a7f58e24f39bca74719dec16548', $fieldEvent->subscriber->foreignId);
        $this->assertSame('Jens', $fieldEvent->subscriber->firstName);
        $this->assertSame('Lind Hansen', $fieldEvent->subscriber->lastName);
        $this->assertSame('WEST', $fieldEvent->subscriber->region);
        $this->assertSame('5772', $fieldEvent->subscriber->zip);
        $this->assertFalse(isset($fieldEvent->subscriber->country));
        $this->assertFalse(isset($fieldEvent->subscriber->hasBrobizz));
        $this->assertFalse(isset($fieldEvent->subscriber->brobizzNumber));
        $this->assertEmpty($fieldEvent->subscriberWere->foreignId);
        $this->assertNull($fieldEvent->subscriberWere->firstName);
        $this->assertNull($fieldEvent->subscriberWere->lastName);
        $this->assertNull($fieldEvent->subscriberWere->region);
        $this->assertNull($fieldEvent->subscriberWere->zip);
        $this->assertNull($fieldEvent->subscriberWere->country);
        $this->assertNull($fieldEvent->subscriberWere->hasBrobizz);
        $this->assertNull($fieldEvent->subscriberWere->brobizzNumber);

        $this->assertInstanceOf('\\PeytzMail\\Entity\\FieldEvent', $fieldEvent = $fieldEvents->events[2]);
        $this->assertSame('533e94a71fc4650767000005', $fieldEvent->eventId);
        $this->assertInstanceOf('\\DateTime', $fieldEvent->created);
        $this->assertSame('2014-04-04T13:16:55+02:00', $fieldEvent->created->format(DATE_RFC3339));
        $this->assertSame('update', $fieldEvent->change);
        $this->assertSame('backend', $fieldEvent->source);
        $this->assertSame('capykq', $fieldEvent->subscriberToken);
        $this->assertSame('foobar@anc.test.peytz.dk', $fieldEvent->subscriber->email);
        $this->assertSame('a7dd516beb494d719092382804768017', $fieldEvent->subscriber->foreignId);
        $this->assertSame('foo', $fieldEvent->subscriber->firstName);
        $this->assertSame('bar', $fieldEvent->subscriber->lastName);
        $this->assertSame('EAST', $fieldEvent->subscriber->region);
        $this->assertSame('2000', $fieldEvent->subscriber->zip);
        $this->assertFalse(isset($fieldEvent->subscriber->country));
        $this->assertFalse((boolean) $fieldEvent->subscriber->hasBrobizz);
        $this->assertFalse(isset($fieldEvent->subscriber->brobizzNumber));
        $this->assertFalse(isset($fieldEvent->subscriberWere->foreignId));
        $this->assertSame('fooooo', $fieldEvent->subscriberWere->firstName);
        $this->assertsame('barrrrr', $fieldEvent->subscriberWere->lastName);
        $this->assertFalse(isset($fieldEvent->subscriberWere->region));
        $this->assertFalse(isset($fieldEvent->subscriberWere->zip));
        $this->assertFalse(isset($fieldEvent->subscriberWere->country));
        $this->assertFalse($fieldEvent->subscriberWere->hasBrobizz);
        $this->assertFalse(isset($fieldEvent->subscriberWere->brobizzNumber));

        $this->assertInstanceOf('\\PEytzMail\\Entity\\FieldEvent', $fieldEvent = $fieldEvents->events[3]);
        $this->assertSame('533e97731fc4657eaf000009', $fieldEvent->eventId);
        $this->assertInstanceOf('\DateTime', $fieldEvent->created);
        $this->assertSame('2014-04-04T13:28:51+02:00', $fieldEvent->created->format(DATE_RFC3339));
        $this->assertSame('create', $fieldEvent->change);
        $this->assertSame('api', $fieldEvent->source);
        $this->assertSame('yrbfmv', $fieldEvent->subscriberToken);
        $this->assertSame('bendt.andersen@gmail.com', $fieldEvent->subscriber->email);
        $this->assertSame('Bendt', $fieldEvent->subscriber->firstName);
        $this->assertSame('Vaarsø Andersen', $fieldEvent->subscriber->lastName);
        $this->assertSame('WEST', $fieldEvent->subscriber->region);
        $this->assertSame('6092', $fieldEvent->subscriber->zip);
        $this->assertSame('9208606200139330', $fieldEvent->subscriber->brobizzNumber);
        $this->assertSame('dk', $fieldEvent->subscriber->country);
        $this->assertTrue($fieldEvent->subscriber->hasBrobizz);
        $this->assertNull($fieldEvent->subscriberWere->firstName);
        $this->assertNull($fieldEvent->subscriberWere->lastName);
        $this->assertNull($fieldEvent->subscriberWere->email);
        $this->assertNull($fieldEvent->subscriberWere->zip);
        $this->assertNull($fieldEvent->subscriberWere->region);
        $this->assertNull($fieldEvent->subscriberWere->hasBrobizz);
        $this->assertNull($fieldEvent->subscriberWere->brobizzNumber);
        $this->assertNull($fieldEvent->subscriberWere->country);
        $this->assertNull($fieldEvent->subscriberWere->foreignId);
    }
}
