<commit_before><?php
/**
 *
 * Date: 10/10/16
 * Time: 12:47
 *
 * @copyright Copyright (c) 2016, <NAME> (<EMAIL>)
 * @license AGPLv3
 */
use \OCA\Passman\AppInfo\Application;

abstract class DatabaseHelperTest extends PHPUnit_Extensions_Database_TestCase {
	CONST DUMPS_DIR = __DIR__ . '/dumps/';

	/**
	 * This function should return the table name, for example
	 * for a test running on oc_passman_vaults it shall return ["oc_passman_vaults"]
	 * @internal
	 * @return string[]
	 */
	abstract public function getTablesNames();

	/**
	 * @var Application
	 */
	protected $app_container;

	/**
	 * @var \OCP\IDBConnection
	 */
	protected $db;

	/**
	 * @var PHPUnit_Extensions_Database_DataSet_MysqlXmlDataSet[]
	 */
	protected $datasets;

	public function __construct() {
		parent::__construct();
	}

	public function setUp() {
		$server = new \OC\Server(getenv('NEXTCLOUD_BASE_DIR'), new \OC\Config(getenv('NEXTCLOUD_CONFIG_DIR'), getenv('NEXTCLOUD_CONFIG_FILE')));
		$this->app_container = $server->getAppContainer('passman');

		$this->db = $this->app_container->getServer()->getDatabaseConnection();

		$this->datasets = [];
		$tables = $this->getTablesNames();
		foreach ($tables as $table){
			$this->datasets[$table] = $this->createMySQLXMLDataSet(self::DUMPS_DIR . $table . '.xml');
			$this->setUpTable($table);
		}
	}

	/**
	 * Initializes the table with the corresponding dataset on the dumps dir
	 *
	 * @param $table_name
	 */
	public function setUpTable($table_name){
		$table = $this->getTableDataset($table_name);

		// Cleanup any data currently inside the table
		$this->db->executeQuery("TRUNCATE $table_name;");

		// Fill the table with the data dumps
		for ($i = 0; $i < $table->getRowCount(); $i++) {
			$row = $table->getRow($i);

			$fields = "";
			$values = "";
			foreach ($row as $key => $value){
				$fields .= "`{$key}`, ";
				$values .= is_numeric($value) ? $value : ("'{$value}'") ;
				$values .= ", ";
			}
<commit_msg>Fix truncate table<commit_after><?php
/**
 *
 * Date: 10/10/16
 * Time: 12:47
 *
 * @copyright Copyright (c) 2016, <NAME> (<EMAIL>)
 * @license AGPLv3
 */
use \OCA\Passman\AppInfo\Application;

abstract class DatabaseHelperTest extends PHPUnit_Extensions_Database_TestCase {
	CONST DUMPS_DIR = __DIR__ . '/dumps/';

	/**
	 * This function should return the table name, for example
	 * for a test running on oc_passman_vaults it shall return ["oc_passman_vaults"]
	 * @internal
	 * @return string[]
	 */
	abstract public function getTablesNames();

	/**
	 * @var Application
	 */
	protected $app_container;

	/**
	 * @var \OCP\IDBConnection
	 */
	protected $db;

	/**
	 * @var PHPUnit_Extensions_Database_DataSet_MysqlXmlDataSet[]
	 */
	protected $datasets;

	public function __construct() {
		parent::__construct();
	}

	public function setUp() {
		$server = new \OC\Server(getenv('NEXTCLOUD_BASE_DIR'), new \OC\Config(getenv('NEXTCLOUD_CONFIG_DIR'), getenv('NEXTCLOUD_CONFIG_FILE')));
		$this->app_container = $server->getAppContainer('passman');

		$this->db = $this->app_container->getServer()->getDatabaseConnection();

		$this->datasets = [];
		$tables = $this->getTablesNames();
		foreach ($tables as $table){
			$this->datasets[$table] = $this->createMySQLXMLDataSet(self::DUMPS_DIR . $table . '.xml');
			$this->setUpTable($table);
		}
	}

	/**
	 * Truncates a table, platform aware
	 * @param $table_name
	 */
	public function truncateTable($table_name) {
		$this->db->executeQuery($this->db->getDatabasePlatform()->getTruncateTableSQL($table_name));
	}

	/**
	 * Initializes the table with the corresponding dataset on the dumps dir
	 *
	 * @param $table_name
	 */
	public function setUpTable($table_name){
		$table = $this->getTableDataset($table_name);

		// Cleanup any data currently inside the table
		$this->truncateTable($table_name);

		// Fill the table with the data dumps
		for ($i = 0; $i < $table->getRowCount(); $i++) {
			$row = $table->getRow($i);

			$fields = "";
			$values = "";
			foreach ($row as $key => $value){
				$fields .= "`{$key}`, ";
				$values .= is_numeric($value) ? $value : ("'{$value}'") ;
				$values .= ", ";
			}

{'fraction_non_alphanumeric': 0.16941747572815535, 'fraction_numerical': 0.007766990291262136, 'mean_word_length': 4.031746031746032, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '2991545', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1365, 'n_words': 306}