<commit_before><?php
/**
 * @package test::lib
 */

require_once 'OverrideController.php';
require_once 'TestObserver.php';

/**
 * BaseTestClass is an abstract class that can be inherited by all test classes
 * which will set up the framework, include paths and such as well as provide
 * various utility functions for easier testing
 */
abstract class BaseTestClass extends PHPUnit_Framework_TestCase
{
	/** @property OverrideController */
	private $overrideController;

	/** @property Controller */
	private $controller;

	private $_preserved_session = Array();
	private $_printLineCount = 0;
	
	/**
	 * Anything fired in the constructor will not appear in the code coverage report
	 */
	final function __construct()
	{

	}
	
	/**
	 * Returns a dummy controller object that can be used to fire private methods
	 * such as setting the current user
	 */
	protected function GetOverrideController()
	{
		if (!$this->overrideController) {
			
			$gc = GlobalConfig::GetInstance();
			
			// override controller to let us access protected controller methods
			$this->overrideController = new OverrideController(
					$gc->GetPhreezer(),
					$gc->GetRenderEngine(),
					$gc->GetContext(),
					new MockRouter()  // swap out generic router for a mock
			);
		}
		
		return $this->overrideController;
	}
	
	/**
	 * Assign a value to the render engine
	 */
	protected function Assign($key,$value)
	{
		$re = GlobalConfig::GetInstance()->GetRenderEngine();
		$re->assign($key,$value);
	}
	
	/**
	 * Reset the controller so that all data in the input and output
	 * buffers are cleared, any compiled template files are removed
	 * and (optionally) the user session is destroyed
	 * 
	 * @param bool $clearSession (default = true)
	 */
	protected function Reset($clearSession = true)
	{
		$this->SetUrl('');
		$this->SetRequestBody('');
		$this->ClearOutput();
		$this->ClearVars();
		$this->RemoveCompiledTemplateFiles();
		
		$re = GlobalConfig::GetInstance()->GetRenderEngine();
		
		if ($re) {
			$re->clearAll();
			$re->assign("ROOT_URL",GlobalConfig::$ROOT_URL);
			$re->assign("PHREEZE_VERSION",Phreezer::$Version);
			$re->assign("PHREEZE_PHAR",Phreezer::PharPath());
		}
		
		if ($clearSession) $this->ClearCurrentUser();
	}

	/**
	 * Initializes the controller to be tested and clears all previous
	 * authentication as well as output variable assignments
	 *
	 * @param string name of controller class (ex AccountController)
	 * @param bool true to clear all authentication
	 */
	function InitController($classname, $clearAuth = true)
	{
		$gc = GlobalConfig::GetInstance();
		eval('$this->controller = new '.$classname.'($gc->GetPhreezer(), $gc->GetRenderEngine(), $gc->GetContext(), $gc->GetRouter() );');

		$this->controller->UnitTestMode = true;
		$this->controller->CaptureOutputMode = true;

		// clear all previous input
		$this->ClearVars();

		// remove any authentication that was hanging around
		if ($clearAuth) $this->ClearCurrentUser();

		// get rid of any feedback or warnings
		$re = $gc->GetRenderEngine();
		$re->clear("warning");
		$re->clear("feedback");
		$this->GetOverrideController()->OverrideSetContext("feedback","");

	}
	

	/**
	 * Removes all of the compiled template files that have been
	 * created by smarty.  existing template files can cause
	 * test to return inaccurate results due to cached data.
	 * Any files created while testing can cause the web app
	 * to crash due to incorrect ownership/permission of the files
	 */
	protected function RemoveCompiledTemplateFiles()
	{
		$path = GlobalConfig::$TEMPLATE_CACHE_PATH;
		
		if (!$path) return; // if not configured then this setup probably has no template cache
		
		$dir = opendir($path);

		while ( false !== ($file = readdir($dir)) )
		{
			if (substr($file,-7) == "tpl.php")
			{
				@chmod($path.$file, 0777);
				@unlink($path.$file);
			}
		}

		closedir($dir);
		
		// $this->Println("Compiled template files removed.",'BaseTestClass.RemoveCompiledTemplateFiles');
	}

	/**<commit_msg>add convenience method to base test to GetPhreezer<commit_after><?php
/**
 * @package test::lib
 */

require_once 'OverrideController.php';
require_once 'TestObserver.php';

/**
 * BaseTestClass is an abstract class that can be inherited by all test classes
 * which will set up the framework, include paths and such as well as provide
 * various utility functions for easier testing
 */
abstract class BaseTestClass extends PHPUnit_Framework_TestCase
{
	/** @property OverrideController */
	private $overrideController;

