<?php

namespace KupShop\GTMOldBundle\Tests;

use KupShop\GTMBundle\Tests\GTMTestHelper;
use KupShop\GTMOldBundle\Utils\DataContainer;
use Symfony\Component\BrowserKit\Cookie;

class GTMPageTest extends \DatabaseTestCase
{
    use GTMTestHelper;

    public function testCartPage()
    {
        $user = \User::createFromId(1);
        $user->activateUser();

        $client = $this->createClient();

        $dataContainer = $this->get(DataContainer::class);
        $this->set(DataContainer::class, $dataContainer);

        $cookie = new Cookie('userlogin', '123');
        $client->getCookieJar()->set($cookie);
        $cookie = new Cookie('PHPSESSID', '12356');
        $client->getCookieJar()->set($cookie);

        $client->request('GET', '/kosik/');

        $exp_res = [
            'page' => [
                'language' => 'cs',
                'type' => 'cart',
                'title' => 'Nákupní košík',
                'path' => '/kosik/',
                'currency' => 'CZK',
                'currencyRate' => \Decimal::fromString('1.00000000', 8),
            ],
            'user' => [
                'userType' => 'loggedIn',
                'userID' => '1',
                'sessionId' => '12356',
                'cartId' => '123456',
                'cookie_bar' => true,
                'email' => 'petr@wpj.cz',
            ],
            'google_tag_params' => [
                'ecomm_pagetype' => 'cart',
            ],
            'cart' => [
                'step' => 1,
                'stepName' => 'cart',
            ],
            'resetRefferer' => false,
        ];

        $data = $dataContainer->getDataLayer();

        $this->assertEquals($exp_res, $this->recursivelyChangeType($data));
    }

    public function testHome()
    {
        $client = $this->createClient();

        $cookie = new Cookie('userlogin', '123');
        $client->getCookieJar()->set($cookie);

        $dataContainer = $this->get(DataContainer::class);
        $this->set(DataContainer::class, $dataContainer);

        $client->request('GET', '/');

        $exp_res = [
            'page' => [
                'language' => 'cs',
                'type' => 'home',
                'title' => '',
                'path' => '/',
                'currency' => 'CZK',
                'currencyRate' => \Decimal::fromString('1.00000000', 8),
            ],
            'user' => [
                'userType' => 'loggedOut',
                'cartId' => '123456',
                'cookie_bar' => true,
            ],
            'google_tag_params' => [
                'ecomm_pagetype' => 'home',
            ],
            'resetRefferer' => false,
        ];

        $dataContainer = $this->get(DataContainer::class);
        $data = $dataContainer->getDataLayer();

        $this->assertEquals($exp_res, $this->recursivelyChangeType($data));
    }
}
