<?php

namespace KupShop\DevelopmentBundle\Resources\upgrades;

use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

class CreateShopDBUpgrade extends \UpgradeNew
{
    protected $priority = -1100;

    public function check_dbExists()
    {
        return defined('TEST_SUITE') && TEST_SUITE == 'shop' && ($this->ensureDbExists('phpunit_test_shop') || $this->checkTableExists('products', 'phpunit_test_shop'));
    }

    /** Create testing database  */
    public function upgrade_dbExists()
    {
        $container = ServiceContainer::getContainer();
        $command = sprintf(
            'mysql -u %s -p%s %s < %s',
            $container->getParameter('kupshop.db.user'),
            $container->getParameter('kupshop.db.password'),
            $container->getParameter('kupshop.db.database'),
            __DIR__.'/database_shop.sql'
        );
        exec($command, $output, $return);

        \Settings::clearCache(true);
    }
}
