|
@@ -1,206 +1,180 @@
|
|
|
<?php
|
|
|
-
|
|
|
/**
|
|
|
- * PHPUnit testsuite for ko7 application
|
|
|
+ * PHPUnit Test Suite for Koseven
|
|
|
*
|
|
|
* @package KO7/UnitTest
|
|
|
- * @author Kohana Team
|
|
|
- * @author BRMatt <matthew@sigswitch.com>
|
|
|
- * @author Paul Banks
|
|
|
- * @copyright (c) Kohana Team
|
|
|
+ *
|
|
|
+ * @author Koseven Team, Paul Banks, BRMatt <matthew@sigswitch.com>
|
|
|
+ * @copyright (c) 2007-2012 Kohana Team
|
|
|
+ * @copyright (c) 2016-2018 Koseven Team
|
|
|
* @license https://koseven.ga/LICENSE.md
|
|
|
+ *
|
|
|
+ * @codeCoverageIgnore Basic Test Suite / PHPUnit Test loader. Individual functions tested by there self.
|
|
|
*/
|
|
|
class KO7_Unittest_Tests {
|
|
|
- static protected $cache = [];
|
|
|
|
|
|
/**
|
|
|
- * Loads test files if they cannot be found by ko7
|
|
|
- * @param <type> $class
|
|
|
+ * Holds Cached Files
|
|
|
+ * @var array
|
|
|
*/
|
|
|
- static function autoload($class)
|
|
|
- {
|
|
|
- $file = str_replace('_', '/', $class);
|
|
|
+ protected static $cache = [];
|
|
|
|
|
|
- if ($file = KO7::find_file('tests', $file))
|
|
|
+ /**
|
|
|
+ * Loads test files that don't match the naming convention of kohana
|
|
|
+ * @param string $class
|
|
|
+ *
|
|
|
+ * @codeCoverageIgnore Simple Autlo-Loader find_file is tested in system tests, no need to test
|
|
|
+ */
|
|
|
+ public static function autoload(string $class) : void
|
|
|
+ {
|
|
|
+ $file = KO7::find_file('tests', str_replace('_', '/', $class));
|
|
|
+ if ($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
|
|
|
+ * - Restores exception and error handlers (for cli)
|
|
|
+ * - registers an autoloader to load test files
|
|
|
*/
|
|
|
- static public function configure_environment()
|
|
|
+ public static function configure_environment() : void
|
|
|
{
|
|
|
restore_exception_handler();
|
|
|
restore_error_handler();
|
|
|
-
|
|
|
spl_autoload_register(['Unittest_tests', 'autoload']);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Creates the test suite for ko7
|
|
|
+ * Creates the test suite for kohana
|
|
|
*
|
|
|
* @return Unittest_TestSuite
|
|
|
+ * @throws KO7_Exception
|
|
|
+ * @throws ReflectionException
|
|
|
*/
|
|
|
- static function suite()
|
|
|
+ public static function suite(): \Unittest_TestSuite
|
|
|
{
|
|
|
- static $suite = NULL;
|
|
|
-
|
|
|
- if ($suite instanceof PHPUnit_Framework_TestSuite)
|
|
|
- {
|
|
|
- return $suite;
|
|
|
- }
|
|
|
-
|
|
|
- Unittest_Tests::configure_environment();
|
|
|
-
|
|
|
+ self::configure_environment();
|
|
|
$suite = new Unittest_TestSuite;
|
|
|
|
|
|
// Load the whitelist and blacklist for code coverage
|
|
|
$config = KO7::$config->load('unittest');
|
|
|
-
|
|
|
if ($config->use_whitelist)
|
|
|
{
|
|
|
- Unittest_Tests::whitelist(NULL, $suite);
|
|
|
+ self::whitelist($suite);
|
|
|
}
|
|
|
|
|
|
// Add tests
|
|
|
$files = KO7::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
|
|
|
+ * @param array $files Array of files to test
|
|
|
+ *
|
|
|
+ * @throws ReflectionException
|
|
|
*/
|
|
|
- static function addTests(Unittest_TestSuite $suite, array $files)
|
|
|
+ public static function addTests(Unittest_TestSuite $suite, array $files) : void
|
|
|
{
|
|
|
-
|
|
|
foreach ($files as $path => $file)
|
|
|
{
|
|
|
if (is_array($file))
|
|
|
{
|
|
|
- if ($path != 'tests'.DIRECTORY_SEPARATOR.'test_data')
|
|
|
+ if ($path !== 'tests'.DIRECTORY_SEPARATOR.'test_data')
|
|
|
{
|
|
|
self::addTests($suite, $file);
|
|
|
}
|
|
|
}
|
|
|
- else
|
|
|
+ elseif (is_file($file) && substr($file, -strlen(EXT)) === EXT)
|
|
|
{
|
|
|
- // 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->addTestFile($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
|
|
|
- * @param Unittest_Testsuite $suite Suite to load the whitelist into
|
|
|
+ * @param Unittest_Testsuite $suite Suite to load the whitelist into
|
|
|
+ *
|
|
|
+ * @throws KO7_Exception
|
|
|
*/
|
|
|
- static public function whitelist(array $directories = NULL, Unittest_TestSuite $suite = NULL)
|
|
|
+ public static function whitelist(Unittest_TestSuite $suite = NULL) : void
|
|
|
{
|
|
|
- if (empty($directories))
|
|
|
- {
|
|
|
- $directories = self::get_config_whitelist();
|
|
|
- }
|
|
|
-
|
|
|
+ $directories = self::get_config_whitelist();
|
|
|
if (count($directories))
|
|
|
{
|
|
|
- foreach ($directories as & $directory)
|
|
|
+ foreach ($directories as &$directory)
|
|
|
{
|
|
|
$directory = realpath($directory).'/';
|
|
|
}
|
|
|
+ unset($directory);
|
|
|
|
|
|
// Only whitelist the "top" files in the cascading filesystem
|
|
|
self::set_whitelist(KO7::list_files('classes', $directories), $suite);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
* Works out the whitelist from the config
|
|
|
* Used only on the CLI
|
|
|
*
|
|
|
- * @returns array Array of directories to whitelist
|
|
|
+ * @return array Array of directories to whitelist
|
|
|
+ * @throws KO7_Exception
|
|
|
*/
|
|
|
- static protected function get_config_whitelist()
|
|
|
+ protected static function get_config_whitelist() : array
|
|
|
{
|
|
|
$config = KO7::$config->load('unittest');
|
|
|
$directories = [];
|
|
|
-
|
|
|
if ($config->whitelist['app'])
|
|
|
{
|
|
|
$directories['k_app'] = APPPATH;
|
|
|
}
|
|
|
-
|
|
|
- if ($modules = $config->whitelist['modules'])
|
|
|
+ $modules = $config->whitelist['modules'];
|
|
|
+ if ($modules)
|
|
|
{
|
|
|
$k_modules = KO7::modules();
|
|
|
|
|
|
- // Have to do this because ko7 merges config...
|
|
|
+ // 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
|
|
|
- {
|
|
|
+ elseif (in_array(FALSE, $modules, TRUE)) {
|
|
|
// modules are disabled
|
|
|
$modules = [];
|
|
|
}
|
|
|
-
|
|
|
+ else {
|
|
|
+ $modules = array_intersect_key($k_modules, array_combine($modules, $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
|
|
|
+ * @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)
|
|
|
+ protected static function set_whitelist(array $files, Unittest_TestSuite $suite) : void
|
|
|
{
|
|
|
-
|
|
|
foreach ($files as $file)
|
|
|
{
|
|
|
if (is_array($file))
|
|
@@ -209,27 +183,15 @@ class KO7_Unittest_Tests {
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- if ( ! isset(Unittest_tests::$cache[$file]))
|
|
|
+ if ( ! isset(self::$cache[$file]))
|
|
|
{
|
|
|
$relative_path = substr($file, strrpos($file, 'classes'.DIRECTORY_SEPARATOR) + 8, -strlen(EXT));
|
|
|
$cascading_file = KO7::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);
|
|
|
- }
|
|
|
+ self::$cache[$file] = ($cascading_file === $file);
|
|
|
}
|
|
|
+ $suite->addFileToWhitelist($file);
|
|
|
}
|
|
|
}
|
|
|
}
|