<?php
use Luticate\Utils\Dbo\LuDbo;
use Luticate\Utils\Dbo\LuDboConstraintException;

/**
 * Created by PhpStorm.
 * User: robin
 * Date: 6/5/16
 * Time: 7:02 PM
 */

class TestDbo extends LuDbo {

    /**
     * @var $_test string
     */
    private $_test;

    /**
     * @return string
     */
    public function getTest()
    {
        return $this->_test;
    }

    /**
     * @param string $test
     */
    public function setTest($test)
    {
        $this->_test = $test;
    }
    
    public function ensure42()
    {
        if ($this->_test != "42") {
            throw new LuDboConstraintException("Only 42 is allowed");
        }
    }

}

class TestDbo2 extends LuDbo {

    /**
     * @var $_test2 TestDbo[]
     */
    private $_test2;

    /**
     * @var $_test3 TestDbo
     * @nullable
     */
    private $_test3;

    /**
     * @return TestDbo[]
     */
    public function getTest2()
    {
        return $this->_test2;
    }

    /**
     * @param TestDbo[] $test2
     */
    public function setTest2($test2)
    {
        $this->_test2 = $test2;
    }

    /**
     * @return TestDbo
     */
    public function getTest3()
    {
        return $this->_test3;
    }

    /**
     * @param TestDbo $test3
     */
    public function setTest3($test3)
    {
        $this->_test3 = $test3;
    }

}

class LuDboDeserializeTest extends \PHPUnit_Framework_TestCase{
    
    public function test()
    {
        $json = ["Test" => "Test."];
        $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo')->jsonSerialize());
    }
    
    public function test2()
    {
        $json = [
            "Test2" => [
                ["Test" => "Test."],
                ["Test" => "Test.2"]
            ],
            "Test3" => ["Test" => "Test.3"]
        ];
        $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo2')->jsonSerialize());
    }

    public function test3()
    {
        $json = [
            "Test2" => [
                ["Test" => "Test."],
                ["Test" => "Test.2"]
            ],
            "Test3" => null
        ];
        $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo2')->jsonSerialize());
    }

    public function test4()
    {
        $json = [
            "Test2" => [
                ["Test" => "Test."],
                null
            ],
            "Test3" => null
        ];
        $this->assertSame($json, LuDbo::deserializeValue($json, 'TestDbo2')->jsonSerialize());
    }
}