<commit_before><?php

/*
 * @package         Tests
 * @copyright       Copyright (C) 2012-2016 BillRun Technologies Ltd. All rights reserved.
 * @license         GNU Affero General Public License Version 3; see LICENSE.txt
 */

/**
 * Test case class for the config module
 *
 * @package         Tests
 * @subpackage      Config
 * @since           4.4
 */
class Tests_UpdateRowSetUp {

	/**
	 * json files names in data dir
	 * each file will be added to the relevant collection.
	 * 
	 * @var array
	 */
	protected $collectionToClean = ['plans', 'services', 'subscribers', 'rates', 'lines', 'balances'];
	protected $importData = ['config', 'plans', 'services', 'subscribers', 'rates',];
	protected $backUpData = array();
	protected $config;

	//protected $config;
	public function __construct() {
		
	}

	/**
	 * executes set up for update row test
	 */
	public function setColletions() {
		$this->backUpCollection($this->importData);
		$this->cleanCollection($this->collectionToClean);
		//$this->backUpConfig();
		foreach ($this->importData as $file) {
			$dataAsText = file_get_contents(dirname(__FILE__) . '/data/' . $file . '.json');
			$parsedData = json_decode($dataAsText, true);
			if ($parsedData === null) {
				echo('Cannot decode <span style="color:#ff3385; font-style: italic;">' . $file . '.json. </span> <br>');
				continue;
			}
			$data = $this->fixDates($parsedData['data']);
			$coll = Billrun_Factory::db()->{$parsedData['collection']}();
			$coll->batchInsert($data);
		}

		/* $dir = dirname(__FILE__).'/data';
		  $files1 = scandir($dir); */
	}

	public function restoreColletions() {
		$this->cleanCollection($this->collectionToClean);
		$this->restoreCollection();
	}

	/**
	 * tranform all fields starts with time* into MongoDate object
	 * @param array $jsonAr 
	 */
	protected function fixDates($jsonAr) {
		foreach ($jsonAr as $key => $jsonFile) {
			$jsonAr[$key] = $this->fixArrayDates($jsonFile);
		}
		return $jsonAr;
	}

	/**
	 * tranform all fields starts with time* into MongoDate object
	 * @param array $jsonAr 
	 */
	protected function fixArrayDates($arr) {
		if (!is_array($arr)) {
			return $arr;
		}
		foreach ($arr as $jsonField => $value) {
			if (is_string($value)) {
				$value = explode("*", $value);
				if ((count($value) == 2) && ($value[0] == 'time')) {
					$value = new MongoDate(strtotime($value[1]));
					$arr[$jsonField] = $value;
				}
			} else if (is_array($arr)) {
				$arr[$jsonField] = $this->fixArrayDates($value);
			}
		}
		return $arr;
	}

	/**
	 * @param array $colNames array of collectins names to clean
	 */
	protected function cleanCollection($colNames) {
		foreach ($colNames as $colName) {
			$colName = $colName . 'Collection';
			Billrun_Factory::db()->$colName()->remove([null]);
		}
	}

	protected function backUpCollection($colNames) {
		foreach ($colNames as $colName) {
			$colName = $colName . 'Collection';
			$items = iterator_to_array(Billrun_Factory::db()->$colName()->query(array())->getIterator());
			$this->backUpData[$colName] = array();
			foreach ($items as $item) {
				array_push($this->backUpData[$colName], $item->getRawData());
			}
		}
	}

	protected function restoreCollection() {
		foreach ($this->backUpData as $colName => $items) {
			if (count($items) > 0) {
				if ($colName == 'configCollection') {
					unset($items[0]['_id']);
					echo "<pre>";
				//	print_r($items[0]);
			//	Billrun_Factory::db()->configCollection()->batchInsert($items[0]);
					MongoCollection::insert($items[0]); 
				} else {
					Billrun_Factory::db()->$colName()->batchInsert($items);
				}
			}
		}

	}

}<commit_msg>fix the config restor<commit_after><?php

