<?php

namespace KupShop\OrderingBundle\Tests;

use KupShop\DevelopmentBundle\Util\Tests\CartTestTrait;

class DeliveryTimeTest extends \DatabaseTestCase
{
    use CartTestTrait;

    protected function setUp(): void
    {
        parent::setUp();

        $dbcfg = \Settings::getDefault();
        $dbcfg['order_not_in_store'] = 'N';
    }

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

    public function testDeliveryTimeSuccess()
    {
        $this->createCart();
        $this->insertProduct(id_product: 20); // jde koupit i kdyz in_store = 0
        $this->insertProduct(id_product: 21); // nejde koupit kdyz in_store = 0
        $this->insertProduct(id_product: 22, id_variation: 1); // jde koupit i kdyz variace in_store = 0
        $this->insertProduct(id_product: 23, id_variation: 2); // nejde koupit kdyz variace in_store = 0
        $this->setDeliveryType();
        $this->cart->createFromDB();

        self::assertNull($this->cart->checkCartItems());
    }

    public function testDeliveryTimeFailOnProduct()
    {
        sqlQueryBuilder()->update('products')->directValues(['in_store' => 0])->andWhere('id=21')->execute();

        $this->createCart();
        $this->insertProduct(id_product: 20); // jde koupit i kdyz in_store = 0
        $this->insertProduct(id_product: 21); // nejde koupit kdyz in_store = 0
        $this->setDeliveryType();
        $this->cart->createFromDB();

        $this->expectExceptionMessage('Produkt <strong>\'Alpine Pro NEW SPIDER PTX\'</strong> není v požadovaném množství skladem.');
        $this->cart->checkCartItems();
    }

    public function testDeliveryTimeFailOnVariation()
    {
        sqlQueryBuilder()->update('products_variations')->directValues(['in_store' => 0])->andWhere('id=2')->execute();

        $this->createCart();
        $this->insertProduct(id_product: 22, id_variation: 1); // jde koupit i kdyz variace in_store = 0
        $this->insertProduct(id_product: 23, id_variation: 2); // nejde koupit kdyz variace in_store = 0
        $this->setDeliveryType();
        $this->cart->createFromDB();

        $this->expectExceptionMessage('Produkt <strong>\'Black and Decker Firestorm FSB14\'</strong> není v požadovaném množství skladem.');
        $this->cart->checkCartItems();
    }
}
