This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/includes/kohana/modules/unittest/classes/Kohana/Unittest/Tests.php

268 lines
6.0 KiB
PHP
Raw Normal View History

2011-05-13 06:00:25 +00:00
<?php defined('SYSPATH') or die('No direct script access.');
2010-08-21 04:43:03 +00:00
/**
* PHPUnit testsuite for kohana application
*
2011-05-13 06:00:25 +00:00
* @package Kohana/UnitTest
2010-08-21 04:43:03 +00:00
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @author Paul Banks
* @copyright (c) 2008-2009 Kohana Team
* @license http://kohanaphp.com/license
*/
2011-05-13 06:00:25 +00:00
class Kohana_Unittest_Tests {
2010-08-21 04:43:03 +00:00
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)
{
2012-11-09 12:18:50 +00:00
restore_exception_handler();
restore_error_handler();
2010-08-21 04:43:03 +00:00
2011-05-13 06:00:25 +00:00
spl_autoload_register(array('Unittest_tests', 'autoload'));
2010-08-21 04:43:03 +00:00
2011-05-13 06:00:25 +00:00
Unittest_tests::$cache = (($cache = Kohana::cache('unittest_whitelist_cache')) === NULL) ? array() : $cache;
2010-08-21 04:43:03 +00:00
}
/**
* Creates the test suite for kohana
*
2012-11-09 12:18:50 +00:00
* @return Unittest_TestSuite
2010-08-21 04:43:03 +00:00
*/
static function suite()
{
static $suite = NULL;
if ($suite instanceof PHPUnit_Framework_TestSuite)
{
return $suite;
}
2012-11-09 12:18:50 +00:00
Unittest_Tests::configure_environment();
2010-08-21 04:43:03 +00:00
2012-11-09 12:18:50 +00:00
$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);
}
2010-08-21 04:43:03 +00:00
2012-11-09 12:18:50 +00:00
// Add tests
$files = Kohana::list_files('tests');
2010-08-21 04:43:03 +00:00
self::addTests($suite, $files);
return $suite;
}
/**
* Add files to test suite $suite
*
* Uses recursion to scan subdirectories
*
2012-11-09 12:18:50 +00:00
* @param Unittest_TestSuite $suite The test suite to add to
2010-08-21 04:43:03 +00:00
* @param array $files Array of files to test
*/
2012-11-09 12:18:50 +00:00
static function addTests(Unittest_TestSuite $suite, array $files)
2010-08-21 04:43:03 +00:00
{
2011-05-13 06:00:25 +00:00
2012-11-09 12:18:50 +00:00
foreach ($files as $path => $file)
2010-08-21 04:43:03 +00:00
{
if (is_array($file))
{
2012-11-09 12:18:50 +00:00
if ($path != 'tests'.DIRECTORY_SEPARATOR.'test_data')
{
self::addTests($suite, $file);
}
2010-08-21 04:43:03 +00:00
}
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);
}
2012-11-09 12:18:50 +00:00
$suite->addFileToBlacklist($file);
2011-05-13 06:00:25 +00:00
}
}
}
}
/**
* Blacklist a set of files in PHPUnit code coverage
*
2012-11-09 12:18:50 +00:00
* @param array $blacklist_items A set of files to blacklist
* @param Unittest_TestSuite $suite The test suite
2011-05-13 06:00:25 +00:00
*/
2012-11-09 12:18:50 +00:00
static public function blacklist(array $blacklist_items, Unittest_TestSuite $suite = NULL)
2011-05-13 06:00:25 +00:00
{
2012-11-09 12:18:50 +00:00
foreach ($blacklist_items as $item)
2011-05-13 06:00:25 +00:00
{
2012-11-09 12:18:50 +00:00
if (is_dir($item))
2011-05-13 06:00:25 +00:00
{
2012-11-09 12:18:50 +00:00
$suite->addDirectoryToBlacklist($item);
2011-05-13 06:00:25 +00:00
}
2012-11-09 12:18:50 +00:00
else
2011-05-13 06:00:25 +00:00
{
2012-11-09 12:18:50 +00:00
$suite->addFileToBlacklist($item);
2010-08-21 04:43:03 +00:00
}
}
}
/**
* 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
2012-11-09 12:18:50 +00:00
* @param Unittest_Testsuite $suite Suite to load the whitelist into
2010-08-21 04:43:03 +00:00
*/
2012-11-09 12:18:50 +00:00
static public function whitelist(array $directories = NULL, Unittest_TestSuite $suite = NULL)
2010-08-21 04:43:03 +00:00
{
if (empty($directories))
{
$directories = self::get_config_whitelist();
}
if (count($directories))
{
2011-05-13 06:00:25 +00:00
foreach ($directories as & $directory)
2010-08-21 04:43:03 +00:00
{
$directory = realpath($directory).'/';
}
// Only whitelist the "top" files in the cascading filesystem
2012-11-09 12:18:50 +00:00
self::set_whitelist(Kohana::list_files('classes', $directories), $suite);
2010-08-21 04:43:03 +00:00
}
}
/**
* 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()
{
2012-11-09 12:18:50 +00:00
$config = Kohana::$config->load('unittest');
2010-08-21 04:43:03 +00:00
$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
2012-11-09 12:18:50 +00:00
* @param Unittest_TestSuite $suite Suite to load the whitelist into
2010-08-21 04:43:03 +00:00
*/
2012-11-09 12:18:50 +00:00
static protected function set_whitelist($files, Unittest_TestSuite $suite = NULL)
2010-08-21 04:43:03 +00:00
{
2011-05-13 06:00:25 +00:00
2010-08-21 04:43:03 +00:00
foreach ($files as $file)
{
if (is_array($file))
{
2012-11-09 12:18:50 +00:00
self::set_whitelist($file, $suite);
2010-08-21 04:43:03 +00:00
}
else
{
2011-05-13 06:00:25 +00:00
if ( ! isset(Unittest_tests::$cache[$file]))
2010-08-21 04:43:03 +00:00
{
$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
2011-05-13 06:00:25 +00:00
Unittest_tests::$cache[$file] = ($cascading_file === $file);
2010-08-21 04:43:03 +00:00
}
2011-05-13 06:00:25 +00:00
if (Unittest_tests::$cache[$file])
2010-08-21 04:43:03 +00:00
{
2012-11-09 12:18:50 +00:00
if (isset($suite))
2011-05-13 06:00:25 +00:00
{
2012-11-09 12:18:50 +00:00
$suite->addFileToWhitelist($file);
2011-05-13 06:00:25 +00:00
}
else
{
PHPUnit_Util_Filter::addFileToWhitelist($file);
}
2010-08-21 04:43:03 +00:00
}
}
}
}
}