Added Kohana v3.0.8
This commit is contained in:
359
includes/kohana/modules/unittest/classes/controller/unittest.php
Normal file
359
includes/kohana/modules/unittest/classes/controller/unittest.php
Normal file
@@ -0,0 +1,359 @@
|
||||
<?php defined('SYSPATH') or die ('No direct script access.');
|
||||
/**
|
||||
* PHPUnit Kohana web based test runner
|
||||
*
|
||||
* @package Kohana/Unittest
|
||||
* @author Kohana Team
|
||||
* @author BRMatt <matthew@sigswitch.com>
|
||||
* @author Paul Banks
|
||||
* @copyright (c) 2008-2009 Kohana Team
|
||||
* @license http://kohanaphp.com/license
|
||||
*/
|
||||
|
||||
Class Controller_UnitTest extends Controller_Template
|
||||
{
|
||||
/**
|
||||
* Whether the archive module is available
|
||||
* @var boolean
|
||||
*/
|
||||
protected $cc_archive_available = FALSE;
|
||||
|
||||
/**
|
||||
* Unittest config
|
||||
* @var Kohana_Config
|
||||
*/
|
||||
protected $config = NULL;
|
||||
|
||||
/**
|
||||
* The uri by which the report uri will be executed
|
||||
* @var string
|
||||
*/
|
||||
protected $report_uri = '';
|
||||
|
||||
/**
|
||||
* The uri by which the run action will be executed
|
||||
* @var string
|
||||
*/
|
||||
protected $run_uri = '';
|
||||
|
||||
/**
|
||||
* Is the XDEBUG extension loaded?
|
||||
* @var boolean
|
||||
*/
|
||||
protected $xdebug_loaded = FALSE;
|
||||
|
||||
/**
|
||||
* Template
|
||||
* @var string
|
||||
*/
|
||||
public $template = 'unittest/layout';
|
||||
|
||||
/**
|
||||
* Loads test suite
|
||||
*/
|
||||
public function before()
|
||||
{
|
||||
parent::before();
|
||||
|
||||
if ( ! Kohana_Tests::enabled())
|
||||
{
|
||||
// Pretend this is a normal 404 error...
|
||||
$this->status = 404;
|
||||
|
||||
throw new Kohana_Request_Exception('Unable to find a route to match the URI: :uri',
|
||||
array(':uri' => $this->request->uri));
|
||||
}
|
||||
|
||||
// Prevent the whitelist from being autoloaded, but allow the blacklist
|
||||
// to be loaded
|
||||
Kohana_Tests::configure_environment(FALSE);
|
||||
|
||||
$this->config = Kohana::config('unittest');
|
||||
|
||||
// This just stops some very very long lines
|
||||
$route = Route::get('unittest');
|
||||
$this->report_uri = $route->uri(array('action' => 'report'));
|
||||
$this->run_uri = $route->uri(array('action' => 'run'));
|
||||
|
||||
// Switch used to disable cc settings
|
||||
$this->xdebug_loaded = extension_loaded('xdebug');
|
||||
$this->cc_archive_enabled = class_exists('Archive');
|
||||
|
||||
Kohana_View::set_global('xdebug_enabled', $this->xdebug_loaded);
|
||||
Kohana_View::set_global('cc_archive_enabled', $this->cc_archive_enabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles index page for /unittest/ and /unittest/index/
|
||||
*/
|
||||
public function action_index()
|
||||
{
|
||||
$this->template->body = View::factory('unittest/index')
|
||||
->set('run_uri', $this->run_uri)
|
||||
->set('report_uri', $this->report_uri)
|
||||
->set('whitelistable_items', $this->get_whitelistable_items())
|
||||
->set('groups', $this->get_groups_list(Kohana_Tests::suite()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles report generation
|
||||
*/
|
||||
public function action_report()
|
||||
{
|
||||
// Fairly foolproof
|
||||
if ( ! $this->config->cc_report_path AND ! class_exists('Archive'))
|
||||
{
|
||||
throw new Kohana_Exception('Cannot generate report');
|
||||
}
|
||||
|
||||
// We don't want to use the HTML layout, we're sending the user 100111011100110010101100
|
||||
$this->auto_render = FALSE;
|
||||
|
||||
$suite = Kohana_Tests::suite();
|
||||
$temp_path = rtrim($this->config->temp_path, '/').'/';
|
||||
$group = (array) Arr::get($_GET, 'group', array());
|
||||
|
||||
// Stop unittest from interpretting "all groups" as "no groups"
|
||||
if (empty($group) OR empty($group[0]))
|
||||
{
|
||||
$group = array();
|
||||
}
|
||||
|
||||
if (Arr::get($_GET, 'use_whitelist', FALSE))
|
||||
{
|
||||
$this->whitelist(Arr::get($_GET, 'whitelist', array()));
|
||||
}
|
||||
|
||||
$runner = new Kohana_Unittest_Runner($suite);
|
||||
|
||||
// If the user wants to download a report
|
||||
if ($this->cc_archive_enabled AND Arr::get($_GET, 'archive') === '1')
|
||||
{
|
||||
// $report is the actual directory of the report,
|
||||
// $folder is the name component of directory
|
||||
list($report, $folder) = $runner->generate_report($group, $temp_path);
|
||||
|
||||
$archive = Archive::factory('zip');
|
||||
|
||||
// TODO: Include the test results?
|
||||
$archive->add($report, 'report', TRUE);
|
||||
|
||||
$filename = $folder.'.zip';
|
||||
|
||||
$archive->save($temp_path.$filename);
|
||||
|
||||
// It'd be nice to clear up afterwards but by deleting the report dir we corrupt the archive
|
||||
// And once the archive has been sent to the user Request stops the script so we can't delete anything
|
||||
// It'll be up to the user to delete files periodically
|
||||
$this->request->send_file($temp_path.$filename, $filename);
|
||||
}
|
||||
else
|
||||
{
|
||||
$folder = trim($this->config->cc_report_path, '/').'/';
|
||||
$path = DOCROOT.$folder;
|
||||
|
||||
if ( ! file_exists($path))
|
||||
{
|
||||
throw new Kohana_Exception('Report directory :dir does not exist', array(':dir' => $path));
|
||||
}
|
||||
|
||||
if ( ! is_writable($path))
|
||||
{
|
||||
throw new Kohana_Exception('Script doesn\'t have permission to write to report dir :dir ', array(':dir' => $path));
|
||||
}
|
||||
|
||||
$runner->generate_report($group, $path, FALSE);
|
||||
|
||||
$this->request->redirect(URL::base(FALSE, TRUE).$folder.'index.html');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles test running interface
|
||||
*/
|
||||
public function action_run()
|
||||
{
|
||||
$this->template->body = View::factory('unittest/results');
|
||||
|
||||
// Get the test suite and work out which groups we're testing
|
||||
$suite = Kohana_Tests::suite();
|
||||
$group = (array) Arr::get($_GET, 'group', array());
|
||||
|
||||
|
||||
// Stop phpunit from interpretting "all groups" as "no groups"
|
||||
if (empty($group) OR empty($group[0]))
|
||||
{
|
||||
$group = array();
|
||||
}
|
||||
|
||||
// Only collect code coverage if the user asked for it
|
||||
$collect_cc = (bool) Arr::get($_GET, 'collect_cc', FALSE);
|
||||
|
||||
if ($collect_cc AND Arr::get($_GET, 'use_whitelist', FALSE))
|
||||
{
|
||||
$whitelist = $this->whitelist(Arr::get($_GET, 'whitelist', array()));
|
||||
}
|
||||
|
||||
$runner = new Kohana_Unittest_Runner($suite);
|
||||
|
||||
try
|
||||
{
|
||||
$runner->run($group, $collect_cc);
|
||||
|
||||
if ($collect_cc)
|
||||
{
|
||||
$this->template->body->set('coverage', $runner->calculate_cc_percentage());
|
||||
}
|
||||
|
||||
if (isset($whitelist))
|
||||
{
|
||||
$this->template->body->set('coverage_explanation', $this->nice_whitelist_explanation($whitelist));
|
||||
}
|
||||
}
|
||||
catch(Kohana_Exception $e)
|
||||
{
|
||||
// Code coverage is not allowed, possibly xdebug disabled?
|
||||
// TODO: Tell the user this?
|
||||
$runner->run($group);
|
||||
}
|
||||
|
||||
// Show some results
|
||||
$this->template->body
|
||||
->set('results', $runner->results)
|
||||
->set('totals', $runner->totals)
|
||||
->set('time', $this->nice_time($runner->time))
|
||||
|
||||
// Sets group to the currently selected group, or default all groups
|
||||
->set('group', Arr::get($this->get_groups_list($suite), reset($group), 'All groups'))
|
||||
->set('groups', $this->get_groups_list($suite))
|
||||
|
||||
->set('report_uri', $this->report_uri.url::query())
|
||||
|
||||
// Whitelist related stuff
|
||||
->set('whitelistable_items', $this->get_whitelistable_items())
|
||||
->set('whitelisted_items', isset($whitelist) ? array_keys($whitelist) : array())
|
||||
->set('whitelist', ! empty($whitelist));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of groups from the test suite, sorted with 'All groups' prefixed
|
||||
*
|
||||
* @return array Array of groups in the test suite
|
||||
*/
|
||||
protected function get_groups_list($suite)
|
||||
{
|
||||
// Make groups aray suitable for drop down
|
||||
$groups = $suite->getGroups();
|
||||
if (count($groups) > 0)
|
||||
{
|
||||
sort($groups);
|
||||
$groups = array_combine($groups, $groups);
|
||||
}
|
||||
return array('' => 'All Groups') + $groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of items that are whitelistable
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_whitelistable_items()
|
||||
{
|
||||
static $whitelist;
|
||||
|
||||
if (count($whitelist))
|
||||
{
|
||||
return $whitelist;
|
||||
}
|
||||
|
||||
$whitelist = array();
|
||||
|
||||
$whitelist['k_app'] = 'Application';
|
||||
|
||||
$k_modules = array_keys(Kohana::modules());
|
||||
|
||||
$whitelist += array_map('ucfirst', array_combine($k_modules, $k_modules));
|
||||
|
||||
$whitelist['k_sys'] = 'Kohana Core';
|
||||
|
||||
return $whitelist;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whitelists a specified set of modules specified by the user
|
||||
*
|
||||
* @param array $modules
|
||||
*/
|
||||
protected function whitelist(array $modules)
|
||||
{
|
||||
$k_modules = Kohana::modules();
|
||||
$whitelist = array();
|
||||
|
||||
// Make sure our whitelist is valid
|
||||
foreach ($modules as $item)
|
||||
{
|
||||
if (isset($k_modules[$item]))
|
||||
{
|
||||
$whitelist[$item] = $k_modules[$item];
|
||||
}
|
||||
elseif ($item === 'k_app')
|
||||
{
|
||||
$whitelist[$item] = APPPATH;
|
||||
}
|
||||
elseif ($item === 'k_sys')
|
||||
{
|
||||
$whitelist[$item] = SYSPATH;
|
||||
}
|
||||
}
|
||||
|
||||
if (count($whitelist))
|
||||
{
|
||||
Kohana_Tests::whitelist($whitelist);
|
||||
}
|
||||
|
||||
return $whitelist;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prettifies the list of whitelisted modules
|
||||
*
|
||||
* @param array Array of whitelisted items
|
||||
* @return string
|
||||
*/
|
||||
protected function nice_whitelist_explanation(array $whitelist)
|
||||
{
|
||||
$items = array_intersect_key($this->get_whitelistable_items(), $whitelist);
|
||||
|
||||
return implode(', ', $items);
|
||||
}
|
||||
|
||||
protected function nice_time($time)
|
||||
{
|
||||
$parts = array();
|
||||
|
||||
if ($time > DATE::DAY)
|
||||
{
|
||||
$parts[] = floor($time/DATE::DAY).'d';
|
||||
$time = $time % DATE::DAY;
|
||||
}
|
||||
|
||||
if ($time > DATE::HOUR)
|
||||
{
|
||||
$parts[] = floor($time/DATE::HOUR).'h';
|
||||
$time = $time % DATE::HOUR;
|
||||
}
|
||||
|
||||
if ($time > DATE::MINUTE)
|
||||
{
|
||||
$parts[] = floor($time/DATE::MINUTE).'m';
|
||||
$time = $time % DATE::MINUTE;
|
||||
}
|
||||
|
||||
if ($time > 0)
|
||||
{
|
||||
$parts[] = round($time, 1).'s';
|
||||
}
|
||||
|
||||
return implode(' ', $parts);
|
||||
}
|
||||
} // End Controller_PHPUnit
|
263
includes/kohana/modules/unittest/classes/kohana/tests.php
Normal file
263
includes/kohana/modules/unittest/classes/kohana/tests.php
Normal file
@@ -0,0 +1,263 @@
|
||||
<?php
|
||||
/**
|
||||
* PHPUnit testsuite for kohana application
|
||||
*
|
||||
* @package Kohana/Unittest
|
||||
* @author Kohana Team
|
||||
* @author BRMatt <matthew@sigswitch.com>
|
||||
* @author Paul Banks
|
||||
* @copyright (c) 2008-2009 Kohana Team
|
||||
* @license http://kohanaphp.com/license
|
||||
*/
|
||||
class Kohana_Tests
|
||||
{
|
||||
static protected $cache = array();
|
||||
|
||||
/**
|
||||
* Loads test files if they cannot be found by kohana
|
||||
* @param <type> $class
|
||||
*/
|
||||
static function autoload($class)
|
||||
{
|
||||
$file = str_replace('_', '/', $class);
|
||||
|
||||
if ($file = Kohana::find_file('tests', $file))
|
||||
{
|
||||
require_once $file;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the environment for testing
|
||||
*
|
||||
* Does the following:
|
||||
*
|
||||
* * Loads the phpunit framework (for the web ui)
|
||||
* * Restores exception phpunit error handlers (for cli)
|
||||
* * registeres an autoloader to load test files
|
||||
*/
|
||||
static public function configure_environment($do_whitelist = TRUE, $do_blacklist = TRUE)
|
||||
{
|
||||
if ( ! class_exists('PHPUnit_Util_Filter', FALSE))
|
||||
{
|
||||
// Make sure the PHPUnit classes are available
|
||||
require_once 'PHPUnit/Framework.php';
|
||||
}
|
||||
|
||||
if (Kohana::$is_cli)
|
||||
{
|
||||
restore_exception_handler();
|
||||
restore_error_handler();
|
||||
}
|
||||
|
||||
spl_autoload_register(array('Kohana_Tests', 'autoload'));
|
||||
|
||||
Kohana_Tests::$cache = ($cache = Kohana::cache('unittest_whitelist_cache')) === NULL ? array() : $cache;
|
||||
|
||||
$config = Kohana::config('unittest');
|
||||
|
||||
if ($do_whitelist AND $config->use_whitelist)
|
||||
{
|
||||
self::whitelist();
|
||||
}
|
||||
|
||||
if ($do_blacklist AND count($config['blacklist']))
|
||||
{
|
||||
foreach ($config->blacklist as $item)
|
||||
{
|
||||
if (is_dir($item))
|
||||
{
|
||||
PHPUnit_Util_Filter::addDirectoryToFilter($item);
|
||||
}
|
||||
else
|
||||
{
|
||||
PHPUnit_Util_Filter::addFileToFilter($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to see if unittest is enabled in the config
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
static function enabled()
|
||||
{
|
||||
$p_environment = Kohana::config('unittest.environment');
|
||||
$k_environment = Kohana::$environment;
|
||||
|
||||
return (is_array($p_environment) AND in_array($k_environment, $p_environment))
|
||||
OR
|
||||
($k_environment === $p_environment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the test suite for kohana
|
||||
*
|
||||
* @return PHPUnit_Framework_TestSuite
|
||||
*/
|
||||
static function suite()
|
||||
{
|
||||
static $suite = NULL;
|
||||
|
||||
if ($suite instanceof PHPUnit_Framework_TestSuite)
|
||||
{
|
||||
return $suite;
|
||||
}
|
||||
|
||||
$files = Kohana::list_files('tests');
|
||||
|
||||
$suite = new PHPUnit_Framework_TestSuite();
|
||||
|
||||
self::addTests($suite, $files);
|
||||
|
||||
return $suite;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add files to test suite $suite
|
||||
*
|
||||
* Uses recursion to scan subdirectories
|
||||
*
|
||||
* @param PHPUnit_Framework_TestSuite $suite The test suite to add to
|
||||
* @param array $files Array of files to test
|
||||
*/
|
||||
static function addTests(PHPUnit_Framework_TestSuite $suite, array $files)
|
||||
{
|
||||
foreach ($files as $file)
|
||||
{
|
||||
if (is_array($file))
|
||||
{
|
||||
self::addTests($suite, $file);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Make sure we only include php files
|
||||
if (is_file($file) AND substr($file, -strlen(EXT)) === EXT)
|
||||
{
|
||||
// The default PHPUnit TestCase extension
|
||||
if ( ! strpos($file, 'TestCase'.EXT))
|
||||
{
|
||||
$suite->addTestFile($file);
|
||||
}
|
||||
else
|
||||
{
|
||||
require_once($file);
|
||||
}
|
||||
|
||||
PHPUnit_Util_Filter::addFileToFilter($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the whitelist
|
||||
*
|
||||
* If no directories are provided then the function'll load the whitelist
|
||||
* set in the config file
|
||||
*
|
||||
* @param array $directories Optional directories to whitelist
|
||||
*/
|
||||
static public function whitelist(array $directories = NULL)
|
||||
{
|
||||
if (empty($directories))
|
||||
{
|
||||
$directories = self::get_config_whitelist();
|
||||
}
|
||||
|
||||
if (count($directories))
|
||||
{
|
||||
foreach ($directories as &$directory)
|
||||
{
|
||||
$directory = realpath($directory).'/';
|
||||
}
|
||||
|
||||
// Only whitelist the "top" files in the cascading filesystem
|
||||
self::set_whitelist(Kohana::list_files('classes', $directories));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Works out the whitelist from the config
|
||||
* Used only on the CLI
|
||||
*
|
||||
* @returns array Array of directories to whitelist
|
||||
*/
|
||||
static protected function get_config_whitelist()
|
||||
{
|
||||
$config = Kohana::config('unittest');
|
||||
$directories = array();
|
||||
|
||||
if ($config->whitelist['app'])
|
||||
{
|
||||
$directories['k_app'] = APPPATH;
|
||||
}
|
||||
|
||||
if ($modules = $config->whitelist['modules'])
|
||||
{
|
||||
$k_modules = Kohana::modules();
|
||||
|
||||
// Have to do this because kohana merges config...
|
||||
// If you want to include all modules & override defaults then TRUE must be the first
|
||||
// value in the modules array of your app/config/unittest file
|
||||
if (array_search(TRUE, $modules, TRUE) === (count($modules) - 1))
|
||||
{
|
||||
$modules = $k_modules;
|
||||
}
|
||||
elseif (array_search(FALSE, $modules, TRUE) === FALSE)
|
||||
{
|
||||
$modules = array_intersect_key($k_modules, array_combine($modules, $modules));
|
||||
}
|
||||
else
|
||||
{
|
||||
// modules are disabled
|
||||
$modules = array();
|
||||
}
|
||||
|
||||
$directories += $modules;
|
||||
}
|
||||
|
||||
if ($config->whitelist['system'])
|
||||
{
|
||||
$directories['k_sys'] = SYSPATH;
|
||||
}
|
||||
|
||||
return $directories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively whitelists an array of files
|
||||
*
|
||||
* @param array $files Array of files to whitelist
|
||||
*/
|
||||
static protected function set_whitelist($files)
|
||||
{
|
||||
foreach ($files as $file)
|
||||
{
|
||||
if (is_array($file))
|
||||
{
|
||||
self::set_whitelist($file);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( ! isset(Kohana_Tests::$cache[$file]))
|
||||
{
|
||||
$relative_path = substr($file, strrpos($file, 'classes'.DIRECTORY_SEPARATOR) + 8, -strlen(EXT));
|
||||
$cascading_file = Kohana::find_file('classes', $relative_path);
|
||||
|
||||
// The theory is that if this file is the highest one in the cascading filesystem
|
||||
// then it's safe to whitelist
|
||||
Kohana_Tests::$cache[$file] = ($cascading_file === $file);
|
||||
}
|
||||
|
||||
if (Kohana_Tests::$cache[$file])
|
||||
{
|
||||
PHPUnit_Util_Filter::addFileToWhitelist($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit testing helpers
|
||||
*
|
||||
* @package Kohana/Unittest
|
||||
* @author Kohana Team
|
||||
* @author BRMatt <matthew@sigswitch.com>
|
||||
* @author Paul Banks
|
||||
* @copyright (c) 2008-2009 Kohana Team
|
||||
* @license http://kohanaphp.com/license
|
||||
*/
|
||||
class Kohana_Unittest_Helpers
|
||||
{
|
||||
/**
|
||||
* Static variable used to work out whether we have an internet
|
||||
* connection
|
||||
* @see has_internet
|
||||
* @var boolean
|
||||
*/
|
||||
static protected $_has_internet = NULL;
|
||||
|
||||
/**
|
||||
* Check for internet connectivity
|
||||
*
|
||||
* @return boolean Whether an internet connection is available
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function which replaces the "/" to OS-specific delimiter
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
static public function dir_separator($path)
|
||||
{
|
||||
return str_replace('/', DIRECTORY_SEPARATOR, $path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all cache files from the kohana cache dir
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup of the environment variables
|
||||
* @see set_environment
|
||||
* @var array
|
||||
*/
|
||||
protected $_environment_backup = array();
|
||||
|
||||
/**
|
||||
* Allows easy setting & backing up of enviroment config
|
||||
*
|
||||
* Option types are checked in the following order:
|
||||
*
|
||||
* * Server Var
|
||||
* * Static Variable
|
||||
* * Config option
|
||||
*
|
||||
* @param array $environment List of environment to set
|
||||
*/
|
||||
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($option);
|
||||
}
|
||||
|
||||
list($group, $var) = explode('.', $option, 2);
|
||||
|
||||
Kohana::config($group)->set($var, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restores the environment to the original state
|
||||
*
|
||||
* @chainable
|
||||
* @return Kohana_Unittest_Helpers $this
|
||||
*/
|
||||
public function restore_environment()
|
||||
{
|
||||
$this->set_environment($this->_environment_backup);
|
||||
}
|
||||
}
|
@@ -0,0 +1,304 @@
|
||||
<?php
|
||||
/**
|
||||
* PHPUnit test runner for kohana
|
||||
*
|
||||
* @package Kohana/Unittest
|
||||
* @author Kohana Team
|
||||
* @author BRMatt <matthew@sigswitch.com>
|
||||
* @author Paul Banks
|
||||
* @copyright (c) 2008-2009 Kohana Team
|
||||
* @license http://kohanaphp.com/license
|
||||
*/
|
||||
Class Kohana_Unittest_Runner implements PHPUnit_Framework_TestListener
|
||||
{
|
||||
/**
|
||||
* Results
|
||||
* @var array
|
||||
*/
|
||||
protected $results = array(
|
||||
'errors' => array(),
|
||||
'failures' => array(),
|
||||
'skipped' => array(),
|
||||
'incomplete' => array(),
|
||||
);
|
||||
|
||||
/**
|
||||
* Test result totals
|
||||
* @var array
|
||||
*/
|
||||
protected $totals = array(
|
||||
'tests' => 0,
|
||||
'passed' => 0,
|
||||
'errors' => 0,
|
||||
'failures' => 0,
|
||||
'skipped' => 0,
|
||||
'incomplete' => 0,
|
||||
'assertions' => 0,
|
||||
);
|
||||
|
||||
/**
|
||||
* Info about the current test running
|
||||
* @var array
|
||||
*/
|
||||
protected $current = array();
|
||||
|
||||
/**
|
||||
* Time for tests to run (seconds)
|
||||
* @var float
|
||||
*/
|
||||
protected $time = 0;
|
||||
|
||||
/**
|
||||
* Result collector
|
||||
* @var PHPUnit_Framework_TestResult
|
||||
*/
|
||||
protected $result = NULL;
|
||||
|
||||
/**
|
||||
* the test suite to run
|
||||
* @var PHPUnit_Framework_TestSuite
|
||||
*/
|
||||
protected $suite = NULL;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param PHPUnit_Framework_TestSuite $suite The suite to test
|
||||
* @param PHPUnit_Framework_TestResult $result Optional result object to use
|
||||
*/
|
||||
function __construct(PHPUnit_Framework_TestSuite $suite, PHPUnit_Framework_TestResult $result = NULL)
|
||||
{
|
||||
if ($result === NULL)
|
||||
{
|
||||
$result = new PHPUnit_Framework_TestResult;
|
||||
}
|
||||
|
||||
$result->addListener($this);
|
||||
|
||||
$this->suite = $suite;
|
||||
$this->result = $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic getter to allow access to member variables
|
||||
*
|
||||
* @param string $var Variable to get
|
||||
* @return mixed
|
||||
*/
|
||||
function __get($var)
|
||||
{
|
||||
return $this->$var;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calcualtes stats for each file covered by the code testing
|
||||
*
|
||||
* Each member of the returned array is formatted like so:
|
||||
*
|
||||
* <code>
|
||||
* array(
|
||||
* 'coverage' => $coverage_percent_for_file,
|
||||
* 'loc' => $lines_of_code,
|
||||
* 'locExecutable' => $lines_of_executable_code,
|
||||
* 'locExecuted' => $lines_of_code_executed
|
||||
* );
|
||||
* </code>
|
||||
*
|
||||
* @return array Statistics for code coverage of each file
|
||||
*/
|
||||
public function calculate_cc()
|
||||
{
|
||||
if ($this->result->getCollectCodeCoverageInformation())
|
||||
{
|
||||
$coverage = $this->result->getCodeCoverageInformation();
|
||||
|
||||
$coverage_summary = PHPUnit_Util_CodeCoverage::getSummary($coverage);
|
||||
|
||||
$stats = array();
|
||||
|
||||
foreach ($coverage_summary as $file => $_lines)
|
||||
{
|
||||
$stats[$file] = PHPUnit_Util_CodeCoverage::getStatistics($coverage_summary, $file);
|
||||
}
|
||||
|
||||
return $stats;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the percentage code coverage information
|
||||
*
|
||||
* @return boolean|float FALSE if cc is not enabled, float for coverage percent
|
||||
*/
|
||||
public function calculate_cc_percentage()
|
||||
{
|
||||
if ($stats = $this->calculate_cc())
|
||||
{
|
||||
$executable = 0;
|
||||
$executed = 0;
|
||||
|
||||
foreach ($stats as $stat)
|
||||
{
|
||||
$executable += $stat['locExecutable'];
|
||||
$executed += $stat['locExecuted'];
|
||||
}
|
||||
|
||||
return $executable > 0 ? ($executed / $executable) * 100 : 100;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a report using the specified $temp_path
|
||||
*
|
||||
* @param array $groups Groups to test
|
||||
* @param string $temp_path Temporary path to use while generating report
|
||||
*/
|
||||
public function generate_report(array $groups, $temp_path, $create_sub_dir = TRUE)
|
||||
{
|
||||
if ( ! is_writable($temp_path))
|
||||
{
|
||||
throw new Kohana_Exception('Temp path :path does not exist or is not writable by the webserver', array(':path' => $temp_path));
|
||||
}
|
||||
|
||||
$folder_path = $temp_path;
|
||||
|
||||
if ($create_sub_dir === TRUE)
|
||||
{
|
||||
// Icky, highly unlikely, but do it anyway
|
||||
// Basically adds "(n)" to the end of the filename until there's a free file
|
||||
$count = 0;
|
||||
do
|
||||
{
|
||||
$folder_name = date('Y-m-d_H:i:s')
|
||||
.( ! empty($groups) ? '['.implode(',', $groups).']' : '')
|
||||
.($count > 0 ? '('.$count.')' : '');
|
||||
++$count;
|
||||
}
|
||||
while(is_dir($folder_path.$folder_name));
|
||||
|
||||
$folder_path .= $folder_name;
|
||||
|
||||
mkdir($folder_path, 0777);
|
||||
}
|
||||
else
|
||||
{
|
||||
$folder_name = basename($folder_path);
|
||||
}
|
||||
|
||||
$this->run($groups, TRUE);
|
||||
|
||||
require_once 'PHPUnit/Runner/Version.php';
|
||||
require_once 'PHPUnit/Util/Report.php';
|
||||
|
||||
PHPUnit_Util_Report::render($this->result, $folder_path);
|
||||
|
||||
return array($folder_path, $folder_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the test suite using the result specified in the constructor
|
||||
*
|
||||
* @param array $groups Optional array of groups to test
|
||||
* @param bool $collect_cc Optional, Should code coverage be collected?
|
||||
* @return Kohana_PHPUnit Instance of $this
|
||||
*/
|
||||
public function run(array $groups = array(), $collect_cc = FALSE)
|
||||
{
|
||||
if ($collect_cc AND ! extension_loaded('xdebug'))
|
||||
{
|
||||
throw new Kohana_Exception('Code coverage cannot be collected because the xdebug extension is not loaded');
|
||||
}
|
||||
|
||||
$this->result->collectCodeCoverageInformation((bool) $collect_cc);
|
||||
|
||||
// Run the tests.
|
||||
$this->suite->run($this->result, FALSE, $groups);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
|
||||
{
|
||||
$this->totals['errors']++;
|
||||
$this->current['result'] = 'errors';
|
||||
$this->current['message'] = $test->getStatusMessage();
|
||||
|
||||
}
|
||||
|
||||
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
|
||||
{
|
||||
$this->totals['failures']++;
|
||||
$this->current['result'] = 'failures';
|
||||
$this->current['message'] = $test->getStatusMessage();
|
||||
}
|
||||
|
||||
public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
|
||||
{
|
||||
$this->totals['incomplete']++;
|
||||
$this->current['result'] = 'incomplete';
|
||||
$this->current['message'] = $test->getStatusMessage();
|
||||
}
|
||||
|
||||
public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
|
||||
{
|
||||
$this->totals['skipped']++;
|
||||
$this->current['result'] = 'skipped';
|
||||
$this->current['message'] = $test->getStatusMessage();
|
||||
}
|
||||
|
||||
public function startTest(PHPUnit_Framework_Test $test)
|
||||
{
|
||||
$this->current['name'] = $test->getName(FALSE);
|
||||
$this->current['description'] = $test->toString();
|
||||
$this->current['result'] = 'passed';
|
||||
}
|
||||
|
||||
public function endTest(PHPUnit_Framework_Test $test, $time)
|
||||
{
|
||||
// Add totals
|
||||
$this->totals['tests']++;
|
||||
$this->totals['assertions'] += $test->getNumAssertions();
|
||||
|
||||
// Handle passed tests
|
||||
if ($this->current['result'] == 'passed')
|
||||
{
|
||||
// Add to total
|
||||
$this->totals['passed']++;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Add to results
|
||||
$this->results[$this->current['result']][] = $this->current;
|
||||
}
|
||||
|
||||
$this->current = array();
|
||||
|
||||
$this->time += $time;
|
||||
}
|
||||
|
||||
public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
|
||||
{
|
||||
}
|
||||
|
||||
public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
|
||||
{
|
||||
// Parse test descriptions to make them look nicer
|
||||
foreach ($this->results as $case => $testresults)
|
||||
{
|
||||
foreach ($testresults as $type => $result)
|
||||
{
|
||||
preg_match('/^(?:([a-z0-9_]+?)::)?([a-z0-9_]+)(?: with data set (#\d+ \(.*?\)))?/i', $result['description'], $m);
|
||||
|
||||
$this->results[$case][$type] += array(
|
||||
'class' => $m[1],
|
||||
'test' => $m[2],
|
||||
'data_set' => isset($m[3]) ? $m[3] : FALSE,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
* TestCase for unittesting
|
||||
*
|
||||
* @package Kohana/Unittest
|
||||
* @author Kohana Team
|
||||
* @author BRMatt <matthew@sigswitch.com>
|
||||
* @author Paul Banks
|
||||
* @copyright (c) 2008-2009 Kohana Team
|
||||
* @license http://kohanaphp.com/license
|
||||
*/
|
||||
Abstract Class Kohana_Unittest_TestCase extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Make sure PHPUnit backs up globals
|
||||
* @var boolean
|
||||
*/
|
||||
protected $backupGlobals = TRUE;
|
||||
|
||||
/**
|
||||
* A set of unittest helpers that are shared between normal / database
|
||||
* testcases
|
||||
* @var Kohana_Unittest_Helpers
|
||||
*/
|
||||
protected $_helpers = NULL;
|
||||
|
||||
/**
|
||||
* A default set of environment to be applied before each test
|
||||
* @var array
|
||||
*/
|
||||
protected $environmentDefault = array();
|
||||
|
||||
/**
|
||||
* Creates a predefined environment using the default environment
|
||||
*
|
||||
* Extending classes that have their own setUp() should call
|
||||
* parent::setUp()
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
$this->_helpers = new Kohana_Unittest_Helpers;
|
||||
|
||||
$this->setEnvironment($this->environmentDefault);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restores the original environment overriden with setEnvironment()
|
||||
*
|
||||
* Extending classes that have their own tearDown()
|
||||
* should call parent::tearDown()
|
||||
*/
|
||||
public function tearDown()
|
||||
{
|
||||
$this->_helpers->restore_environment();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all kohana related cache files in the cache directory
|
||||
*/
|
||||
public function cleanCacheDir()
|
||||
{
|
||||
return Kohana_Unittest_Helpers::clean_cache_dir();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function that replaces all occurences of '/' with
|
||||
* the OS-specific directory separator
|
||||
*
|
||||
* @param string $path The path to act on
|
||||
* @return string
|
||||
*/
|
||||
public function dirSeparator($path)
|
||||
{
|
||||
return Kohana_Unittest_Helpers::dir_separator($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows easy setting & backing up of enviroment config
|
||||
*
|
||||
* Option types are checked in the following order:
|
||||
*
|
||||
* * Server Var
|
||||
* * Static Variable
|
||||
* * Config option
|
||||
*
|
||||
* @param array $environment List of environment to set
|
||||
*/
|
||||
public function setEnvironment(array $environment)
|
||||
{
|
||||
return $this->_helpers->set_environment($environment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for internet connectivity
|
||||
*
|
||||
* @return boolean Whether an internet connection is available
|
||||
*/
|
||||
public function hasInternet()
|
||||
{
|
||||
return Kohana_Unittest_Helpers::has_internet();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user