<?php

namespace KupShop\PriceHistoryBundle\Tests;

use KupShop\PriceHistoryBundle\EventListener\CronListener;

class PriceHistoryTest extends \DatabaseTestCase
{
    public function testGenerateNewHistory()
    {
        $this->assertEquals(0, returnSQLResult('SELECT COUNT(*) FROM price_history WHERE id_pricelist IS NULL'));
        $cron = new CronListener();
        $cron->checkPriceChanges();
        $this->assertEquals(18, returnSQLResult('SELECT COUNT(*) FROM price_history WHERE id_pricelist IS NULL'));
    }

    public function testGenerateNewPricelistHistory()
    {
        $cron = new CronListener();
        $cron->checkPriceListChanges();
        $this->assertEquals(2, returnSQLResult('SELECT COUNT(*) FROM price_history WHERE id_pricelist IS NOT NULL'));
        $this->assertArraySubset([
            [
                'id_pricelist' => '1',
                'id_product' => '1',
                'id_variation' => '16',
                'price' => '500.0000',
                'last' => '1',
                'up' => '0',
            ],
            [
                'id_pricelist' => '1',
                'id_product' => '3',
                'id_variation' => null,
                'price' => '600.0000',
                'last' => '1',
                'up' => '0',
            ],
        ], sqlQuery('SELECT * FROM price_history')->fetchAllAssociative());
    }

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