<?php
use Luticate\Utils\Business\LuDocParser;

/**
 * Created by PhpStorm.
 * User: robin
 * Date: 5/29/16
 * Time: 7:52 PM
 */
class LuDocParserTest extends \PHPUnit_Framework_TestCase
{
    public function testSimpleSummary()
    {
        $doc = new LuDocParser("/**
     * Get all data
     */");
        $doc->parse();
        $this->assertSame("Get all data\n", $doc->getSummary());
    }
    
    public function testSimpleSummaryMultiLine()
    {
        $doc = new LuDocParser("/**
     * Get all data
     * Using the database
     */");
        $doc->parse();
        $this->assertSame("Get all data\nUsing the database\n", $doc->getSummary());
    }

    public function testSimpleParam()
    {
        $doc = new LuDocParser('/**
     * Get all data
     * @param $myvar MyType some doc
     * @param MyType $myvar2 some other doc
     */');
        $doc->parse();
        $this->assertSame("Get all data\n", $doc->getSummary());
        
        $params = [];
        foreach ($doc->getParams() as $param) {
            $params[] = $param->jsonSerialize();
        }
        
        $expectedParams = [
            ["Name" => 'myvar', "Type" => "MyType", "Summary" => "some doc\n",
                "Constraints" => []],
            ["Name" => 'myvar2', "Type" => "MyType", "Summary" => "some other doc\n",
                "Constraints" => []]
        ];
        
        $this->assertSame($expectedParams, $params);
    }

    public function testSimpleParamMultiLine()
    {
        $doc = new LuDocParser('/**
     * Get all data
     * @param $myvar MyType some doc
     * @param MyType $myvar2 some other doc
     * on another line
     */');
        $doc->parse();
        $this->assertSame("Get all data\n", $doc->getSummary());

        $params = [];
        foreach ($doc->getParams() as $name => $param) {
            $params[$name] = $param->jsonSerialize();
        }

        $expectedParams = [
            'myvar' => ["Name" => 'myvar', "Type" => "MyType", "Summary" => "some doc\n",
                "Constraints" => []],
            'myvar2' => ["Name" => 'myvar2', "Type" => "MyType", "Summary" => "some other doc\non another line\n",
                "Constraints" => []]
        ];

        $this->assertSame($expectedParams, $params);
    }

    public function testAll()
    {
        $doc = new LuDocParser('/**
     * Get all data
     * On another line
     * @param $myvar MyType some doc
     * Too much doc
     * @param MyType $myvar2 some other doc
     * on another line
     * @min 42
     * @max 42
     * @between 0 42
     * @another 1 2 42.42 true false "a string" \'string\' null
     * and another
     * @return AnotherType
     */');
        $doc->parse();
        $this->assertSame("Get all data\nOn another line\n", $doc->getSummary());
        $this->assertSame("AnotherType", $doc->getReturnType());

        $params = [];
        foreach ($doc->getParams() as $name => $param) {
            $params[$name] = $param->jsonSerialize();
        }

        $expectedParams = [
            'myvar' => ["Name" => 'myvar', "Type" => "MyType", "Summary" => "some doc\nToo much doc\n",
                "Constraints" => []],
            'myvar2' => ["Name" => 'myvar2', "Type" => "MyType", "Summary" => "some other doc\non another line\nand another\n",
                "Constraints" => [
                    ["Method" => "min", "Arguments" => [42]],
                    ["Method" => "max", "Arguments" => [42]],
                    ["Method" => "between", "Arguments" => [0, 42]],
                    ["Method" => "another", "Arguments" => [1, 2, 42.42, true, false, "a string", "string", null]]
                ]
            ]
        ];

        $this->assertSame($expectedParams, $params);
    }
}