	/** @property Controller */
	private $controller;

	private $_preserved_session = Array();
	private $_printLineCount = 0;
	
	/**
	 * Anything fired in the constructor will not appear in the code coverage report
	 */
	final function __construct()
	{

	}
	
	/**
	 * Returns a dummy controller object that can be used to fire private methods
	 * such as setting the current user
	 */
	protected function GetOverrideController()
	{
		if (!$this->overrideController) {
			
			$gc = GlobalConfig::GetInstance();
			
			// override controller to let us access protected controller methods
			$this->overrideController = new OverrideController(
					$gc->GetPhreezer(),
					$gc->GetRenderEngine(),
					$gc->GetContext(),
					new MockRouter()  // swap out generic router for a mock
			);
		}
		
		return $this->overrideController;
	}
	
	/**
	 * Assign a value to the render engine
	 */
	protected function Assign($key,$value)
	{
		$re = GlobalConfig::GetInstance()->GetRenderEngine();
		$re->assign($key,$value);
	}
	
	/**
	 * Reset the controller so that all data in the input and output
	 * buffers are cleared, any compiled template files are removed
	 * and (optionally) the user session is destroyed
	 * 
	 * @param bool $clearSession (default = true)
	 */
	protected function Reset($clearSession = true)
	{
		$this->SetUrl('');
		$this->SetRequestBody('');
		$this->ClearOutput();
		$this->ClearVars();
		$this->RemoveCompiledTemplateFiles();
		
		$re = GlobalConfig::GetInstance()->GetRenderEngine();
		
		if ($re) {
			$re->clearAll();
			$re->assign("ROOT_URL",GlobalConfig::$ROOT_URL);
			$re->assign("PHREEZE_VERSION",Phreezer::$Version);
			$re->assign("PHREEZE_PHAR",Phreezer::PharPath());
		}
		
		if ($clearSession) $this->ClearCurrentUser();
	}

	/**
	 * Initializes the controller to be tested and clears all previous
	 * authentication as well as output variable assignments
	 *
	 * @param string name of controller class (ex AccountController)
	 * @param bool true to clear all authentication
	 */
	function InitController($classname, $clearAuth = true)
	{
		$gc = GlobalConfig::GetInstance();
		eval('$this->controller = new '.$classname.'($gc->GetPhreezer(), $gc->GetRenderEngine(), $gc->GetContext(), $gc->GetRouter() );');

		$this->controller->UnitTestMode = true;
		$this->controller->CaptureOutputMode = true;

		// clear all previous input
		$this->ClearVars();

		// remove any authentication that was hanging around
		if ($clearAuth) $this->ClearCurrentUser();

		// get rid of any feedback or warnings
		$re = $gc->GetRenderEngine();
		$re->clear("warning");
		$re->clear("feedback");
		$this->GetOverrideController()->OverrideSetContext("feedback","");

	}
	
	/**
	 * Convenience method to return the global Phreezer object
	 * @return Phreezer
	 */
	function GetPhreezer()
	{
		return GlobalConfig::GetInstance()->GetPhreezer();
	}

	/**
	 * Removes all of the compiled template files that have been
	 * created by smarty.  existing template files can cause
	 * test to return inaccurate results due to cached data.
	 * Any files created while testing can cause the web app
	 * to crash due to incorrect ownership/permission of the files
	 */
	protected function RemoveCompiledTemplateFiles()
	{
		$path = GlobalConfig::$TEMPLATE_CACHE_PATH;
		
		if (!$path) return; // if not configured then this setup probably has no template cache
		
		$dir = opendir($path);

		while ( false !== ($file = readdir($dir)) )
		{
			if (substr($file,-7) == "tpl.php")
			{
				@chmod($path.$file, 0777);
				@unlink($path.$file);
			}
		}

		closedir($dir);
		
		// $this->Println("Compiled template files removed.",'BaseTestClass.RemoveCompiledTemplateFiles');
	}

	/**
{'fraction_non_alphanumeric': 0.1319097010372178, 'fraction_numerical': 0.0014643075045759609, 'mean_word_length': 3.970285021224985, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 76, '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': '2365124', 'n_tokens_mistral': 2601, 'n_tokens_neox': 2287, 'n_words': 721}