<?php

declare(strict_types=1);

namespace KupShop\ConvertorsBundle\Tests;

use Query\Filter;

class FilterTest extends \DatabaseTestCase
{
    /**
     * @dataProvider dataSet_testByVariationsConvertions
     */
    public function testByVariationsConvertions(array $data, string $expected): void
    {
        $fetched = sqlQueryBuilder()
            ->select('p.id')
            ->fromProducts()
            ->joinVariationsOnProducts()
            ->where(Filter::byVariationsSizeConvertion($data))
            ->orderBy('p.id')
            ->groupBy('p.id')
            ->execute()
            ->fetchAllAssociative();

        $this->assertEquals(json_decode($expected, true), $fetched);
    }

    public function dataSet_testByVariationsConvertions(): iterable
    {
        yield 'Zafiltrovani podle konvertoru' => [[['value' => [384]]], '[{"id":"6167"},{"id":"6202"}]'];

        yield 'Zafiltrovani podle konvertoru + zafiltrovana hodnota nastavena u produktu primo v parametru, takze takovy produkt chceme taky najit' => [
            [['value' => [396]]], '[{"id": "6167"}, {"id": "6168"}]',
        ];

        yield 'Zafiltrovani vice hodnot podle konvertoru + zafiltrovana hodnota nastavena u produktu primo v parametru' => [
            [['value' => [396, 401]]], '[{"id":"6167"}, {"id": "6168"}, {"id": "6202"}]',
        ];
    }

    /**
     * @dataProvider dataSet_testGetSizeTable
     */
    public function testGetSizeTable(int $convertorId, string $expected): void
    {
        $filter = new \Filter();

        $sizeConvertions = $filter->getSizeConvertionVariation(1);

        if (empty($sizeConvertions)) {
            $this->fail();
        }

        $this->assertEquals(json_decode($expected, true), $sizeConvertions);
    }

