Location: PHPKode > projects > OpenNitro > trunk/Nitro/Libraries/PHPUnit/Nitro_TestSuite.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: Nitro_TestSuite                                                  |
// +---------------------------------------------------------------------------+
// | 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_TestSuite.inc.php 229 2008-04-17 09:20:31Z oli $
//

/**
 * This file contains the Nitro_TestSuite class
 * 
 * @author			Jesper Avot <hide@address.com>
 * @copyright		2006 June Systems BV
 * @package			Classes
 * @subpackage	Nitro_TestSuite
 */

/**
 * Include required files
 */
include_once 'Nitro/Libraries/PHPUnit/UnitTest.inc.php';

/**
 * Nitro TestSuite class
 */
class Nitro_TestSuite {
	var $Result = array();
	var $Settings = array();
	
	function Nitro_TestSuite()
	{
		$this->Settings = $_REQUEST; //Dirty! But does what we want.
	}
	
	function ProcessTests()
	{
		$this->Result['Test'] = array();
		if (isset($this->Settings['File']) && strlen($this->Settings['File'])) {
			$this->Result['Test'][NITRO_ROOT.$this->Settings['File']] =& $this->_CalcTest(NITRO_ROOT.$this->Settings['File']);
		}
	}
	
	function &_CalcTest($File)
	{		
		$RV = new TestResult();
		
		if (is_object($Suite = UnitTest::CreateSuite($File))) {
			ob_start();
			$Suite->run($RV);
			ob_end_clean();
		}
		
		return $RV;
	}
	
	function Test()
	{
		$ReturnValue['SCRIPT:()'] = '';
		if (is_array($this->Result['Test'])) {
			foreach ($this->Result['Test'] AS $File => $Calc) { 
				$Code = sha1($File);
				$failureCount = 0;
				$warningCount = 0;
				$indexWarningCount = 0;
				$failureMessages = array();
				$warningMessages = array();
				$indexWarningsMessages = array();
				if ($Calc->failureCount()) {
					foreach ($Calc->GetFailures() AS $failure) {
						$exception		= $failure->GetException();	
						$errorMessage	= 'TESTED FUNCTION: '.$failure->GetTestName().'<br />TEST OUTPUT:';
						$errorMessage.= $exception->GetMessage();
						$errorMessage = str_replace('\t', '<br />', $errorMessage);
						$errorMessage = str_replace('\n', '<br />', $errorMessage);
						$errorMessage = str_replace('<code class="expected">', 'Expected:<br /><code class="expected">', $errorMessage);
						$errorMessage = str_replace('<code class="actual">', '<br />Actual:<br /><code class="actual">', $errorMessage);
	
						if (preg_match('/PHP ERROR.*Error: Undefined index(.*)/', $errorMessage, $matches)) {
							$indexWarningsMessages[] = $errorMessage;
							$indexWarningCount++;
						} else if (!preg_match('/PHP ERROR.*Error: Undefined (index|offset|variable)(.*)/', $errorMessage, $matches) && !preg_match('/PHP ERROR.*Error: (fsockopen)(.*)/', $errorMessage)) {
							$failureMessages[] = $errorMessage;
							$failureCount++;
						} else {
							$warningMessages[] = $errorMessage;
							$warningCount++;
						}
					}
				}
				$failureMessages = implode('<br /><br /><hr><br />', $failureMessages);
				$warningMessages = implode('<br /><br /><hr><br />', $warningMessages);
				$indexWarningsMessages = implode('<br /><br /><hr><br />', $indexWarningsMessages);

				$text = '<div style="background-color: '.($failureCount > 0 ? 'red' : ($warningCount > 0 ? 'orange' : 'green')).'">';
				$text.= '<span style="color: '.($failureCount > 0 ? '#000000' : '#ffffff').';">';
				$text.= 'Tested:&nbsp;'.$Calc->CountTests().', ';
				$text.= '<span'.(strlen($indexWarningsMessages) ? ' onclick="ShowTestMessage(\''.rawurlencode($indexWarningsMessages).'\');"' : '').'>Undefined Indices:&nbsp;'.$indexWarningCount.'</span>, ';
				$text.= '<span'.(strlen($warningMessages) ? ' onclick="ShowTestMessage(\''.rawurlencode($warningMessages).'\');"' : '').'>Warnings:&nbsp;'.$warningCount.'</span>, ';
				$text.= '<span'.(strlen($failureMessages) ? ' onclick="ShowTestMessage(\''.rawurlencode($failureMessages).'\');"' : '').'>Failed:&nbsp;'.$failureCount.'</span>';
				$text.= '</span></div>';

				$ReturnValue['Tests_'.$Code.'_Result'] = $text;
			}
		} else {
			$ReturnValue['MESSAGE'] = 'Please select one or more files to test!';
		}
		return $ReturnValue;
	}
	
	function GetTestFilesOverview()
	{
		$testFiles = UnitTest::GetTestFilesFromDir(NITRO_ROOT.'/Tests/Nitro/', TRUE);
		$testFiles = $this->FilterTestFiles($testFiles);

		$Template = new NitroTemplate(NULL);
		$Template->Assign('testFiles', $testFiles);
		$HTML = $Template->Fetch("file:".NITRO_PATH."Defaults/Templates/TestSuiteList.tpl");

		return $HTML;
	}
	
	function FilterTestFiles($testFiles)
	{
		$filteredTestFiles = array();
		if (isset($this->Settings['Filter']) && strlen($this->Settings['Filter'])) {
			$Filters = explode(' ', $this->Settings['Filter']);
			foreach ($testFiles AS $testFile) {
				$showFile = str_replace(NITRO_ROOT.'/Tests/Nitro//', '', $testFile);
				$exclude = false;
				foreach($Filters AS $filter) {
					if (strpos($filter, '-') === 0) {
						// negate
						$filter = substr($filter, 1, strlen($filter));
						if (strstr(strtolower($showFile), strtolower($filter))) {
							$exclude = true;
							break; //stop searching
						}
					} elseif (!strstr(strtolower($showFile), strtolower($filter))) {
						$exclude = true;
						break; //stop searching
					}
				}
				if (!$exclude) $filteredTestFiles[] = $testFile;
			}
		} else {
			$filteredTestFiles = $testFiles;
		}
		return $filteredTestFiles;
	}
	
	function PrintSettings()
	{
		print_r($this->Settings); exit;
	}
}
?>
Return current item: OpenNitro