<?php

namespace Payjp;

class PayjpObjectTest extends TestCase
{
    public function testArrayAccessorsSemantics()
    {
        $s = new PayjpObject();
        $s['foo'] = 'a';
        $this->assertSame($s['foo'], 'a');
        $this->assertTrue(isset($s['foo']));
        unset($s['foo']);
        $this->assertFalse(isset($s['foo']));
    }

    public function testNormalAccessorsSemantics()
    {
        $s = new PayjpObject();
        $s->foo = 'a';
        $this->assertSame($s->foo, 'a');
        $this->assertTrue(isset($s->foo));
        unset($s->foo);
        $this->assertFalse(isset($s->foo));
    }

    public function testArrayAccessorsMatchNormalAccessors()
    {
        $s = new PayjpObject();
        $s->foo = 'a';
        $this->assertSame($s['foo'], 'a');

        $s['bar'] = 'b';
        $this->assertSame($s->bar, 'b');
    }

    public function testKeys()
    {
        $s = new PayjpObject();
        $s->foo = 'a';
        $this->assertSame($s->keys(), ['foo']);
    }

    public function testToArray()
    {
        $s = new PayjpObject();
        $s->foo = 'a';

        $converted = $s->__toArray();

        $this->assertSame('array', gettype($converted));
        $this->assertArrayHasKey('foo', $converted);
        $this->assertEquals('a', $converted['foo']);
    }

    public function testRecursiveToArray()
    {
        $s = new PayjpObject();
        $z = new PayjpObject();

        $s->child = $z;
        $z->foo = 'a';

        $converted = $s->__toArray(true);

        $this->assertSame('array', gettype($converted));
        $this->assertArrayHasKey('child', $converted);
        $this->assertSame('array', gettype($converted['child']));
        $this->assertArrayHasKey('foo', $converted['child']);
        $this->assertEquals('a', $converted['child']['foo']);
    }
}
