<?php

namespace FilterTest;

class FilterVariationsTest extends \DatabaseTestCase
{
    /** @var \Filter */
    private $filter;

    /** @var \FilterParams */
    private $filterParams;

    public function setUp(): void
    {
        $this->filter = new \Filter();
        $this->filterParams = new \FilterParams();
        $this->filterParams->setSections([1, 2]);
        $this->filter->setFilterParams($this->filterParams);

        parent::setUp();
    }

    /**
     * @dataProvider dataSet_testSetVariation
     */
    public function testSetVariation($labelId, array $valueIds, array $expectedResult)
    {
        $this->filterParams->setVariation($labelId, $valueIds);

        $result = $this->filter->getProducers()['values'];
        $this->assertEquals($expectedResult, $result);
    }

    public function dataSet_testSetVariation()
    {
        return [
            [
                1,
                [1],
                [
                    1 => ['id' => '1', 'name' => 'Producer 1', 'photo' => 'x.jpg', 'count' => '1', 'filter_url' => null],
                ],
            ],
            [
                1,
                [1, 2],
                [
                    2 => ['id' => '2', 'name' => 'Producer 2', 'photo' => 'x.jpg', 'count' => '1', 'filter_url' => null],
                    1 => ['id' => '1', 'name' => 'Producer 1', 'photo' => 'x.jpg', 'count' => '1', 'filter_url' => null],
                ],
            ],
        ];
    }

    public function testGetVariationCountProduct()
    {
        $expected = [
            'param_name' => 'size',
            'values' => [
                '2' => ['code' => null, 'value_data' => null, 'count' => '2', 'name' => '42', 'filter_url' => null],
                '1' => ['code' => null, 'value_data' => null, 'count' => '1', 'name' => '41', 'filter_url' => null],
            ],
        ];
        $result = $this->filter->getVariation(1);

        $this->assertEquals($expected, $result);
    }

    public function testGetVariationCountVariation()
    {
        $expected = [
            'param_name' => 'size',
            'values' => [
                '2' => ['code' => null, 'value_data' => null, 'count' => '2', 'name' => '42', 'filter_url' => null],
                '1' => ['code' => null, 'value_data' => null, 'count' => '2', 'name' => '41', 'filter_url' => null],
            ],
        ];
        $this->filterParams->setResultEntity(\FilterParams::ENTITY_VARIATION);
        $result = $this->filter->getVariation(1);

        $this->assertEquals($expected, $result);
    }

    public function getDataset()
    {
        return new \PHPUnit\DbUnit\DataSet\YamlDataSet(__DIR__.'/FilterVariationsTest.yml');
    }
}
