Location: PHPKode > projects > OpenNitro > trunk/Nitro/Libraries/PHPUnit/Nitro_TestCase.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: Nitro_Test                                                       |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2003-2006 June Systems BV                                   |
// +---------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or modify it   |
// | under the terms of the GNU Lesser General Public License as published by  |
// | the Free Software Foundation; either version 2.1 of the License, or (at   |
// | your option) any later version.                                           |
// |                                                                           |
// | This library is distributed in the hope that it will be useful, but       |
// | WITHOUT ANY WARRANTY; without even the implied warranty of                |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser   |
// | General Public License for more details.                                  |
// |                                                                           |
// | You should have received a copy of the GNU Lesser General Public License  |
// | along with this library; if not, write to the Free Software Foundation,   |
// | Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA            |
// +---------------------------------------------------------------------------+
// | Authors: Jesper Avot <hide@address.com>                             |
// +---------------------------------------------------------------------------+
//
// $Id: Nitro_TestCase.inc.php 229 2008-04-17 09:20:31Z oli $
//

/**
 * This file contains the Nitro_Test wrapper class
 * 
 * @author			Jesper Avot <hide@address.com>
 * @copyright		2006 June Systems BV
 * @package			Classes
 * @subpackage	Nitro_Test
 */
class Nitro_TestCase extends TestCase {
	var $DB = NULL;
	var $PreparedTables = array();
	
	function Nitro_TestCase($Name = 'Nitro_TestCase')
	{
		global $DB;
		$this->DB = $DB['NitroTest']; //defined in INI
		
		parent::TestCase($Name);
	}
	
	function PrepareDatabaseTable($Tables = array(), $Datasets = array())
	{
		return $this->PrepareDatabaseTables($Tables, $Datasets);
	}
	
	function PrepareDatabaseTables($Tables = array(), $Datasets = array())
	{
		if (!is_array($Tables)) {
			if (strlen($Tables)) {
				$Tables = explode(',', $Tables);
			} else {
				$Tables = array();
			}
		}
		
		if (is_array($Tables)) {
			foreach ($Tables AS $TableName) {
				//structure
				$this->PrepareStructure($TableName);
				$this->PreparedTables[$TableName] = $TableName;
				
				//datasets
				$this->PrepareDataset($TableName, $Datasets);
			}
		}
		
		return TRUE;
	}
	
	function PrepareDatabaseTables_Search($SearchTerm = '', $Datasets = array())
	{
		if (strlen($SearchTerm)) {
			$Path = NITRO_ROOT.'/Tests/TestData/DB/';
			$DirectoryListing = glob($Path.$SearchTerm.'*');
			foreach ($DirectoryListing AS $Directory) {
				$TableName = str_replace($Path, '', $Directory);
				$this->PrepareDatabaseTable($TableName, $Datasets);
			}
			$return_value = TRUE;
		} else {
			$return_value = FALSE;
		}
		
		return $return_value;
	}
	
	function PrepareStructure($TableName = '')
	{
		$this->_checkDBConnection();

		if (strlen($TableName) && is_object($this->DB)) {
			$StructureFile = NITRO_ROOT.'/Tests/TestData/DB/'.$TableName.'/Structure.inc.php';
			if (file_exists($StructureFile)) {
				$Query = '';
				include $StructureFile;
				if (strlen($Query)) {
					$DropQuery = "
						DROP TABLE IF EXISTS ".$TableName."
					";
					$Result = $this->DB->query($DropQuery);
					if (DB::isError($Result)) { echo 'Drop Query failed: '.$DropQuery; exit; } 
					
					$Result = $this->DB->query($Query);
					if (DB::isError($Result)) { echo 'Structure Query failed: '.$Query; exit; } 
				}
			}
		}
	}
	
	function PrepareDatasets($Datasets = array())
	{
		if (is_array($this->PreparedTables)) {
			foreach ($this->PreparedTables AS $TableName) {
				$this->PrepareDataset($TableName, $Datasets);
			}
		}
	}
	
	function PrepareDataset($TableName = '', $Datasets = array())
	{
		$this->_checkDBConnection();

		if (!is_array($Datasets)) {
			if (strlen($Datasets)) {
				$Datasets = explode(',', $Datasets);
			} else {
				$Datasets = array();
			}
		}
		
		if (is_array($Datasets) && is_object($this->DB)) {
			foreach ($Datasets AS $DatasetID) {
				$DatasetFile = NITRO_ROOT.'/Tests/TestData/DB/'.$TableName.'/Dataset_'.$DatasetID.'.inc.php';
				$DatasetLoadFile = NITRO_ROOT.'/Tests/TestData/DB/'.$TableName.'/Dataset_'.$DatasetID.'.sql.load';
				if (file_exists($DatasetFile)) {
					$Query = '';
					include $DatasetFile;
					if (strlen($Query)) {
						$Result = $this->DB->query($Query);
						if (DB::isError($Result)) { echo 'Dataset_'.$DatasetID.' Query failed: '.$Query; exit; } 
					}
				} else if (file_exists($DatasetLoadFile)) {
					$Result = $this->DB->query('LOAD DATA INFILE "'.$DatasetLoadFile.'" INTO TABLE `'.$TableName.'` FIELDS TERMINATED BY \',\' OPTIONALLY ENCLOSED BY \'"\'');
					if (DB::isError($Result)) { echo 'Dataset_'.$DatasetID.' Load Data failed: '.mysql_error(); exit; } 
				}
			}
		}
	}
	
	function CleanDatabaseTable($TableName, $Drop = TRUE)
	{
		$this->_checkDBConnection();

		if (strlen($TableName) && is_object($this->DB)) {
			if ($Drop) {
				$Query = "DROP TABLE IF EXISTS `".$TableName."`;";
			} else {
				$Query = "TRUNCATE TABLE IF EXISTS `".$TableName."`;";
			}
			$this->DB->query($Query);
			if (DB::isError($Result)) { echo 'Cleanup query failed: '.$Query; exit; } 
			
			$return_value = TRUE;
		} else {
			$return_value = FALSE;
		}
		
		return $return_value;
	}
	
	function CleanDatabaseTables($Drop = TRUE)
	{
		if (is_array($this->PreparedTables)) {
			foreach ($this->PreparedTables AS $TableName) {
				$this->CleanDatabaseTable($TableName, $Drop);
			}
			$return_value = TRUE;
		} else {
			$return_value = FALSE;
		}
		
		$this->PreparedTables = array();
		
		return $return_value;
	}

	function _checkDBConnection()
	{
		if (!is_object($this->DB)) {
			echo "Test DB connection not available!";
			exit;
		}
	}
}
?>
Return current item: OpenNitro