<?php

/**
 * Created by PhpStorm.
 * User: ondra
 * Date: 24.10.17
 * Time: 12:03.
 */

namespace KupShop\ContentBundle\Tests;

use KupShop\ContentBundle\View\NewsletterSubscribeView;
use KupShop\ContentBundle\View\NewsletterUnsubscribeView;
use KupShop\KupShopBundle\Exception\PermanentRedirectException;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use Query\Operator;
use Symfony\Component\HttpFoundation\Request;

class NewsletterTest extends \DatabaseTestCase
{
    public function testSubscribeSendMail()
    {
        $view = ServiceContainer::getService(NewsletterSubscribeView::class);

        $request = new Request();
        $request->request->set('email', 'test@test.cz');
        $view->setRequest($request);

        $vars = $view->getBodyVariables();

        $this->assertTrue($vars['subscribed']);
    }

    public function testSubscribeWrongEmail()
    {
        $view = ServiceContainer::getService(NewsletterSubscribeView::class);

        $request = new Request();
        $request->request->set('email', 'nesmysl');
        $view->setRequest($request);

        $vars = $view->getBodyVariables();

        $this->assertFalse($vars['subscribed']);
    }

    public function testSubscribe()
    {
        $view = ServiceContainer::getService(NewsletterSubscribeView::class);

        $today = new \DateTime();
        $today = $today->format('d.m.Y');

        $request = new Request();
        $request->request->set('email', 'test@test.cz');
        $request->request->set('code', md5('test@test.cz-'.$today));
        $view->setRequest($request);

        $this->expectException(PermanentRedirectException::class);

        $view->getBodyVariables();

        $news = sqlQueryBuilder()
            ->select('get_news')
            ->from('users')
            ->where(Operator::equals(['email' => 'test@test.cz']))
            ->execute()->fetchColumn();

        $this->assertEquals('Y', $news);
    }

    public function testUnsubscribe()
    {
        $view = ServiceContainer::getService(NewsletterUnsubscribeView::class);

        $unsubscribe = 'y89LKUp0KEktLtFLrgIA';
        $request = new Request();
        $request->request->set('unsubscribe', $unsubscribe);
        $request->request->set('email', 'ondra@test.cz');
        $view->setRequest($request);

        $vars = $view->getBodyVariables();

        $this->assertEquals('ondra@test.cz', $vars['unsubscribed']);
    }

    public function getDataSet()
    {
        return $this->getJsonDataSet('
        {
            "users" : [
              {
                "id":1,
                "passw":"",
                "user_key":"",
                "figure":"N",
                "name":"",
                "surname":"",
                "firm":"",
                "street":"",
                "city":"",
                "zip":"",
                "country":"CZ",
                "currency":"CZK",
                "email":"ondra@test.cz",
                "ico":"",
                "dic":"",
                "phone":"",
                "mobile":"",
                "fax":"",
                "delivery_name":"",
                "delivery_surname":"",
                "delivery_firm":"",
                "delivery_street":"",
                "delivery_city":"",
                "delivery_zip":"",
                "delivery_country":"CZ",
                "account_no":"",
                "account_bank":"",
                "account_symbol":"",
                "get_news":"Y",
                "prefer_transport":null,
                "date_reg":"2017-10-25 06:36:07",
                "date_logged":null,
                "custom_address":"",
                "state":"",
                "delivery_custom_address":"",
                "delivery_state":""
              }
            ]
        }
        ');
    }
}
