<?php

namespace KupShop\PohodaBundle\Tests\Orders\Delete;

use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\PohodaBundle\Utils\Exporters\OrdersDeleter;
use KupShop\PohodaBundle\Utils\PohodaConnector;
use KupShop\PohodaBundle\Utils\Tools\TransformHelper;
use Query\Operator;

class DeleteOrderTest extends \DatabaseTestCase
{
    public function getDataSet()
    {
        return $this->getJsonDataSetFromFile();
    }

    protected $PohodaConnector;
    /**
     * @var TransformHelper
     */
    protected $transformHelper;

    /**
     * @var OrdersDeleter
     */
    protected $ordersDeleter;

    protected $requestTemplate = 'requests/ordersDel.tpl';

    protected function setEnvironment()
    {
        $dbcfg = \Settings::getDefault();
        $dbcfg->saveValue('pohoda', []);
        $dbcfg->updateValue('pohoda', [
            'mservers' => [
                0 => [
                    'active' => 'N',
                    'url' => 'http://pohoda.atpt.cz:4052/xml',
                    'username' => 'Admin',
                    'password' => '',
                    'ico' => '0123456789',
                    'maintenanceFrom' => '',
                    'maintenanceTo' => '',
                    'productsIn' => [
                        'last_sync' => '13.5.2021 16:12:59',
                        'internet' => 'N', ],
                    'ordersOut' => [
                        'last_sync' => '13.5.2021 17:28:09',
                        'last_sync_diff' => '', ],
                    'issuedInvoiceIn' => [
                        'last_sync' => '',
                        'last_sync_diff' => '', ],
                ],
            ],
        ]);

        $this->ordersDeleter = $this->get(OrdersDeleter::class);
        $this->PohodaConnector = ServiceContainer::getService(PohodaConnector::class);
        $this->transformHelper = $this->get(TransformHelper::class);
    }

    public function testRequestOrder()
    {
        $this->setEnvironment();
        $this->ordersDeleter->setIDs(['207055', '207056']);

        $orders = $this->ordersDeleter->getData();
        if ($orders) {
            $request = $this->PohodaConnector->getRequestContent($this->requestTemplate, ['orders' => $orders]);
            $request = $this->ordersDeleter->replace($request);
            $this->assertEquals(file_get_contents(__DIR__.'/requestOrders.xml'), $request);
        }
    }

    public function testDeleteOrder()
    {
        $mock = $this->getMockBuilder(PohodaConnector::class)
         ->setMethods(['sendRequest'])
         ->getMock();

        $mock->method('sendRequest')
            ->willReturn(file_get_contents(__DIR__.'/responseOrders.xml'));
        $mock->method('setActiveConfigById')
            ->willReturn(true);
        $this->set(PohodaConnector::class, $mock);
        $this->PohodaConnector = $this->get(PohodaConnector::class);

        $this->setEnvironment();
        $this->ordersDeleter->setIDs(['207055', '207056']);
        $this->ordersDeleter->processData();

        $qb = sqlQueryBuilder()
            ->select('order_no, pohoda_sync_date, pohoda_id ')
            ->from('orders')
            ->andWhere(Operator::equals(['id' => '207055']))
            ->execute()
            ->fetch();
        $this->assertEquals('B21207055', $qb['order_no']);
        $this->assertEquals(null, $qb['pohoda_sync_date']);
        $this->assertEquals(null, $qb['pohoda_id']);

        $qb = sqlQueryBuilder()
            ->select('order_no, pohoda_sync_date, pohoda_id ')
            ->from('orders')
            ->andWhere(Operator::equals(['id' => '207056']))
            ->execute()
            ->fetch();
        $this->assertEquals('021207056', $qb['order_no']);
        $this->assertEquals(null, $qb['pohoda_id']);
    }
}
