<?php

namespace PeytzMail\Tests\Entity;

use PeytzMail\Tests\TestCase;
use PeytzMail\Entity\TrackingEvents;

class TrackingEventsTest extends TestCase
{
    public function testUnsubscribesCheckEntitysPropertiesCreatedWithAJsonString()
    {
        $json = <<<JSON
{
    "current_page"  : 1,
    "total_records" : 2,
    "total_pages"   : 1,
    "next_page"     : null,
    "unsubscribes"  : [
        {
            "created"       : "2013-10-17T14:47:08+02:00",
            "email"         : "john.smith@example.com",
            "track_id"      : "525fdc4c2ceb65693c00008a",
            "foreign_id"    : "",
            "subscriber_id" : "john-smith-example-com",
            "type"          : "complaint"
        },
        {
            "created"       : "2013-10-17T13:41:58+02:00",
            "email"         : "jane.smith@example.com",
            "track_id"      : "525fdc4c2ceb65693c00008a",
            "foreign_id"    : "",
            "subscriber_id" : "jane-smith-example-com",
            "type"          : "frontend"
        }
    ]
}
JSON;

        $event = 'unsubscribes';

        $trackingEvents = new TrackingEvents;
        $trackingEvents->createFromJson($json, $event);

        $this->assertSame(1, $trackingEvents->currentPage);
        $this->assertSame(2, $trackingEvents->totalRecords);
        $this->assertSame(1, $trackingEvents->totalPages);
        $this->assertNull($trackingEvents->nextPage);

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

        $trackingEvent = $trackingEvents->$event;
        $this->assertInstanceOf('\\PeytzMail\\Entity\\TrackingEvent', $trackingEvent = $trackingEvent[0]);
        $this->assertSame('525fdc4c2ceb65693c00008a', $trackingEvent->trackId);
        $this->assertSame('john-smith-example-com', $trackingEvent->subscriberId);
        $this->assertSame('john.smith@example.com', $trackingEvent->email);
        $this->assertEmpty($trackingEvent->foreignId);
        $this->assertInstanceOf('\\DateTime', $trackingEvent->created);
        $this->assertSame('2013-10-17T14:47:08+02:00', $trackingEvent->created->format(DATE_RFC3339));
        $this->assertSame('complaint', $trackingEvent->type);

        $trackingEvent = $trackingEvents->$event;
        $this->assertInstanceOf('\\PeytzMail\\Entity\\TrackingEvent', $trackingEvent = $trackingEvent[1]);
        $this->assertSame('525fdc4c2ceb65693c00008a', $trackingEvent->trackId);
        $this->assertSame('jane-smith-example-com', $trackingEvent->subscriberId);
        $this->assertSame('jane.smith@example.com', $trackingEvent->email);
        $this->assertEmpty($trackingEvent->foreignId);
        $this->assertInstanceOf('\\DateTime', $trackingEvent->created);
        $this->assertSame('2013-10-17T13:41:58+02:00', $trackingEvent->created->format(DATE_RFC3339));
        $this->assertSame('frontend', $trackingEvent->type);
    }

    public function testClicksCheckEntitysPropertiesCreatedWithAJsonString()
    {
        $json = <<<JSON
{
    "current_page"  : 1,
    "total_records" : 2,
    "total_pages"   : 1,
    "next_page"     : null,
    "clicks"        : [
        {
            "created"       : "2013-10-17T14:47:08+02:00",
            "email"         : "john.smith@example.com",
            "track_id"      : "525fdc4c2ceb65693c00008a",
            "foreign_id"    : "",
            "subscriber_id" : "john-smith-example-com",
            "newsletter_id" : "my-newsletter"
        },
        {
            "created"       : "2013-10-17T13:41:58+02:00",
            "email"         : "jane.smith@example.com",
            "track_id"      : "525fdc4c2ceb65693c00008a",
            "foreign_id"    : "",
            "subscriber_id" : "jane-smith-example-com",
            "newsletter_id" : "my-newsletter"
        }
    ]
}
JSON;

        $event = 'clicks';

        $trackingEvents = new TrackingEvents;
        $trackingEvents->createFromJson($json, $event);

        $this->assertSame(1, $trackingEvents->currentPage);
        $this->assertSame(2, $trackingEvents->totalRecords);
        $this->assertSame(1, $trackingEvents->totalPages);
        $this->assertNull($trackingEvents->nextPage);

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

        $trackingEvent = $trackingEvents->$event;
        $this->assertInstanceOf('\\PeytzMail\\Entity\\TrackingEvent', $trackingEvent = $trackingEvent[0]);
        $this->assertSame('525fdc4c2ceb65693c00008a', $trackingEvent->trackId);
        $this->assertSame('john-smith-example-com', $trackingEvent->subscriberId);
        $this->assertSame('john.smith@example.com', $trackingEvent->email);
        $this->assertEmpty($trackingEvent->foreignId);
        $this->assertInstanceOf('\\DateTime', $trackingEvent->created);
        $this->assertSame('2013-10-17T14:47:08+02:00', $trackingEvent->created->format(DATE_RFC3339));
        $this->assertSame('my-newsletter', $trackingEvent->newsletterId);

        $trackingEvent = $trackingEvents->$event;
        $this->assertInstanceOf('\\PeytzMail\\Entity\\TrackingEvent', $trackingEvent = $trackingEvent[1]);
        $this->assertSame('525fdc4c2ceb65693c00008a', $trackingEvent->trackId);
        $this->assertSame('jane-smith-example-com', $trackingEvent->subscriberId);
        $this->assertSame('jane.smith@example.com', $trackingEvent->email);
        $this->assertEmpty($trackingEvent->foreignId);
        $this->assertInstanceOf('\\DateTime', $trackingEvent->created);
        $this->assertSame('2013-10-17T13:41:58+02:00', $trackingEvent->created->format(DATE_RFC3339));
        $this->assertSame('my-newsletter', $trackingEvent->newsletterId);
    }
}