/*
 * @package         Tests
 * @copyright       Copyright (C) 2012-2016 BillRun Technologies Ltd. All rights reserved.
 * @license         GNU Affero General Public License Version 3; see LICENSE.txt
 */

/**
 * Test case class for the config module
 *
 * @package         Tests
 * @subpackage      Config
 * @since           4.4
 */
class Tests_UpdateRowSetUp {

	/**
	 * json files names in data dir
	 * each file will be added to the relevant collection.
	 * 
	 * @var array
	 */
	protected $collectionToClean = ['plans', 'services', 'subscribers', 'rates', 'lines', 'balances'];
	protected $importData = ['plans', 'services', 'subscribers', 'rates',];
	protected $backUpData = array();
	protected $config;
	protected $configCollection;

	//protected $config;
	public function __construct() {
		
	}

	/**
	 * executes set up for update row test
	 */
	public function setColletions() {
		$this->backUpCollection($this->importData);
		$this->cleanCollection($this->collectionToClean);
		//$this->backUpConfig();
		foreach ($this->importData as $file) {
			$dataAsText = file_get_contents(dirname(__FILE__) . '/data/' . $file . '.json');
			$parsedData = json_decode($dataAsText, true);
			if ($parsedData === null) {
				echo('Cannot decode <span style="color:#ff3385; font-style: italic;">' . $file . '.json. </span> <br>');
				continue;
			}
			$data = $this->fixDates($parsedData['data']);
			$coll = Billrun_Factory::db()->{$parsedData['collection']}();
			$coll->batchInsert($data);
		}

		/* $dir = dirname(__FILE__).'/data';
		  $files1 = scandir($dir); */
	}

	public function restoreColletions() {
		$this->cleanCollection($this->collectionToClean);
		$this->restoreCollection();
	}

	/**
	 * tranform all fields starts with time* into MongoDate object
	 * @param array $jsonAr 
	 */
	protected function fixDates($jsonAr) {
		foreach ($jsonAr as $key => $jsonFile) {
			$jsonAr[$key] = $this->fixArrayDates($jsonFile);
		}
		return $jsonAr;
	}

	/**
	 * tranform all fields starts with time* into MongoDate object
	 * @param array $jsonAr 
	 */
	protected function fixArrayDates($arr) {
		if (!is_array($arr)) {
			return $arr;
		}
		foreach ($arr as $jsonField => $value) {
			if (is_string($value)) {
				$value = explode("*", $value);
				if ((count($value) == 2) && ($value[0] == 'time')) {
					$value = new MongoDate(strtotime($value[1]));
					$arr[$jsonField] = $value;
				}
			} else if (is_array($arr)) {
				$arr[$jsonField] = $this->fixArrayDates($value);
			}
		}
		return $arr;
	}

	/**
	 * @param array $colNames array of collectins names to clean
	 */
	protected function cleanCollection($colNames) {
		foreach ($colNames as $colName) {
			$colName = $colName . 'Collection';
			Billrun_Factory::db()->$colName()->remove([null]);
		}
	}

	protected function backUpCollection($colNames) {
		
		foreach ($colNames as $colName) {
			$colName = $colName . 'Collection';
			$items = iterator_to_array(Billrun_Factory::db()->$colName()->query(array())->getIterator());
			$this->backUpData[$colName] = array();
			foreach ($items as $item) {
				array_push($this->backUpData[$colName], $item->getRawData());
			}

		}
	}
	protected function backUpConfig() {
			$items = iterator_to_array(Billrun_Factory::db()->configCollection()->query(array())->getIterator());
			$this->configCollection = $item->getRawData();
			
//			echo "<pre>";
//			print_r($this->backUpData[$colName]);die;
		}
	

	protected function restoreCollection() {
		foreach ($this->backUpData as $colName => $items) {
			if (count($items) > 0) {
					Billrun_Factory::db()->$colName()->batchInsert($items);
			}
		}

	}

}
{'fraction_non_alphanumeric': 0.17832553015698155, 'fraction_numerical': 0.006058936931974662, 'mean_word_length': 3.6948933419521657, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5878217', 'n_tokens_mistral': 2671, 'n_tokens_neox': 2330, 'n_words': 534}