diff --git a/~dev_rating/modules/unittest/bootstrap.php b/~dev_rating/modules/unittest/bootstrap.php index 493076b62b3d66ff34c2f968def713aa08d33142..dd0c9fcdc9c4feafa92ddbd848bcbc455e3710dd 100644 --- a/~dev_rating/modules/unittest/bootstrap.php +++ b/~dev_rating/modules/unittest/bootstrap.php @@ -34,11 +34,11 @@ define('EXT', '.php'); /** * Set the path to the document root * - * This assumes that this file is stored 2 levels below the DOCROOT, if you move - * this bootstrap file somewhere else then you'll need to modify this value to + * This assumes that this file is stored 2 levels below the DOCROOT, if you move + * this bootstrap file somewhere else then you'll need to modify this value to * compensate. */ -define('DOCROOT', realpath(dirname(__FILE__).'/../../').DIRECTORY_SEPARATOR); +define('DOCROOT', realpath(dirname(__FILE__) . '/../../') . DIRECTORY_SEPARATOR); /** * Set the PHP error reporting level. If you set this in php.ini, you remove this. @@ -63,27 +63,24 @@ error_reporting(E_ALL | E_STRICT); */ // Make the application relative to the docroot -if ( ! is_dir($application) AND is_dir(DOCROOT.$application)) -{ - $application = DOCROOT.$application; +if (!is_dir($application) AND is_dir(DOCROOT . $application)) { + $application = DOCROOT . $application; } // Make the modules relative to the docroot -if ( ! is_dir($modules) AND is_dir(DOCROOT.$modules)) -{ - $modules = DOCROOT.$modules; +if (!is_dir($modules) AND is_dir(DOCROOT . $modules)) { + $modules = DOCROOT . $modules; } // Make the system relative to the docroot -if ( ! is_dir($system) AND is_dir(DOCROOT.$system)) -{ - $system = DOCROOT.$system; +if (!is_dir($system) AND is_dir(DOCROOT . $system)) { + $system = DOCROOT . $system; } // Define the absolute paths for configured directories -define('APPPATH', realpath($application).DIRECTORY_SEPARATOR); -define('MODPATH', realpath($modules).DIRECTORY_SEPARATOR); -define('SYSPATH', realpath($system).DIRECTORY_SEPARATOR); +define('APPPATH', realpath($application) . DIRECTORY_SEPARATOR); +define('MODPATH', realpath($modules) . DIRECTORY_SEPARATOR); +define('SYSPATH', realpath($system) . DIRECTORY_SEPARATOR); // Clean up the configuration vars unset($application, $modules, $system); @@ -91,40 +88,34 @@ unset($application, $modules, $system); /** * Define the start time of the application, used for profiling. */ -if ( ! defined('KOHANA_START_TIME')) -{ - define('KOHANA_START_TIME', microtime(TRUE)); +if (!defined('KOHANA_START_TIME')) { + define('KOHANA_START_TIME', microtime(true)); } /** * Define the memory usage at the start of the application, used for profiling. */ -if ( ! defined('KOHANA_START_MEMORY')) -{ - define('KOHANA_START_MEMORY', memory_get_usage()); +if (!defined('KOHANA_START_MEMORY')) { + define('KOHANA_START_MEMORY', memory_get_usage()); } // Bootstrap the application -require APPPATH.'bootstrap'.EXT; +require APPPATH . 'bootstrap' . EXT; // Disable output buffering -if (($ob_len = ob_get_length()) !== FALSE) -{ - // flush_end on an empty buffer causes headers to be sent. Only flush if needed. - if ($ob_len > 0) - { - ob_end_flush(); - } - else - { - ob_end_clean(); - } +if (($ob_len = ob_get_length()) !== false) { + // flush_end on an empty buffer causes headers to be sent. Only flush if needed. + if ($ob_len > 0) { + ob_end_flush(); + } else { + ob_end_clean(); + } } // Enable the unittest module if it is not already loaded - use the absolute path $modules = Kohana::modules(); -$unittest_path = realpath(__DIR__).DIRECTORY_SEPARATOR; -if ( ! in_array($unittest_path, $modules)) { +$unittest_path = realpath(__DIR__) . DIRECTORY_SEPARATOR; +if (!in_array($unittest_path, $modules)) { $modules['unittest'] = $unittest_path; Kohana::modules($modules); } diff --git a/~dev_rating/modules/unittest/bootstrap_all_modules.php b/~dev_rating/modules/unittest/bootstrap_all_modules.php index 9a591a2acea94b54b58a022c653f5e75efb3d33e..3e298be97b72c21b5ab87e86870cd78a60e94eb9 100644 --- a/~dev_rating/modules/unittest/bootstrap_all_modules.php +++ b/~dev_rating/modules/unittest/bootstrap_all_modules.php @@ -5,14 +5,12 @@ include_once('bootstrap.php'); // Enable all modules we can find $modules_iterator = new DirectoryIterator(MODPATH); -$modules = array(); +$modules = []; -foreach ($modules_iterator as $module) -{ - if ($module->isDir()) - { - $modules[$module->getFilename()] = MODPATH.$module->getFilename(); - } +foreach ($modules_iterator as $module) { + if ($module->isDir()) { + $modules[$module->getFilename()] = MODPATH . $module->getFilename(); + } } Kohana::modules(Kohana::modules() + $modules); diff --git a/~dev_rating/modules/unittest/classes/Kohana/Unittest/Database/TestCase.php b/~dev_rating/modules/unittest/classes/Kohana/Unittest/Database/TestCase.php index a6353c8a3a0667da9f46c8339c70df2c2d96639e..18b44b52ddc47e430344f853578332fd6455e3a9 100644 --- a/~dev_rating/modules/unittest/classes/Kohana/Unittest/Database/TestCase.php +++ b/~dev_rating/modules/unittest/classes/Kohana/Unittest/Database/TestCase.php @@ -1,311 +1,286 @@ <?php + /** * TestCase for testing a database * - * @package Kohana/UnitTest - * @author Kohana Team - * @author BRMatt <matthew@sigswitch.com> + * @package Kohana/UnitTest + * @author Kohana Team + * @author BRMatt <matthew@sigswitch.com> * @copyright (c) 2008-2009 Kohana Team - * @license http://kohanaphp.com/license + * @license http://kohanaphp.com/license */ -abstract class Kohana_Unittest_Database_TestCase extends PHPUnit_Extensions_Database_TestCase { - - /** - * Whether we should enable work arounds to make the tests compatible with phpunit 3.4 - * @var boolean - */ - protected static $_assert_type_compatability = NULL; - - /** - * Make sure PHPUnit backs up globals - * @var boolean - */ - protected $backupGlobals = FALSE; - - /** - * 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(); - - /** - * The kohana database connection that PHPUnit should use for this test - * @var string - */ - protected $_database_connection = 'default'; - - /** - * Creates a predefined environment using the default environment - * - * Extending classes that have their own setUp() should call - * parent::setUp() - */ - public function setUp() - { - if(self::$_assert_type_compatability === NULL) - { - if( ! class_exists('PHPUnit_Runner_Version')) - { - require_once 'PHPUnit/Runner/Version.php'; - } - - self::$_assert_type_compatability = version_compare(PHPUnit_Runner_Version::id(), '3.5.0', '<='); - } - - $this->_helpers = new Kohana_Unittest_Helpers; - - $this->setEnvironment($this->environmentDefault); - - return parent::setUp(); - } - - /** - * 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(); - - return parent::tearDown(); - } - - /** - * Creates a connection to the unittesting database - * - * @return PDO - */ - public function getConnection() - { - // Get the unittesting db connection - $config = Kohana::$config->load('database.'.$this->_database_connection); - - if(strtolower($config['type']) !== 'pdo') - { - $config['connection']['dsn'] = strtolower($config['type']).':'. - 'host='.$config['connection']['hostname'].';'. - 'dbname='.$config['connection']['database']; - } - - $pdo = new PDO( - $config['connection']['dsn'], - $config['connection']['username'], - $config['connection']['password'] - ); - - return $this->createDefaultDBConnection($pdo, $config['connection']['database']); - } +abstract class Kohana_Unittest_Database_TestCase extends PHPUnit_Extensions_Database_TestCase +{ + + /** + * Whether we should enable work arounds to make the tests compatible with phpunit 3.4 + * @var boolean + */ + protected static $_assert_type_compatability = null; + + /** + * Make sure PHPUnit backs up globals + * @var boolean + */ + protected $backupGlobals = false; + + /** + * 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 = []; + + /** + * The kohana database connection that PHPUnit should use for this test + * @var string + */ + protected $_database_connection = 'default'; + + /** + * Creates a predefined environment using the default environment + * + * Extending classes that have their own setUp() should call + * parent::setUp() + */ + public function setUp() { + if (self::$_assert_type_compatability === null) { + if (!class_exists('PHPUnit_Runner_Version')) { + require_once 'PHPUnit/Runner/Version.php'; + } + + self::$_assert_type_compatability = version_compare(PHPUnit_Runner_Version::id(), '3.5.0', '<='); + } + + $this->_helpers = new Kohana_Unittest_Helpers; + + $this->setEnvironment($this->environmentDefault); + + return parent::setUp(); + } + + /** + * 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(); + + return parent::tearDown(); + } + + /** + * Creates a connection to the unittesting database + * + * @return PDO + */ + public function getConnection() { + // Get the unittesting db connection + $config = Kohana::$config->load('database.' . $this->_database_connection); + + if (strtolower($config['type']) !== 'pdo') { + $config['connection']['dsn'] = strtolower($config['type']) . ':' . + 'host=' . $config['connection']['hostname'] . ';' . + 'dbname=' . $config['connection']['database']; + } + + $pdo = new PDO( + $config['connection']['dsn'], + $config['connection']['username'], + $config['connection']['password'] + ); + + return $this->createDefaultDBConnection($pdo, $config['connection']['database']); + } /** * Gets a connection to the unittest database * * @return Kohana_Database The database connection */ - public function getKohanaConnection() - { + public function getKohanaConnection() { return Database::instance(Kohana::$config->load('unittest')->db_connection); } - /** - * 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(); - } - - /** - * Asserts that a variable is of a given type. - * - * @param string $expected - * @param mixed $actual - * @param string $message - * @since Method available since Release 3.5.0 - */ - public static function assertInstanceOf($expected, $actual, $message = '') - { - if(self::$_assert_type_compatability) - { - return self::assertType($expected, $actual, $message); - } - - return parent::assertInstanceOf($expected, $actual, $message); - } - - /** - * Asserts that an attribute is of a given type. - * - * @param string $expected - * @param string $attributeName - * @param mixed $classOrObject - * @param string $message - * @since Method available since Release 3.5.0 - */ - public static function assertAttributeInstanceOf($expected, $attributeName, $classOrObject, $message = '') - { - if(self::$_assert_type_compatability) - { - return self::assertAttributeType($expected, $attributeName, $classOrObject, $message); - } - - return parent::assertAttributeInstanceOf($expected, $attributeName, $classOrObject, $message); - } - - /** - * Asserts that a variable is not of a given type. - * - * @param string $expected - * @param mixed $actual - * @param string $message - * @since Method available since Release 3.5.0 - */ - public static function assertNotInstanceOf($expected, $actual, $message = '') - { - if(self::$_assert_type_compatability) - { - return self::assertNotType($expected, $actual, $message); - } - - return self::assertNotInstanceOf($expected, $actual, $message); - } - - /** - * Asserts that an attribute is of a given type. - * - * @param string $expected - * @param string $attributeName - * @param mixed $classOrObject - * @param string $message - * @since Method available since Release 3.5.0 - */ - public static function assertAttributeNotInstanceOf($expected, $attributeName, $classOrObject, $message = '') - { - if(self::$_assert_type_compatability) - { - return self::assertAttributeNotType($expected, $attributeName, $classOrObject, $message); - } - - return self::assertAttributeNotInstanceOf($expected, $attributeName, $classOrObject, $message); - } - - /** - * Asserts that a variable is of a given type. - * - * @param string $expected - * @param mixed $actual - * @param string $message - * @since Method available since Release 3.5.0 - */ - public static function assertInternalType($expected, $actual, $message = '') - { - if(self::$_assert_type_compatability) - { - return self::assertType($expected, $actual, $message); - } - - return parent::assertInternalType($expected, $actual, $message); - } - - /** - * Asserts that an attribute is of a given type. - * - * @param string $expected - * @param string $attributeName - * @param mixed $classOrObject - * @param string $message - * @since Method available since Release 3.5.0 - */ - public static function assertAttributeInternalType($expected, $attributeName, $classOrObject, $message = '') - { - if(self::$_assert_type_compatability) - { - return self::assertAttributeType($expected, $attributeName, $classOrObject, $message); - } - - return self::assertAttributeInternalType($expected, $attributeName, $classOrObject, $message); - } - - /** - * Asserts that a variable is not of a given type. - * - * @param string $expected - * @param mixed $actual - * @param string $message - * @since Method available since Release 3.5.0 - */ - public static function assertNotInternalType($expected, $actual, $message = '') - { - if(self::$_assert_type_compatability) - { - return self::assertNotType($expected, $actual, $message); - } - - return self::assertNotInternalType($expected, $actual, $message); - } - - /** - * Asserts that an attribute is of a given type. - * - * @param string $expected - * @param string $attributeName - * @param mixed $classOrObject - * @param string $message - * @since Method available since Release 3.5.0 - */ - public static function assertAttributeNotInternalType($expected, $attributeName, $classOrObject, $message = '') - { - if(self::$_assert_type_compatability) - { - return self::assertAttributeNotType($expected, $attributeName, $classOrObject, $message); - } - - return self::assertAttributeNotInternalType($expected, $attributeName, $classOrObject, $message); - } + /** + * 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(); + } + + /** + * Asserts that a variable is of a given type. + * + * @param string $expected + * @param mixed $actual + * @param string $message + * @since Method available since Release 3.5.0 + */ + public static function assertInstanceOf($expected, $actual, $message = '') { + if (self::$_assert_type_compatability) { + return self::assertType($expected, $actual, $message); + } + + return parent::assertInstanceOf($expected, $actual, $message); + } + + /** + * Asserts that an attribute is of a given type. + * + * @param string $expected + * @param string $attributeName + * @param mixed $classOrObject + * @param string $message + * @since Method available since Release 3.5.0 + */ + public static function assertAttributeInstanceOf($expected, $attributeName, $classOrObject, $message = '') { + if (self::$_assert_type_compatability) { + return self::assertAttributeType($expected, $attributeName, $classOrObject, $message); + } + + return parent::assertAttributeInstanceOf($expected, $attributeName, $classOrObject, $message); + } + + /** + * Asserts that a variable is not of a given type. + * + * @param string $expected + * @param mixed $actual + * @param string $message + * @since Method available since Release 3.5.0 + */ + public static function assertNotInstanceOf($expected, $actual, $message = '') { + if (self::$_assert_type_compatability) { + return self::assertNotType($expected, $actual, $message); + } + + return self::assertNotInstanceOf($expected, $actual, $message); + } + + /** + * Asserts that an attribute is of a given type. + * + * @param string $expected + * @param string $attributeName + * @param mixed $classOrObject + * @param string $message + * @since Method available since Release 3.5.0 + */ + public static function assertAttributeNotInstanceOf($expected, $attributeName, $classOrObject, $message = '') { + if (self::$_assert_type_compatability) { + return self::assertAttributeNotType($expected, $attributeName, $classOrObject, $message); + } + + return self::assertAttributeNotInstanceOf($expected, $attributeName, $classOrObject, $message); + } + + /** + * Asserts that a variable is of a given type. + * + * @param string $expected + * @param mixed $actual + * @param string $message + * @since Method available since Release 3.5.0 + */ + public static function assertInternalType($expected, $actual, $message = '') { + if (self::$_assert_type_compatability) { + return self::assertType($expected, $actual, $message); + } + + return parent::assertInternalType($expected, $actual, $message); + } + + /** + * Asserts that an attribute is of a given type. + * + * @param string $expected + * @param string $attributeName + * @param mixed $classOrObject + * @param string $message + * @since Method available since Release 3.5.0 + */ + public static function assertAttributeInternalType($expected, $attributeName, $classOrObject, $message = '') { + if (self::$_assert_type_compatability) { + return self::assertAttributeType($expected, $attributeName, $classOrObject, $message); + } + + return self::assertAttributeInternalType($expected, $attributeName, $classOrObject, $message); + } + + /** + * Asserts that a variable is not of a given type. + * + * @param string $expected + * @param mixed $actual + * @param string $message + * @since Method available since Release 3.5.0 + */ + public static function assertNotInternalType($expected, $actual, $message = '') { + if (self::$_assert_type_compatability) { + return self::assertNotType($expected, $actual, $message); + } + + return self::assertNotInternalType($expected, $actual, $message); + } + + /** + * Asserts that an attribute is of a given type. + * + * @param string $expected + * @param string $attributeName + * @param mixed $classOrObject + * @param string $message + * @since Method available since Release 3.5.0 + */ + public static function assertAttributeNotInternalType($expected, $attributeName, $classOrObject, $message = '') { + if (self::$_assert_type_compatability) { + return self::assertAttributeNotType($expected, $attributeName, $classOrObject, $message); + } + + return self::assertAttributeNotInternalType($expected, $attributeName, $classOrObject, $message); + } } diff --git a/~dev_rating/modules/unittest/classes/Kohana/Unittest/Helpers.php b/~dev_rating/modules/unittest/classes/Kohana/Unittest/Helpers.php index 410090e0e1008f799c9f60131d030937f8b68618..68c5875ba3578270821ad2c80232069d6376fac6 100644 --- a/~dev_rating/modules/unittest/classes/Kohana/Unittest/Helpers.php +++ b/~dev_rating/modules/unittest/classes/Kohana/Unittest/Helpers.php @@ -1,169 +1,148 @@ -<?php +<?php /** * Unit testing helpers */ -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->load($option); - } - - list($group, $var) = explode('.', $option, 2); - - Kohana::$config->load($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); - } +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 = []; + + /** + * 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, ['_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); + } + } + } + + /** + * Restores the environment to the original state + * + * @chainable + * @return Kohana_Unittest_Helpers $this + */ + public function restore_environment() { + $this->set_environment($this->_environment_backup); + } } diff --git a/~dev_rating/modules/unittest/classes/Kohana/Unittest/TestCase.php b/~dev_rating/modules/unittest/classes/Kohana/Unittest/TestCase.php index 5db43c97600dbac739dbdc8eda75a2c24fbf07d7..2006503e96995616b76f1aa450939d4db3ca4935 100644 --- a/~dev_rating/modules/unittest/classes/Kohana/Unittest/TestCase.php +++ b/~dev_rating/modules/unittest/classes/Kohana/Unittest/TestCase.php @@ -4,258 +4,235 @@ * A version of the stock PHPUnit testcase that includes some extra helpers * and default settings */ -abstract class Kohana_Unittest_TestCase extends PHPUnit_Framework_TestCase { - - /** - * Whether we should enable work arounds to make the tests compatible with phpunit 3.4 - * @var boolean - */ - protected static $_assert_type_compatability = NULL; - - /** - * Make sure PHPUnit backs up globals - * @var boolean - */ - protected $backupGlobals = FALSE; - - /** - * 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() - { - if(self::$_assert_type_compatability === NULL) - { - if( ! class_exists('PHPUnit_Runner_Version')) - { - require_once 'PHPUnit/Runner/Version.php'; - } - - self::$_assert_type_compatability = version_compare(PHPUnit_Runner_Version::id(), '3.5.0', '<='); - } - - $this->_helpers = new 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 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 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 Unittest_Helpers::has_internet(); - } - - /** - * Asserts that a variable is of a given type. - * - * @param string $expected - * @param mixed $actual - * @param string $message - * @since Method available since Release 3.5.0 - */ - public static function assertInstanceOf($expected, $actual, $message = '') - { - if(self::$_assert_type_compatability) - { - return self::assertType($expected, $actual, $message); - } - - return parent::assertInstanceOf($expected, $actual, $message); - } - - /** - * Asserts that an attribute is of a given type. - * - * @param string $expected - * @param string $attributeName - * @param mixed $classOrObject - * @param string $message - * @since Method available since Release 3.5.0 - */ - public static function assertAttributeInstanceOf($expected, $attributeName, $classOrObject, $message = '') - { - if(self::$_assert_type_compatability) - { - return self::assertAttributeType($expected, $attributeName, $classOrObject, $message); - } - - return parent::assertAttributeInstanceOf($expected, $attributeName, $classOrObject, $message); - } - - /** - * Asserts that a variable is not of a given type. - * - * @param string $expected - * @param mixed $actual - * @param string $message - * @since Method available since Release 3.5.0 - */ - public static function assertNotInstanceOf($expected, $actual, $message = '') - { - if(self::$_assert_type_compatability) - { - return self::assertNotType($expected, $actual, $message); - } - - return parent::assertNotInstanceOf($expected, $actual, $message); - } - - /** - * Asserts that an attribute is of a given type. - * - * @param string $expected - * @param string $attributeName - * @param mixed $classOrObject - * @param string $message - * @since Method available since Release 3.5.0 - */ - public static function assertAttributeNotInstanceOf($expected, $attributeName, $classOrObject, $message = '') - { - if(self::$_assert_type_compatability) - { - return self::assertAttributeNotType($expected, $attributeName, $classOrObject, $message); - } - - return parent::assertAttributeNotInstanceOf($expected, $attributeName, $classOrObject, $message); - } - - /** - * Asserts that a variable is of a given type. - * - * @param string $expected - * @param mixed $actual - * @param string $message - * @since Method available since Release 3.5.0 - */ - public static function assertInternalType($expected, $actual, $message = '') - { - if(self::$_assert_type_compatability) - { - return self::assertType($expected, $actual, $message); - } - - return parent::assertInternalType($expected, $actual, $message); - } - - /** - * Asserts that an attribute is of a given type. - * - * @param string $expected - * @param string $attributeName - * @param mixed $classOrObject - * @param string $message - * @since Method available since Release 3.5.0 - */ - public static function assertAttributeInternalType($expected, $attributeName, $classOrObject, $message = '') - { - if(self::$_assert_type_compatability) - { - return self::assertAttributeType($expected, $attributeName, $classOrObject, $message); - } - - return parent::assertAttributeInternalType($expected, $attributeName, $classOrObject, $message); - } - - /** - * Asserts that a variable is not of a given type. - * - * @param string $expected - * @param mixed $actual - * @param string $message - * @since Method available since Release 3.5.0 - */ - public static function assertNotInternalType($expected, $actual, $message = '') - { - if(self::$_assert_type_compatability) - { - return self::assertNotType($expected, $actual, $message); - } - - return parent::assertNotInternalType($expected, $actual, $message); - } - - /** - * Asserts that an attribute is of a given type. - * - * @param string $expected - * @param string $attributeName - * @param mixed $classOrObject - * @param string $message - * @since Method available since Release 3.5.0 - */ - public static function assertAttributeNotInternalType($expected, $attributeName, $classOrObject, $message = '') - { - if(self::$_assert_type_compatability) - { - return self::assertAttributeNotType($expected, $attributeName, $classOrObject, $message); - } - - return parent::assertAttributeNotInternalType($expected, $attributeName, $classOrObject, $message); - } +abstract class Kohana_Unittest_TestCase extends PHPUnit_Framework_TestCase +{ + + /** + * Whether we should enable work arounds to make the tests compatible with phpunit 3.4 + * @var boolean + */ + protected static $_assert_type_compatability = null; + + /** + * Make sure PHPUnit backs up globals + * @var boolean + */ + protected $backupGlobals = false; + + /** + * 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 = []; + + /** + * Creates a predefined environment using the default environment + * + * Extending classes that have their own setUp() should call + * parent::setUp() + */ + public function setUp() { + if (self::$_assert_type_compatability === null) { + if (!class_exists('PHPUnit_Runner_Version')) { + require_once 'PHPUnit/Runner/Version.php'; + } + + self::$_assert_type_compatability = version_compare(PHPUnit_Runner_Version::id(), '3.5.0', '<='); + } + + $this->_helpers = new 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() { + 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 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 Unittest_Helpers::has_internet(); + } + + /** + * Asserts that a variable is of a given type. + * + * @param string $expected + * @param mixed $actual + * @param string $message + * @since Method available since Release 3.5.0 + */ + public static function assertInstanceOf($expected, $actual, $message = '') { + if (self::$_assert_type_compatability) { + return self::assertType($expected, $actual, $message); + } + + return parent::assertInstanceOf($expected, $actual, $message); + } + + /** + * Asserts that an attribute is of a given type. + * + * @param string $expected + * @param string $attributeName + * @param mixed $classOrObject + * @param string $message + * @since Method available since Release 3.5.0 + */ + public static function assertAttributeInstanceOf($expected, $attributeName, $classOrObject, $message = '') { + if (self::$_assert_type_compatability) { + return self::assertAttributeType($expected, $attributeName, $classOrObject, $message); + } + + return parent::assertAttributeInstanceOf($expected, $attributeName, $classOrObject, $message); + } + + /** + * Asserts that a variable is not of a given type. + * + * @param string $expected + * @param mixed $actual + * @param string $message + * @since Method available since Release 3.5.0 + */ + public static function assertNotInstanceOf($expected, $actual, $message = '') { + if (self::$_assert_type_compatability) { + return self::assertNotType($expected, $actual, $message); + } + + return parent::assertNotInstanceOf($expected, $actual, $message); + } + + /** + * Asserts that an attribute is of a given type. + * + * @param string $expected + * @param string $attributeName + * @param mixed $classOrObject + * @param string $message + * @since Method available since Release 3.5.0 + */ + public static function assertAttributeNotInstanceOf($expected, $attributeName, $classOrObject, $message = '') { + if (self::$_assert_type_compatability) { + return self::assertAttributeNotType($expected, $attributeName, $classOrObject, $message); + } + + return parent::assertAttributeNotInstanceOf($expected, $attributeName, $classOrObject, $message); + } + + /** + * Asserts that a variable is of a given type. + * + * @param string $expected + * @param mixed $actual + * @param string $message + * @since Method available since Release 3.5.0 + */ + public static function assertInternalType($expected, $actual, $message = '') { + if (self::$_assert_type_compatability) { + return self::assertType($expected, $actual, $message); + } + + return parent::assertInternalType($expected, $actual, $message); + } + + /** + * Asserts that an attribute is of a given type. + * + * @param string $expected + * @param string $attributeName + * @param mixed $classOrObject + * @param string $message + * @since Method available since Release 3.5.0 + */ + public static function assertAttributeInternalType($expected, $attributeName, $classOrObject, $message = '') { + if (self::$_assert_type_compatability) { + return self::assertAttributeType($expected, $attributeName, $classOrObject, $message); + } + + return parent::assertAttributeInternalType($expected, $attributeName, $classOrObject, $message); + } + + /** + * Asserts that a variable is not of a given type. + * + * @param string $expected + * @param mixed $actual + * @param string $message + * @since Method available since Release 3.5.0 + */ + public static function assertNotInternalType($expected, $actual, $message = '') { + if (self::$_assert_type_compatability) { + return self::assertNotType($expected, $actual, $message); + } + + return parent::assertNotInternalType($expected, $actual, $message); + } + + /** + * Asserts that an attribute is of a given type. + * + * @param string $expected + * @param string $attributeName + * @param mixed $classOrObject + * @param string $message + * @since Method available since Release 3.5.0 + */ + public static function assertAttributeNotInternalType($expected, $attributeName, $classOrObject, $message = '') { + if (self::$_assert_type_compatability) { + return self::assertAttributeNotType($expected, $attributeName, $classOrObject, $message); + } + + return parent::assertAttributeNotInternalType($expected, $attributeName, $classOrObject, $message); + } } diff --git a/~dev_rating/modules/unittest/classes/Kohana/Unittest/TestSuite.php b/~dev_rating/modules/unittest/classes/Kohana/Unittest/TestSuite.php index c8533732b93cf135f5fecf07b03b898c11db3e23..f995b40e2795d1854454b465fba92a67eaa5e2eb 100644 --- a/~dev_rating/modules/unittest/classes/Kohana/Unittest/TestSuite.php +++ b/~dev_rating/modules/unittest/classes/Kohana/Unittest/TestSuite.php @@ -1,23 +1,24 @@ <?php defined('SYSPATH') or die('No direct script access.'); /** - * A version of the stock PHPUnit testsuite that supports whitelisting and + * A version of the stock PHPUnit testsuite that supports whitelisting and * blacklisting for code coverage filter */ -abstract class Kohana_Unittest_TestSuite extends PHPUnit_Framework_TestSuite +abstract class Kohana_Unittest_TestSuite extends PHPUnit_Framework_TestSuite { - /** - * Holds the details of files that should be white and blacklisted for - * code coverage - * - * @var array - */ - protected $_filter_calls = array( - 'addFileToBlacklist' => array(), - 'addDirectoryToBlacklist' => array(), - 'addFileToWhitelist' => array()); - - /** + /** + * Holds the details of files that should be white and blacklisted for + * code coverage + * + * @var array + */ + protected $_filter_calls = [ + 'addFileToBlacklist' => [], + 'addDirectoryToBlacklist' => [], + 'addFileToWhitelist' => [] + ]; + + /** * Runs the tests and collects their result in a TestResult. * * @param PHPUnit_Framework_TestResult $result @@ -28,53 +29,46 @@ abstract class Kohana_Unittest_TestSuite extends PHPUnit_Framework_TestSuite * @return PHPUnit_Framework_TestResult * @throws InvalidArgumentException */ - public function run(PHPUnit_Framework_TestResult $result = NULL, $filter = FALSE, array $groups = array(), array $excludeGroups = array(), $processIsolation = FALSE) - { - - // Get the code coverage filter from the suite's result object - $coverage = $result->getCodeCoverage(); - - if ($coverage) - { - $coverage_filter = $coverage->filter(); + public function run(PHPUnit_Framework_TestResult $result = null, $filter = false, array $groups = [], array $excludeGroups = [], $processIsolation = false) { + + // Get the code coverage filter from the suite's result object + $coverage = $result->getCodeCoverage(); + + if ($coverage) { + $coverage_filter = $coverage->filter(); + + // Apply the white and blacklisting + foreach ($this->_filter_calls as $method => $args) { + foreach ($args as $arg) { + $coverage_filter->$method($arg); + } + } + } - // Apply the white and blacklisting - foreach ($this->_filter_calls as $method => $args) - { - foreach ($args as $arg) - { - $coverage_filter->$method($arg); - } - } - } - - return parent::run($result, $filter, $groups, $excludeGroups, $processIsolation); - } - - /** - * Queues a file to be added to the code coverage blacklist when the suite runs - * @param string $file - */ - public function addFileToBlacklist($file) - { - $this->_filter_calls['addFileToBlacklist'][] = $file; - } + return parent::run($result, $filter, $groups, $excludeGroups, $processIsolation); + } - /** - * Queues a directory to be added to the code coverage blacklist when the suite runs - * @param string $dir - */ - public function addDirectoryToBlacklist($dir) - { - $this->_filter_calls['addDirectoryToBlacklist'][] = $dir; - } - - /** - * Queues a file to be added to the code coverage whitelist when the suite runs - * @param string $file - */ - public function addFileToWhitelist($file) - { - $this->_filter_calls['addFileToWhitelist'][] = $file; - } + /** + * Queues a file to be added to the code coverage blacklist when the suite runs + * @param string $file + */ + public function addFileToBlacklist($file) { + $this->_filter_calls['addFileToBlacklist'][] = $file; + } + + /** + * Queues a directory to be added to the code coverage blacklist when the suite runs + * @param string $dir + */ + public function addDirectoryToBlacklist($dir) { + $this->_filter_calls['addDirectoryToBlacklist'][] = $dir; + } + + /** + * Queues a file to be added to the code coverage whitelist when the suite runs + * @param string $file + */ + public function addFileToWhitelist($file) { + $this->_filter_calls['addFileToWhitelist'][] = $file; + } } diff --git a/~dev_rating/modules/unittest/classes/Kohana/Unittest/Tests.php b/~dev_rating/modules/unittest/classes/Kohana/Unittest/Tests.php index 308cd4ac113809a7e7a9e2d172ef74d231c0d2b3..6ea003f8830282490f5d585bc1413668d84e6d9f 100644 --- a/~dev_rating/modules/unittest/classes/Kohana/Unittest/Tests.php +++ b/~dev_rating/modules/unittest/classes/Kohana/Unittest/Tests.php @@ -3,265 +3,221 @@ /** * PHPUnit testsuite for kohana application * - * @package Kohana/UnitTest - * @author Kohana Team - * @author BRMatt <matthew@sigswitch.com> - * @author Paul Banks + * @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 + * @license http://kohanaphp.com/license */ -class Kohana_Unittest_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) - { - restore_exception_handler(); - restore_error_handler(); - - spl_autoload_register(array('Unittest_tests', 'autoload')); - - Unittest_tests::$cache = (($cache = Kohana::cache('unittest_whitelist_cache')) === NULL) ? array() : $cache; - - } - - /** - * Creates the test suite for kohana - * - * @return Unittest_TestSuite - */ - static function suite() - { - static $suite = NULL; - - if ($suite instanceof PHPUnit_Framework_TestSuite) - { - return $suite; - } - - Unittest_Tests::configure_environment(); - - $suite = new Unittest_TestSuite; - - // Load the whitelist and blacklist for code coverage - $config = Kohana::$config->load('unittest'); - - if ($config->use_whitelist) - { - Unittest_Tests::whitelist(NULL, $suite); - } - - if (count($config['blacklist'])) - { - Unittest_Tests::blacklist($config->blacklist, $suite); - } - - // Add tests - $files = Kohana::list_files('tests'); - self::addTests($suite, $files); - - return $suite; - } - - /** - * Add files to test suite $suite - * - * Uses recursion to scan subdirectories - * - * @param Unittest_TestSuite $suite The test suite to add to - * @param array $files Array of files to test - */ - static function addTests(Unittest_TestSuite $suite, array $files) - { - - foreach ($files as $path => $file) - { - if (is_array($file)) - { - if ($path != 'tests'.DIRECTORY_SEPARATOR.'test_data') - { - 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); - } - - $suite->addFileToBlacklist($file); - } - } - } - } - - /** - * Blacklist a set of files in PHPUnit code coverage - * - * @param array $blacklist_items A set of files to blacklist - * @param Unittest_TestSuite $suite The test suite - */ - static public function blacklist(array $blacklist_items, Unittest_TestSuite $suite = NULL) - { - foreach ($blacklist_items as $item) - { - if (is_dir($item)) - { - $suite->addDirectoryToBlacklist($item); - } - else - { - $suite->addFileToBlacklist($item); - } - } - } - - /** - * 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 - * @param Unittest_Testsuite $suite Suite to load the whitelist into - */ - static public function whitelist(array $directories = NULL, Unittest_TestSuite $suite = 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), $suite); - } - } - - /** - * 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->load('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 - * @param Unittest_TestSuite $suite Suite to load the whitelist into - */ - static protected function set_whitelist($files, Unittest_TestSuite $suite = NULL) - { - - foreach ($files as $file) - { - if (is_array($file)) - { - self::set_whitelist($file, $suite); - } - else - { - if ( ! isset(Unittest_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 - Unittest_tests::$cache[$file] = ($cascading_file === $file); - } - - if (Unittest_tests::$cache[$file]) - { - if (isset($suite)) - { - $suite->addFileToWhitelist($file); - } - else - { - PHPUnit_Util_Filter::addFileToWhitelist($file); - } - } - } - } - } +class Kohana_Unittest_Tests +{ + static protected $cache = []; + + /** + * 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) { + restore_exception_handler(); + restore_error_handler(); + + spl_autoload_register(['Unittest_tests', 'autoload']); + + Unittest_tests::$cache = (($cache = Kohana::cache('unittest_whitelist_cache')) === null) ? [] : $cache; + + } + + /** + * Creates the test suite for kohana + * + * @return Unittest_TestSuite + */ + static function suite() { + static $suite = null; + + if ($suite instanceof PHPUnit_Framework_TestSuite) { + return $suite; + } + + Unittest_Tests::configure_environment(); + + $suite = new Unittest_TestSuite; + + // Load the whitelist and blacklist for code coverage + $config = Kohana::$config->load('unittest'); + + if ($config->use_whitelist) { + Unittest_Tests::whitelist(null, $suite); + } + + if (count($config['blacklist'])) { + Unittest_Tests::blacklist($config->blacklist, $suite); + } + + // Add tests + $files = Kohana::list_files('tests'); + self::addTests($suite, $files); + + return $suite; + } + + /** + * Add files to test suite $suite + * + * Uses recursion to scan subdirectories + * + * @param Unittest_TestSuite $suite The test suite to add to + * @param array $files Array of files to test + */ + static function addTests(Unittest_TestSuite $suite, array $files) { + + foreach ($files as $path => $file) { + if (is_array($file)) { + if ($path != 'tests' . DIRECTORY_SEPARATOR . 'test_data') { + 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); + } + + $suite->addFileToBlacklist($file); + } + } + } + } + + /** + * Blacklist a set of files in PHPUnit code coverage + * + * @param array $blacklist_items A set of files to blacklist + * @param Unittest_TestSuite $suite The test suite + */ + static public function blacklist(array $blacklist_items, Unittest_TestSuite $suite = null) { + foreach ($blacklist_items as $item) { + if (is_dir($item)) { + $suite->addDirectoryToBlacklist($item); + } else { + $suite->addFileToBlacklist($item); + } + } + } + + /** + * 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 + * @param Unittest_Testsuite $suite Suite to load the whitelist into + */ + static public function whitelist(array $directories = null, Unittest_TestSuite $suite = 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), $suite); + } + } + + /** + * 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->load('unittest'); + $directories = []; + + 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 = []; + } + + $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 + * @param Unittest_TestSuite $suite Suite to load the whitelist into + */ + static protected function set_whitelist($files, Unittest_TestSuite $suite = null) { + + foreach ($files as $file) { + if (is_array($file)) { + self::set_whitelist($file, $suite); + } else { + if (!isset(Unittest_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 + Unittest_tests::$cache[$file] = ($cascading_file === $file); + } + + if (Unittest_tests::$cache[$file]) { + if (isset($suite)) { + $suite->addFileToWhitelist($file); + } else { + PHPUnit_Util_Filter::addFileToWhitelist($file); + } + } + } + } + } } diff --git a/~dev_rating/modules/unittest/classes/Unittest/Database/TestCase.php b/~dev_rating/modules/unittest/classes/Unittest/Database/TestCase.php index baeaa0a803e1205a5766faa63acf72fcc18c4968..179f73af21e1aa4ac6f95ba05fead63eecf83a52 100644 --- a/~dev_rating/modules/unittest/classes/Unittest/Database/TestCase.php +++ b/~dev_rating/modules/unittest/classes/Unittest/Database/TestCase.php @@ -3,15 +3,15 @@ /** * Transparent extension for Kohana_Unittest_Database_TestCase * - * Provides some unittest helpers and allows a kohana database connection to be + * Provides some unittest helpers and allows a kohana database connection to be * used to connect to the database * - * @package Kohana/UnitTest - * @author Kohana Team + * @package Kohana/UnitTest + * @author Kohana Team * @copyright (c) 2008-2009 Kohana Team - * @license http://kohanaphp.com/license + * @license http://kohanaphp.com/license */ abstract class Unittest_Database_TestCase extends Kohana_Unittest_Database_TestCase { - + } diff --git a/~dev_rating/modules/unittest/classes/Unittest/Helpers.php b/~dev_rating/modules/unittest/classes/Unittest/Helpers.php index 46da816aadfda8fef3b47218e438af80a805f2c1..47e34b4296501a62c620ca76782adbbbbc5380f7 100644 --- a/~dev_rating/modules/unittest/classes/Unittest/Helpers.php +++ b/~dev_rating/modules/unittest/classes/Unittest/Helpers.php @@ -1,3 +1,5 @@ <?php defined('SYSPATH') or die('No direct script access.'); -class Unittest_Helpers extends Kohana_Unittest_Helpers {} +class Unittest_Helpers extends Kohana_Unittest_Helpers +{ +} diff --git a/~dev_rating/modules/unittest/classes/Unittest/TestCase.php b/~dev_rating/modules/unittest/classes/Unittest/TestCase.php index b30aab29b0f53da2a0a06c75d254ee7c27f2606e..ea4a480c603d21ff61af9815efab5e9e984a96b8 100644 --- a/~dev_rating/modules/unittest/classes/Unittest/TestCase.php +++ b/~dev_rating/modules/unittest/classes/Unittest/TestCase.php @@ -1,3 +1,5 @@ <?php defined('SYSPATH') or die('No direct script access.'); -abstract class Unittest_TestCase extends Kohana_Unittest_TestCase {} +abstract class Unittest_TestCase extends Kohana_Unittest_TestCase +{ +} diff --git a/~dev_rating/modules/unittest/classes/Unittest/TestSuite.php b/~dev_rating/modules/unittest/classes/Unittest/TestSuite.php index 6ab179cfe84b5cdddbe737f3740c93794efea95a..3552b21a822711b1e70072761ad50efb0809fe2b 100644 --- a/~dev_rating/modules/unittest/classes/Unittest/TestSuite.php +++ b/~dev_rating/modules/unittest/classes/Unittest/TestSuite.php @@ -1,3 +1,5 @@ <?php defined('SYSPATH') OR die('No direct script access.'); -class Unittest_TestSuite extends Kohana_Unittest_TestSuite {} +class Unittest_TestSuite extends Kohana_Unittest_TestSuite +{ +} diff --git a/~dev_rating/modules/unittest/classes/Unittest/Tests.php b/~dev_rating/modules/unittest/classes/Unittest/Tests.php index 1d6f9fc38c706eddde12bfb3cb3ffd7bbdd7648b..88c795b4f926358e8e2eb71dfd38a87acc39b068 100644 --- a/~dev_rating/modules/unittest/classes/Unittest/Tests.php +++ b/~dev_rating/modules/unittest/classes/Unittest/Tests.php @@ -1,3 +1,5 @@ <?php defined('SYSPATH') or die('No direct script access.'); -class Unittest_Tests extends Kohana_Unittest_Tests {} +class Unittest_Tests extends Kohana_Unittest_Tests +{ +} diff --git a/~dev_rating/modules/unittest/config/unittest.php b/~dev_rating/modules/unittest/config/unittest.php index 8db23c6e4e5914879c35b21fa1c4d68c2aa09f5b..fe250cf7266aca9cbee66446f770f82fea267dc2 100644 --- a/~dev_rating/modules/unittest/config/unittest.php +++ b/~dev_rating/modules/unittest/config/unittest.php @@ -1,34 +1,34 @@ <?php defined('SYSPATH') or die('No direct script access.'); -return array( - - // If you don't use a whitelist then only files included during the request will be counted - // If you do, then only whitelisted items will be counted - 'use_whitelist' => TRUE, - - // Items to whitelist, only used in cli - 'whitelist' => array( - - // Should the app be whitelisted? - // Useful if you just want to test your application - 'app' => TRUE, - - // Set to array(TRUE) to include all modules, or use an array of module names - // (the keys of the array passed to Kohana::modules() in the bootstrap) - // Or set to FALSE to exclude all modules - 'modules' => array(TRUE), - - // If you don't want the Kohana code coverage reports to pollute your app's, - // then set this to FALSE - 'system' => TRUE, - ), - - // Does what it says on the tin - // Blacklisted files won't be included in code coverage reports - // If you use a whitelist then the blacklist will be ignored - 'use_blacklist' => FALSE, - - // List of individual files/folders to blacklist - 'blacklist' => array( - ), -); +return [ + + // If you don't use a whitelist then only files included during the request will be counted + // If you do, then only whitelisted items will be counted + 'use_whitelist' => true, + + // Items to whitelist, only used in cli + 'whitelist' => [ + + // Should the app be whitelisted? + // Useful if you just want to test your application + 'app' => true, + + // Set to array(TRUE) to include all modules, or use an array of module names + // (the keys of the array passed to Kohana::modules() in the bootstrap) + // Or set to FALSE to exclude all modules + 'modules' => [true], + + // If you don't want the Kohana code coverage reports to pollute your app's, + // then set this to FALSE + 'system' => true, + ], + + // Does what it says on the tin + // Blacklisted files won't be included in code coverage reports + // If you use a whitelist then the blacklist will be ignored + 'use_blacklist' => false, + + // List of individual files/folders to blacklist + 'blacklist' => [ + ], +]; diff --git a/~dev_rating/modules/unittest/config/userguide.php b/~dev_rating/modules/unittest/config/userguide.php index a286a261744c8dce6b6fd2910aa696d130c8e8bd..26f422f7b3899d7a635aa7e5a887582c3f094317 100644 --- a/~dev_rating/modules/unittest/config/userguide.php +++ b/~dev_rating/modules/unittest/config/userguide.php @@ -1,13 +1,12 @@ <?php defined('SYSPATH') OR die('No direct script access.'); -return array -( - 'modules' => array( - 'unittest' => array( - 'enabled' => TRUE, - 'name' => 'Unittest', - 'description' => 'Unit testing module', - 'copyright' => '© 2009-2011 Kohana Team', - ) - ) -); \ No newline at end of file +return [ + 'modules' => [ + 'unittest' => [ + 'enabled' => true, + 'name' => 'Unittest', + 'description' => 'Unit testing module', + 'copyright' => '© 2009-2011 Kohana Team', + ] + ] +]; \ No newline at end of file diff --git a/~dev_rating/modules/unittest/example.phpunit.xml b/~dev_rating/modules/unittest/example.phpunit.xml index 9792d37d38c3b4d6ebc67ad6417f515d91ad4b03..a4741913dbb43847b910d14013ae3603d260898d 100644 --- a/~dev_rating/modules/unittest/example.phpunit.xml +++ b/~dev_rating/modules/unittest/example.phpunit.xml @@ -8,9 +8,9 @@ Any options you specify when calling phpunit will override the ones in here --> <phpunit colors="true" bootstrap="rel/path/to/index.php"> - <testsuites> - <testsuite name="Kohana Tests"> - <file>rel/path/to/unittest/tests.php</file> - </testsuite> - </testsuites> + <testsuites> + <testsuite name="Kohana Tests"> + <file>rel/path/to/unittest/tests.php</file> + </testsuite> + </testsuites> </phpunit> diff --git a/~dev_rating/modules/unittest/tests.php b/~dev_rating/modules/unittest/tests.php index 4421fc971df055198c0f14f2e803ebb033bda752..653d24493c3725eae62d15fa025d39f2aa4397ea 100644 --- a/~dev_rating/modules/unittest/tests.php +++ b/~dev_rating/modules/unittest/tests.php @@ -1,20 +1,17 @@ <?php -if ( ! class_exists('Kohana')) -{ - die('Please include the kohana bootstrap file (see README.markdown)'); +if (!class_exists('Kohana')) { + die('Please include the kohana bootstrap file (see README.markdown)'); } -if ($file = Kohana::find_file('classes', 'Unittest/Tests')) -{ - require_once $file; +if ($file = Kohana::find_file('classes', 'Unittest/Tests')) { + require_once $file; - // PHPUnit requires a test suite class to be in this file, - // so we create a faux one that uses the kohana base - class TestSuite extends Unittest_Tests - {} -} -else -{ - die('Could not include the test suite'); + // PHPUnit requires a test suite class to be in this file, + // so we create a faux one that uses the kohana base + class TestSuite extends Unittest_Tests + { + } +} else { + die('Could not include the test suite'); }