    public function dataSet_testGetSizeTable(): iterable
    {
        return [
            [1, '{"values":{"L|0.1 l":{"count":1,"conversion_id":387,"conversion_value_type":"L","conversion_value":"0.1 l","conversion_value_description":"","conversion_value_data":[],"name":"0.1 l","conversion_type":"L","convertor_type":"text"},"L|0.25 l":{"count":1,"conversion_id":386,"conversion_value_type":"L","conversion_value":"0.25 l","conversion_value_description":"","conversion_value_data":[],"name":"0.25 l","conversion_type":"L","convertor_type":"text"},"L|0.5 l":{"count":2,"conversion_id":385,"conversion_value_type":"L","conversion_value":"0.5 l","conversion_value_description":"","conversion_value_data":[],"name":"0.5 l","conversion_type":"L","convertor_type":"text"},"L|0.75 l":{"count":2,"conversion_id":384,"conversion_value_type":"L","conversion_value":"0.75 l","conversion_value_description":"","conversion_value_data":[],"name":"0.75 l","conversion_type":"L","convertor_type":"text"},"L|1 l":{"count":2,"conversion_id":383,"conversion_value_type":"L","conversion_value":"1 l","conversion_value_description":"","conversion_value_data":[],"name":"1 l","conversion_type":"L","convertor_type":"text"},"L|2 l":{"count":1,"conversion_id":382,"conversion_value_type":"L","conversion_value":"2 l","conversion_value_description":"","conversion_value_data":[],"name":"2 l","conversion_type":"L","convertor_type":"text"},"L|3 l":{"count":1,"conversion_id":381,"conversion_value_type":"L","conversion_value":"3 l","conversion_value_description":"","conversion_value_data":[],"name":"3 l","conversion_type":"L","convertor_type":"text"},"L|5 l":{"count":1,"conversion_id":380,"conversion_value_type":"L","conversion_value":"5 l","conversion_value_description":"","conversion_value_data":[],"name":"5 l","conversion_type":"L","convertor_type":"text"},"ML|100 ml":{"count":1,"conversion_id":395,"conversion_value_type":"ML","conversion_value":"100 ml","conversion_value_description":"","conversion_value_data":[],"name":"100 ml","conversion_type":"ML","convertor_type":"text"},"ML|1000 ml":{"count":2,"conversion_id":391,"conversion_value_type":"ML","conversion_value":"1000 ml","conversion_value_description":"","conversion_value_data":[],"name":"1000 ml","conversion_type":"ML","convertor_type":"text"},"ML|2000 ml":{"count":1,"conversion_id":390,"conversion_value_type":"ML","conversion_value":"2000 ml","conversion_value_description":"","conversion_value_data":[],"name":"2000 ml","conversion_type":"ML","convertor_type":"text"},"ML|250 ml":{"count":1,"conversion_id":394,"conversion_value_type":"ML","conversion_value":"250 ml","conversion_value_description":"","conversion_value_data":[],"name":"250 ml","conversion_type":"ML","convertor_type":"text"},"ML|3000 ml":{"count":1,"conversion_id":389,"conversion_value_type":"ML","conversion_value":"3000 ml","conversion_value_description":"","conversion_value_data":[],"name":"3000 ml","conversion_type":"ML","convertor_type":"text"},"ML|500 ml":{"count":2,"conversion_id":393,"conversion_value_type":"ML","conversion_value":"500 ml","conversion_value_description":"","conversion_value_data":[],"name":"500 ml","conversion_type":"ML","convertor_type":"text"},"ML|5000 ml":{"count":1,"conversion_id":388,"conversion_value_type":"ML","conversion_value":"5000 ml","conversion_value_description":"","conversion_value_data":[],"name":"5000 ml","conversion_type":"ML","convertor_type":"text"},"ML|750 ml":{"count":2,"conversion_id":392,"conversion_value_type":"ML","conversion_value":"750 ml","conversion_value_description":"","conversion_value_data":[],"name":"750 ml","conversion_type":"ML","convertor_type":"text"},"DL|10 dl":{"count":1,"conversion_id":403,"conversion_value_type":"DL","conversion_value":"10 dl","conversion_value_description":"","conversion_value_data":[],"name":"10 dl","conversion_type":"DL","convertor_type":"text"},"DL|100 dl":{"count":2,"conversion_id":399,"conversion_value_type":"DL","conversion_value":"100 dl","conversion_value_description":"","conversion_value_data":[],"name":"100 dl","conversion_type":"DL","convertor_type":"text"},"DL|200 dl":{"count":1,"conversion_id":398,"conversion_value_type":"DL","conversion_value":"200 dl","conversion_value_description":"","conversion_value_data":[],"name":"200 dl","conversion_type":"DL","convertor_type":"text"},"DL|25 dl":{"count":1,"conversion_id":402,"conversion_value_type":"DL","conversion_value":"25 dl","conversion_value_description":"","conversion_value_data":[],"name":"25 dl","conversion_type":"DL","convertor_type":"text"},"DL|300 dl":{"count":1,"conversion_id":397,"conversion_value_type":"DL","conversion_value":"300 dl","conversion_value_description":"","conversion_value_data":[],"name":"300 dl","conversion_type":"DL","convertor_type":"text"},"DL|50 dl":{"count":2,"conversion_id":401,"conversion_value_type":"DL","conversion_value":"50 dl","conversion_value_description":"","conversion_value_data":[],"name":"50 dl","conversion_type":"DL","convertor_type":"text"},"DL|500 dl":{"count":1,"conversion_id":396,"conversion_value_type":"DL","conversion_value":"500 dl","conversion_value_description":"","conversion_value_data":[],"name":"500 dl","conversion_type":"DL","convertor_type":"text"},"DL|75 dl":{"count":2,"conversion_id":400,"conversion_value_type":"DL","conversion_value":"75 dl","conversion_value_description":"","conversion_value_data":[],"name":"75 dl","conversion_type":"DL","convertor_type":"text"}}}'],
        ];
    }

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