Modules

Kohana_Unittest_Helpers

This class is a transparent base class for Unittest_Helpers and should not be accessed directly.

Unit testing helpers

Class declared in MODPATH/unittest/classes/Kohana/Unittest/Helpers.php on line 6.

Properties

protected array $_environment_backup

Backup of the environment variables

Default value:
array(0) 

protected static boolean $_has_internet

Static variable used to work out whether we have an internet connection

NULL

Methods

public static clean_cache_dir( ) (defined in Kohana_Unittest_Helpers)

Removes all cache files from the kohana cache dir

Return Values

  • void

Source Code

static public function clean_cache_dir()
{
	$cache_dir = opendir(Kohana::$cache_dir);

	while ($dir = readdir($cache_dir))
	{
		// Cache files are split into directories based on first two characters of hash
		if ($dir[0] !== '.' AND strlen($dir) === 2)
		{
			$dir = self::dir_separator(Kohana::$cache_dir.'/'.$dir.'/');

			$cache = opendir($dir);

			while ($file = readdir($cache))
			{
				if ($file[0] !== '.')
				{
					unlink($dir.$file);
				}
			}

			closedir($cache);

			rmdir($dir);
		}
	}

	closedir($cache_dir);
}

public static dir_separator( string $path ) (defined in Kohana_Unittest_Helpers)

Helper function which replaces the "/" to OS-specific delimiter

Parameters

  • string $path required - $path

Return Values

  • string

Source Code

static public function dir_separator($path)
{
	return str_replace('/', DIRECTORY_SEPARATOR, $path);
}

public static has_internet( ) (defined in Kohana_Unittest_Helpers)

Check for internet connectivity

Return Values

  • boolean - Whether an internet connection is available

Source Code

public static function has_internet()
{
	if ( ! isset(self::$_has_internet))
	{
		// The @ operator is used here to avoid DNS errors when there is no connection.
		$sock = @fsockopen("www.google.com", 80, $errno, $errstr, 1);

		self::$_has_internet = (bool) $sock ? TRUE : FALSE;
	}

	return self::$_has_internet;
}

public restore_environment( ) (defined in Kohana_Unittest_Helpers)

Restores the environment to the original state

Tags

  • Chainable -

Return Values

  • Kohana_Unittest_Helpers - $this

Source Code

public function restore_environment()
{
	$this->set_environment($this->_environment_backup);	
}

public set_environment( array $environment ) (defined in Kohana_Unittest_Helpers)

Allows easy setting & backing up of enviroment config

Option types are checked in the following order:

  • Server Var
  • Static Variable
  • Config option

Parameters

  • array $environment required - List of environment to set

Source Code

public function set_environment(array $environment)
{
	if ( ! count($environment))
		return FALSE;

	foreach ($environment as $option => $value)
	{
		$backup_needed = ! array_key_exists($option, $this->_environment_backup);

		// Handle changing superglobals
		if (in_array($option, array('_GET', '_POST', '_SERVER', '_FILES')))
		{
			// For some reason we need to do this in order to change the superglobals
			global $$option;

			if ($backup_needed)
			{
				$this->_environment_backup[$option] = $$option;
			}

			// PHPUnit makes a backup of superglobals automatically
			$$option = $value;
		}
		// If this is a static property i.e. Html::$windowed_urls
		elseif (strpos($option, '::$') !== FALSE)
		{
			list($class, $var) = explode('::$', $option, 2);

			$class = new ReflectionClass($class);

			if ($backup_needed)
			{
				$this->_environment_backup[$option] = $class->getStaticPropertyValue($var);
			}

			$class->setStaticPropertyValue($var, $value);
		}
		// If this is an environment variable
		elseif (preg_match('/^[A-Z_-]+$/', $option) OR isset($_SERVER[$option]))
		{
			if ($backup_needed)
			{
				$this->_environment_backup[$option] = isset($_SERVER[$option]) ? $_SERVER[$option] : '';
			}
			
			$_SERVER[$option] = $value;
		}
		// Else we assume this is a config option
		else
		{
			if ($backup_needed)
			{
				$this->_environment_backup[$option] = Kohana::$config->load($option);
			}

			list($group, $var) = explode('.', $option, 2);

			Kohana::$config->load($group)->set($var, $value);
		}
	}
}