<?php

/**
 * Created by PhpStorm.
 * User: filip
 * Date: 11/14/16
 * Time: 2:07 PM.
 */

use KupShop\DevelopmentBundle\Util\Tests\CartTestTrait;
use KupShop\OrderingBundle\Exception\CartValidationException;

class ProductSetTest extends DatabaseTestCase
{
    use CartTestTrait;

    /**
     * @dataProvider dataSet_testUpdateSetsStore
     */
    public function testUpdateSetsStore($productId, array $expectedSetsInStore)
    {
        /** @var \KupShop\CatalogBundle\Util\SetsUpdateStore $setsUpdateStore */
        $setsUpdateStore = $this->get(\KupShop\CatalogBundle\Util\SetsUpdateStore::class);
        $setsUpdateStore->updateSetsStore($productId);

        $query = sqlQuery('SELECT id, in_store FROM products WHERE id IN (?) ORDER BY id', [array_keys($expectedSetsInStore)], [\Doctrine\DBAL\Connection::PARAM_INT_ARRAY]);
        $result = $query->fetchAll(PDO::FETCH_KEY_PAIR);

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

    public function dataSet_testUpdateSetsStore()
    {
        return [
            'only set with product 1 updates' => [1, [
                '3' => '1',
                '6' => '6',
            ]],
            'only set with product 4 updates' => [4, [
                '3' => '0',
                '6' => '0',
            ]],
            'all sets update' => [null, [
                '3' => '1',
                '6' => '0',
            ]],
        ];
    }

    public function testOrderingSetProduct()
    {
        $dbcfg = Settings::getDefault();
        $dbcfg->saveValue('order_availability', \Settings::ORDER_AVAILABILITY_IN_STORE_OR_SUPPLIER_STORE);
        $dbcfg->clearCache(true);
        $this->createCart();

        $this->insertProduct(1, null, 3);
        $this->setInvoice();
        $this->setDeliveryType();

        $this->cart->createFromDB();
        $this->expectException(CartValidationException::class);
        $this->expectExceptionMessage('Produkt <strong>\'NIKE Capri LACE Test Dlouheho Nazvu Produktu Test Dlouheho Produktu Tes\'</strong> není v požadovaném množství skladem.');
        $this->cart->submitOrder();
    }

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