<?php

namespace PeytzMail\Tests\Entity;

use PeytzMail\Tests\TestCase;
use PeytzMail\Entity\Mailinglist;

class MailinglistTest extends TestCase
{
    public function testCheckEntitysPropertiesCreatedWithAJsonString()
    {
        $json = <<<JSON
{
    "mailinglist" : {
        "foo"     : "bar",
        "integer" : 123,
        "float"   : 0.5,
        "boolean" : false
    }
}
JSON;

        $mailinglist = new Mailinglist;
        $mailinglist->createFromJson($json);

        $this->assertSame('bar', $mailinglist->foo);
        $this->assertSame(123, $mailinglist->integer);
        $this->assertSame(0.5, $mailinglist->float);
        $this->assertFalse($mailinglist->boolean);
    }

    public function testWrongJsonFormatShouldFail()
    {
        $this->setExpectedException(
            '\\PeytzMail\\Exception\\InvalidArgumentException',
            '[Peytz Mail] We cannot create the entity with this JSON data.'
        );

        $json = <<<JSON
{
    "mailinglists" : [
        {
            "foo" : "bar"
        },
        {
            "baz" : "qmx"
        }
    ]
}
JSON;

        $mailinglist = new Mailinglist;
        $mailinglist->createFromJson($json);
    }

    public function testCheckEntitysPropertiesCreatedWithAStandardObject()
    {
        $object = new \stdClass;
        $object->foo     = 'bar';
        $object->integer = 123;
        $object->float   = 0.5;
        $object->boolean = false;

        $mailinglist = new Mailinglist;
        $mailinglist->createFromObject($object);

        $this->assertSame('bar', $mailinglist->foo);
        $this->assertSame(123, $mailinglist->integer);
        $this->assertSame(0.5, $mailinglist->float);
        $this->assertFalse($mailinglist->boolean);
    }
}
