<?php

namespace PeytzMail\Tests\Entity;

use PeytzMail\Tests\TestCase;
use PeytzMail\Entity\Events;

class EventsTest extends TestCase
{
    public function testCheckEntitysPropertiesCreatedWithAJsonString()
    {
        $json = <<<JSON
{
    "events" : [
        {
            "event_id"         : "524047a20f912c5ca3000001",
            "created"          : "2013-09-23T15:52:34+02:00",
            "change"           : "click",
            "source"           : "frontend",
            "subscriber_token" : "uzcxdj"
        },
        {
            "event_id"         : "524049b7098fd8a546000001",
            "created"          : "2013-09-23T16:01:27+02:00",
            "change"           : "open",
            "source"           : "frontend",
            "subscriber_token" : "uzcxdj"
        },
        {
            "event_id"         : "526fbeeda3aaa04664000002",
            "created"          : "2013-10-29T15:22:32+01:00",
            "change"           : "subscribe",
            "source"           : "frontend",
            "subscriber_token" : "uzcxdj"
        },
        {
            "event_id"         : "526fc4a8fdb132fd10000002",
            "created"          : "2013-10-29T16:25:50+01:00",
            "change"           : "unsubscribe",
            "source"           : "backend",
            "subscriber_token" : "ermjsx",
            "extra_fields" : {
                "email"      : "foo@bar.org",
                "foreign_id" : "my_foreign_id"
            }
        },
        {
            "event_id"         : "526fd37ea3aaa033e8000001",
            "created"          : "2013-10-29T16:25:50+01:00",
            "change"           : "subscribe",
            "source"           : "backend",
            "subscriber_token" : "ermjsx",
            "extra_fields" : {
                "email"          : "baz@bar.org",
                "full_name"      : "John Doe",
                "zip"            : "6800",
                "region"         : "WEST",
                "country"        : "dk",
                "has_brobizz"    : true,
                "brobizz_number" : "123456"
            }
        }
    ],
    "current_page"  : 1,
    "total_records" : 5,
    "total_pages"   : 1,
    "next_page"     : null
}
JSON;

        $events = new Events;
        $events->createFromJson($json);

        $this->assertSame(1, $events->currentPage);
        $this->assertSame(5, $events->totalRecords);
        $this->assertSame(1, $events->totalPages);
        $this->assertNull($events->nextPage);

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

        $this->assertInstanceOf('\\PeytzMail\\Entity\\Event', $event = $events->events[0]);
        $this->assertSame('524047a20f912c5ca3000001', $event->eventId);
        $this->assertInstanceOf('\\DateTime', $event->created);
        $this->assertSame('2013-09-23T15:52:34+02:00', $event->created->format(DATE_RFC3339));
        $this->assertSame('click', $event->change);
        $this->assertSame('frontend', $event->source);
        $this->assertSame('uzcxdj', $event->subscriberToken);
        $this->assertNull($event->subscriber);

        $this->assertInstanceOf('\\PeytzMail\\Entity\\Event', $event = $events->events[1]);
        $this->assertSame('524049b7098fd8a546000001', $event->eventId);
        $this->assertInstanceOf('\\DateTime', $event->created);
        $this->assertSame('2013-09-23T16:01:27+02:00', $event->created->format(DATE_RFC3339));
        $this->assertSame('open', $event->change);
        $this->assertSame('frontend', $event->source);
        $this->assertSame('uzcxdj', $event->subscriberToken);
        $this->assertNull($event->subscriber);

        $this->assertInstanceOf('\\PeytzMail\\Entity\\Event', $event = $events->events[2]);
        $this->assertSame('526fbeeda3aaa04664000002', $event->eventId);
        $this->assertInstanceOf('\\DateTime', $event->created);
        $this->assertSame('2013-10-29T15:22:32+01:00', $event->created->format(DATE_RFC3339));
        $this->assertSame('subscribe', $event->change);
        $this->assertSame('frontend', $event->source);
        $this->assertSame('uzcxdj', $event->subscriberToken);
        $this->assertNull($event->subscriber);

        $this->assertInstanceOf('\\PeytzMail\\Entity\\Event', $event = $events->events[3]);
        $this->assertSame('526fc4a8fdb132fd10000002', $event->eventId);
        $this->assertInstanceOf('\\DateTime', $event->created);
        $this->assertSame('2013-10-29T16:25:50+01:00', $event->created->format(DATE_RFC3339));
        $this->assertSame('unsubscribe', $event->change);
        $this->assertSame('backend', $event->source);
        $this->assertSame('ermjsx', $event->subscriberToken);
        $this->assertInstanceOf('\\PeytzMail\\Entity\\Subscriber', $event->subscriber);
        $this->assertSame('foo@bar.org', $event->subscriber->email);
        $this->assertSame('my_foreign_id', $event->subscriber->foreignId);

        $this->assertInstanceOf('\\PeytzMail\\Entity\\Event', $event = $events->events[4]);
        $this->assertSame('526fd37ea3aaa033e8000001', $event->eventId);
        $this->assertInstanceOf('\\DateTime', $event->created);
        $this->assertSame('2013-10-29T16:25:50+01:00', $event->created->format(DATE_RFC3339));
        $this->assertSame('subscribe', $event->change);
        $this->assertSame('backend', $event->source);
        $this->assertSame('ermjsx', $event->subscriberToken);
        $this->assertInstanceOf('\\PeytzMail\\Entity\\Subscriber', $event->subscriber);
        $this->assertSame('baz@bar.org', $event->subscriber->email);
        $this->assertSame('John Doe', $event->subscriber->fullName);
        $this->assertSame('6800', $event->subscriber->zip);
        $this->assertSame('WEST', $event->subscriber->region);
        $this->assertSame('dk', $event->subscriber->country);
        $this->assertTrue($event->subscriber->hasBrobizz);
        $this->assertSame('123456', $event->subscriber->brobizzNumber);
    }
}
