<?php

namespace PeytzMail\Tests;

use PeytzMail\PeytzMail;
use PeytzMail\Entity\Events;

class SubscriberEventsTest extends TestCase
{
    protected $mailinglist = 'foo-bar-list';

    public function testWithInvalidChange()
    {
        $this->setExpectedException(
            '\\PeytzMail\\Exception\\InvalidArgumentException',
            '[Peytz Mail] Change parameter should be subscribe, unsubscribe, click or open.'
        );

        $peytzMail = new PeytzMail($this->getStubClient());
        $peytzMail->getSubscriberEvents($this->mailinglist, array('invalid-change'));
    }

    public function testWithInvalidSortingType()
    {
        $this->setExpectedException(
            '\\PeytzMail\\Exception\\InvalidArgumentException',
            '[Peytz Mail] Sorting parameter should be from_id, period_start and/or period_end.'
        );

        $peytzMail = new PeytzMail($this->getStubClient());
        $peytzMail->getSubscriberEvents($this->mailinglist, array('subscribe'), array('invalid-sorting-type' => 'value'));
    }

    public function testWeGotEvents()
    {
        $json = <<<JSON
{
    "events" : [
        {
            "event_id"         : "526fbeeda3aaa04664000002",
            "created"          : "2013-10-29T15:22:32+01:00",
            "change"           : "subscribe",
            "source"           : "frontend",
            "subscriber_token" : "uzcxdj"
        },
        {
            "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" : 2,
    "total_pages"   : 1,
    "next_page"     : null
}
JSON;

        $peytzMail = new PeytzMail($this->getMockClientReturns($json));
        $events    = $peytzMail->getSubscriberEvents($this->mailinglist, array('subscribe'), array('from_id' => '524047a20f912c5ca3000001'));

        $this->assertInstanceOf('\\PeytzMail\\Entity\\Events', $events);
        $this->assertSame(1, $events->currentPage);
        $this->assertSame(2, $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('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[1]);
        $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);
    }

    public function testWithANotExistingPage()
    {
        $json = <<<JSON
{
    "events": [
    ],
    "current_page"  : 10000,
    "total_records" : 225961,
    "total_pages"   : 462,
    "next_page"     : null
}
JSON;

        $peytzMail = new PeytzMail($this->getMockClientReturns($json));
        $events    = $peytzMail->getSubscriberEvents($this->mailinglist, array('subscribe'), array('from_id' => '524047a20f912c5ca3000001'), 10000);

        $this->assertInstanceOf('\\PeytzMail\\Entity\\Events', $events);
        $this->assertSame(10000, $events->currentPage);
        $this->assertSame(225961, $events->totalRecords);
        $this->assertSame(462, $events->totalPages);
        $this->assertNull($events->nextPage);

        $this->assertTrue(is_array($events->events));
        $this->assertEmpty($events->events);
    }
}
