<?php

/**
 * Created by PhpStorm.
 * User: filip
 * Date: 7/15/16
 * Time: 11:47 AM.
 */
require_once __DIR__.'/../../web/functions.common.php';

class FunctionsCommonTest extends DatabaseTestCase
{
    public function testImplodeKeyValue()
    {
        $array = [
            'one' => 1,
            'two' => 2,
        ];

        $expected = 'one-1|two-2';

        $this->assertSame($expected, implodeKeyValue($array, '-', '|'));
    }

    /**
     * @dataProvider data_testPrintPrice
     */
    public function testPrintPrice($price, $printedPrice, $printPriceOptions, $price_round, $price_round_direction, $price_precision, $price_decimal_mark = null)
    {
        $currency = $this->get(\KupShop\KupShopBundle\Context\CurrencyContext::class)->getActive();
        $currency->setPriceRound($price_round);
        $currency->setPriceRoundDirection($price_round_direction);
        $currency->setPricePrecision($price_precision);

        if ($price_decimal_mark) {
            $currency->setPriceDecimalMark($price_decimal_mark);
        }

        $this->assertEquals($printedPrice, printPrice($price, $printPriceOptions));
    }

    public function data_testPrintPrice()
    {
        return [
            // Price rounding
            [toDecimal('10.123'), '10,12 Kč', [], '0', 'math', '2'], // 0
            [toDecimal('10.123'), '10,12 Kč', [], '1', 'math', '2'], // 1
            [toDecimal('10.654'), '10,50 Kč', [], '50', 'math', '2'], // 2
            [toDecimal('10.123'), '10,00 Kč', [], '100', 'math', '2'], // 3
            [toDecimal('10.123'), '10 Kč', [], '0', 'math', '0'], // 4
            [toDecimal('10.123'), '10,12 Kč', [], '0', 'math', '2'], // 5
            [toDecimal('10.123'), '10,12 Kč', [], '0', 'math', 'dynamic'], // 6
            [toDecimal('10.123'), '10 Kč', [], '100', 'math', '0'], // 7
            [toDecimal('10.123'), '10,00 Kč', [], '100', 'math', '2'], // 8
            [toDecimal('10.123'), '10 Kč', [], '100', 'math', 'dynamic'], // 9
            [toDecimal('10'), '10 Kč', [], '0', 'math', 'dynamic'], // 10
            [toDecimal('10.2'), '10,20 Kč', [], '0', 'math', 'dynamic'], // 11
            [toDecimal('10.2'), '10,20 Kč', [], '10', 'up', '2'], // 12
            [toDecimal('10.2')->removeVat(21)->addVat(21), '10,20 Kč', [], '10', 'up', '2'], // 13
            [toDecimal('10.23'), '10,25 Kč', [], '5', 'math', '2'], // 14
            [toDecimal('10.234'), '10,25 Kč', [], '5', 'math', '2'], // 15
            [toDecimal('10.222'), '10,20 Kč', [], '5', 'math', '2'], // 15
            [toDecimal('10.222'), '10.20 Kč', [], '5', 'math', '2', '.'], // 15
            [toDecimal('10.222'), '10,20 Kč', [], '5', 'math', '2', ','], // 15
            [toDecimal('10.10'), '10,00 Kč', [], '25', 'math', 2, ','],
            [toDecimal('10.16'), '10,25 Kč', [], '25', 'math', 2, ','],
            [toDecimal('10.61'), '10,50 Kč', [], '25', 'math', 2, ','],
            [toDecimal('10.69'), '10,75 Kč', [], '25', 'math', 2, ','],
            [toDecimal('10.83'), '10,75 Kč', [], '25', 'math', 2, ','],
            [toDecimal('10.88'), '11,00 Kč', [], '25', 'math', 2, ','],
        ];
    }

    public function testJsonDecodeStrictWorks()
    {
        $this->assertSame(['test' => 1], json_decode_strict('{"test": 1}', 1));
    }

    /**
     * @expectedException \JsonException
     *
     * @expectedExceptionCode 3
     *
     * @expectedExceptionMessage Error
     */
    public function testJsonDecodeStrictThrows()
    {
        json_decode_strict('{"}');
    }

    /**
     * @dataProvider data_testPageUrl
     */
    public function testPageUrl($url, $params)
    {
        $this->assertEquals($url, createScriptURL($params));
    }

    public function data_testPageUrl()
    {
        return [
            ['/kontakt/', ['s' => 'page', 'label' => 'contact']],
            ['/reklamace/', ['s' => 'page', 'label' => 'complaint-rules']],
            ['/reklamace', ['s' => 'page', 'ID' => '1']],
            ['/obchodni-podminky_p2.html', ['s' => 'page', 'ID' => '2']],
        ];
    }

    /**
     * @dataProvider data_testPreparePrice
     */
    public function testPreparePrice($string, $float)
    {
        $price = $this->preparePrice($string);
        $this->assertEquals($float, $price);
        $this->assertIsFloat($price);
    }

    public function data_testPreparePrice()
    {
        return [
            ['-123', -123],
            ['123.55', 123.55],
            ['123,55', 123.55],
            ['12 000,5', 12000.5],
            ['ahoj123', 123],
            ['.5', 0.5],
            ['0', 0],
        ];
    }

    public function testPreparePriceNull()
    {
        $string = null;
        $this->assertNull($this->preparePrice($string));
        $string = '';
        $this->assertNull($this->preparePrice($string));
    }

    public function testRequestFromConsole()
    {
        $this->assertEquals('https://www.kupshop.local/', path('home', [], 0));
    }

    protected function getDataSet()
    {
        return $this->getJsonDataSet(/* @lang JSON */ '{
                "menu_links": [
                  {
                    "id":31,
                    "id_menu":null,
                    "type":2,
                    "parent":null,
                    "list_order":4,
                    "name":"Reklamační řád",
                    "name_short":"",
                    "url":"reklamace",
                    "link":null,
                    "target":"",
                    "figure":"Y",
                    "data":null,
                    "id_block":null,
                    "template":"",
                    "meta_title":null,
                    "meta_description":null,
                    "meta_keywords":null,
                    "old_id_page":1
                  },
                  {
                    "id":32,
                    "id_menu":null,
                    "type":2,
                    "parent":null,
                    "list_order":4,
                    "name":"Obchodní podmínky",
                    "name_short":"",
                    "url":"obchodni-podminky_p2.html",
                    "link":null,
                    "target":"",
                    "figure":"Y",
                    "data":null,
                    "id_block":null,
                    "template":"",
                    "meta_title":null,
                    "meta_description":null,
                    "meta_keywords":null,
                    "old_id_page":2
                  }
                ]
        }');
    }
}
