<?php

namespace KupShop\PohodaBundle\Tests;

use KupShop\DevelopmentBundle\Util\Tests\LoggerTestTrait;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\PohodaBundle\Utils\Importers\ProductsImporter;
use KupShop\PohodaBundle\Utils\Tools\TransformHelper;

class ImportProductsTest extends \DatabaseTestCase
{
    use LoggerTestTrait;

    public function getDataSet()
    {
        return $this->getJsonDataSetFromFile();
    }

    /**
     * @var TransformHelper
     */
    protected $transformHelper;
    /**
     * @var ProductsImporter
     */
    protected $productsImporter;

    protected function setEnvironment()
    {
        $this->productsImporter = ServiceContainer::getService(ProductsImporter::class);
        $this->transformHelper = ServiceContainer::getService(TransformHelper::class);
        $this->getLogger();
    }

    public function testTransformProductsIn()
    {
        $this->setEnvironment();

        $xmlResponse = file_get_contents(__DIR__.'/productsInResponse.xml');

        $resultXML = $this->transformHelper->baseTransform($xmlResponse, $this->transformHelper->loadXslt('productsIn.xsl'));

        $this->assertXmlStringEqualsXmlFile(__DIR__.'/productsInResult.xml', $resultXML);
    }

    public function testImportProducts()
    {
        $this->setEnvironment();

        $xmlResponse = file_get_contents(__DIR__.'/productsInResponse.xml');

        $resultXML = $this->transformHelper->baseTransform($xmlResponse, $this->transformHelper->loadXslt('productsIn.xsl'));

        $this->productsImporter->xml = $resultXML;

        if ($this->productsImporter->parseProducts()) {
            $result = $this->productsImporter->importProducts();
        }

        $testHandler = $this->getTestHandler();
        $this->assertEquals(true, $testHandler->hasNoticeThatContains('POHODA RESPONSE: Product Import'));

        $this->assertTrue($result);

        $product = sqlQueryBuilder()->select('id, title, code, price')->from('products')->where('id = 1000')->execute()->fetch();

        $this->assertEquals([
            'id' => 1000,
            'price' => 487.6033,
            'title' => 'Brzdové lanko Shimano DURA-ACE, SIL-TEC, 1,6 x 2050 mm',
            'code' => 'B00536',
        ], $product);
    }

    public function testImportProductsStores()
    {
        $this->insertSQL('stores', ['name' => 'Prvni sklad', 'id' => 2]);
        $this->insertSQL('stores', ['name' => 'Druhy sklad', 'id' => 3]);

        $this->setEnvironment();

        $this->productsImporter->xml = $xmlDOMResponse = new \DOMDocument();
        $xmlDOMResponse->loadXML(file_get_contents(__DIR__.'/ShopItems_withStores.xml'));

        if ($this->productsImporter->parseProducts()) {
            $result = $this->productsImporter->importProducts();
        }

        $this->assertTrue($result);

        $store1 = sqlQueryBuilder()->select('*')->from('stores_items')->where('id_product = 1000 and id_store = 2')->execute()->fetch();
        $this->assertEquals(2, $store1['quantity']);

        $store1 = sqlQueryBuilder()->select('*')->from('stores_items')->where('id_product = 1000 and id_store = 3')->execute()->fetch();
        $this->assertEquals(3, $store1['quantity']);

        /* Je to naschval v jednom testu, protože transakce maže sklady, ale nenastavi autoincrement, takze pak jsou jiny ID */

        $this->productsImporter->xml = $xmlDOMResponse = new \DOMDocument();
        $xmlDOMResponse->loadXML(file_get_contents(__DIR__.'/ShopItems_withStores.xml'));

        if ($this->productsImporter->parseProducts()) {
            $result = $this->productsImporter->importProducts();
        }

        $this->productsImporter->xml = $xmlDOMResponse = new \DOMDocument();
        $xmlDOMResponse->loadXML(file_get_contents(__DIR__.'/ShopItems_withStores.xml'));

        if ($this->productsImporter->parseProducts()) {
            $result = $this->productsImporter->importProducts();
        }

        $store1 = sqlFetchAll($this->selectSQL('stores_items', []));

        $this->assertEquals(2, count($store1));
    }
}
