<?php

declare(strict_types=1);

namespace KupShop\CheckAppBundle\Tests;

use KupShop\WarehouseBundle\Util\StockInWorker;
use Query\Operator;

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

    public function testPriceBuyAfterSubmitStockIn()
    {
        $productPriceBuy = sqlQueryBuilder()->select('price_buy')->from('products')->where(Operator::equals(['id' => 3822]))->execute()->fetchColumn();
        $variationPriceBuy = sqlQueryBuilder()->select('price_buy')->from('products_variations')->where(Operator::equals(['id' => 1718603]))->execute()->fetchColumn();

        $this->assertEquals(null, $productPriceBuy);
        $this->assertEquals(null, $variationPriceBuy);

        $worker = $this->get(StockInWorker::class);
        $result = $worker->SubmitStockIn(21886, $this->getStockInRows());

        $this->assertTrue($result);

        $productPriceBuy = sqlQueryBuilder()->select('price_buy')->from('products')->where(Operator::equals(['id' => 3822]))->execute()->fetchColumn();
        $variationPriceBuy = sqlQueryBuilder()->select('price_buy')->from('products_variations')->where(Operator::equals(['id' => 1718603]))->execute()->fetchColumn();

        $this->assertEquals(105.4504, $productPriceBuy);
        $this->assertEquals(1150.0000, $variationPriceBuy);
    }

    protected function getStockInRows()
    {
        $data = [
            371506 => new \stdClass(),
            371505 => new \stdClass(),
            371507 => new \stdClass(),
        ];

        $data[371506]->checked = 2;
        $data[371506]->id = 371506;
        $data[371506]->id_product = 3822;
        $data[371506]->id_variation = null;

        $data[371505]->checked = 3;
        $data[371505]->id = 371505;
        $data[371505]->id_product = 101900;
        $data[371505]->id_variation = 1718603;

        $data[371507]->checked = 0;
        $data[371507]->id = 371507;
        $data[371507]->id_product = 101900;
        $data[371507]->id_variation = 1718604;

        return $data;
    }
}
