<?php

namespace KupShop\CatalogBundle\Tests\AutomaticImport;

use Query\Operator;

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

    public function testParameterGroup()
    {
        $params = [
            'type' => \AutomaticImport::TYPE_XML,
            'id' => -1,
            'id_supplier' => -1,
            'transformation' => file_get_contents(__DIR__.'/transformation.xslt'),
            'modify_in_store' => 1,
            'pair' => 1,
            'add_new' => 1,
            'source' => __DIR__.'/AutomaticImportTest.xml',
            'sourceFile' => __DIR__.'/AutomaticImportTest.xml',
        ];

        $import = new \AutomaticImport($params);

        $import->process();

        $qb = sqlQueryBuilder()
            ->select('code, id_parameter_group')
            ->from('products')
            ->andWhere(Operator::equals(['code' => '13783']))
            ->execute()
            ->fetch();
        $this->assertEquals(['code' => '13783', 'id_parameter_group' => '2'], $qb);
    }

    public function testParsing()
    {
        $params = [
            'type' => \AutomaticImport::TYPE_XML,
            'id' => -1,
            'id_supplier' => -1,
            'transformation' => file_get_contents(__DIR__.'/transformation.xslt'),
            'modify_in_store' => 1,
            'pair' => 1,
            'add_new' => 1,
            'source' => __DIR__.'/AutomaticImportTest.xml',
            'sourceFile' => __DIR__.'/AutomaticImportTest.xml',
        ];

        $import = new \AutomaticImport($params);
        $import->display = true;

        $this->assertTrue($import->parseParams());
        $this->assertTrue($import->getSourceFile());
        $this->assertTrue($import->getXML());
        $this->assertTrue($import->transformXML());
        $this->assertTrue($import->parseProducts());

        $this->assertCount(2, $import->products);

        $product = $import->products[0];
        $this->assertEquals($product['product_parameters'], [
            14 => [0 => 1, 1 => 2],
            -1 => [],
            0 => [],
            17 => [0 => 14],
        ]);
        $this->assertEquals($product['product_parameters_force'], [
            14 => true,
            17 => false,
        ]);

        $product = $import->products[1];
        $this->assertEquals($product['product_parameters'], [
            14 => [0 => 1],
            17 => [0 => 14],
        ]);
        $this->assertEquals($product['product_parameters_force'], [
            14 => null,
            17 => false,
        ]);
    }

    /**
     * @dataProvider testFlagParsing_data
     */
    public function testFlagParsing($input, $output, $error = null, $mapping = [])
    {
        $import = new \AutomaticImport([]);
        $reflection = new \ReflectionClass($import);
        $method = $reflection->getMethod('parseXMLFlagValue');

        $product = [];
        $this->assertEquals($output, $method->invokeArgs($import, [$input, &$product, 'mapping' => $mapping]));
        $this->assertEquals($error, $product['error'][0] ?? null);
    }

    public function testFlagParsingData()
    {
        yield 'true' => ['true', true];
        yield 'false' => ['false', false];
        yield 'nonsense' => ['nonsense', false, 'Neplatná hodnota příznaku: nonsense'];
        yield '0' => ['0', false];
        yield '1' => ['1', true];
        yield '2' => ['2', true];
        yield '-1' => ['-1', false];
        yield 'extra value "pokus"' => ['pokus', 'pokus', null, ['pokus' => 'pokus']];
    }
}
