<?php

namespace Alsendo\AlsendoWrapper\Tests\Unit\Api\Zaslat;

use Alsendo\AlsendoWrapper\Api\Zaslat\Model\Order\Valuation\ZaslatOrderValuation;
use Alsendo\AlsendoWrapper\Api\Zaslat\Wrapper\OrderValuationRateWrapper;
use Alsendo\AlsendoWrapper\Json;
use PHPUnit\Framework\TestCase;

class ZaslatRatesGetResponseTest extends TestCase
{
    public function testRatesGetResponse()
    {
        $json = '{
        "status": 200,
        "message": "Found 3 offers",
        "rates": [{
            "carrier": "UPS",
            "service": "Export SK",
            "service_id": 21,
            "pickup_date": "2016-07-10",
            "delivery_date": "2016-07-12",
            "price": {
                "value": 178.51,
                "currency": "CZK"
            },
            "price_vat": {
                "value": 216,
                "currency": "CZK"
            }
        }]
    }';
        $decodedJson = json_decode($json, true, 12, JSON_THROW_ON_ERROR);
        $this->assertIsArray($decodedJson);
        $this->assertArrayHasKey('rates', $decodedJson);
        $this->assertArrayHasKey(0, $decodedJson['rates']);

        $rate = $decodedJson['rates'][0];
        $this->assertArrayHasKey('carrier', $rate);
        $this->assertArrayHasKey('service', $rate);
        $this->assertArrayHasKey('service_id', $rate);
        $this->assertArrayHasKey('pickup_date', $rate);
        $this->assertArrayHasKey('delivery_date', $rate);
        $this->assertArrayHasKey('price', $rate);
        $this->assertArrayHasKey('price_vat', $rate);

        $rate = OrderValuationRateWrapper::wrap($rate);

        $this->assertArrayHasKey('priceTable', $rate);
        $this->assertArrayHasKey('price', $rate['priceTable']);
        $this->assertArrayHasKey('currency', $rate['priceTable']);
        $this->assertArrayHasKey('priceGross', $rate['priceTable']);

        $orderValuation = Json::mapArrayToObject($rate, ZaslatOrderValuation::class);

        $this->assertInstanceOf(ZaslatOrderValuation::class, $orderValuation);
    }
}