Browse Source

Updated unittest module and composer.json

Tobias Oitzinger 6 years ago
parent
commit
3b7db98732

+ 2 - 1
composer.json

@@ -26,7 +26,8 @@
     "issues": "https://github.com/koseven/koseven/issues"
   },
   "require": {
-    "php": ">=7.2"
+    "php": ">=7.2",
+    "ext-dom": "*"
   },
   "require-dev": {
     "phpunit/phpunit": "^8",

+ 50 - 61
modules/unittest/classes/KO7/Unittest/Helpers.php

@@ -1,32 +1,39 @@
 <?php
-
 /**
+ * Helper Class for Unit Tests.
+ *
  * @package    KO7/UnitTest
- * @author     Kohana Team
+ *
+ * @author     Koseven Team
  * @copyright  (c) 2007-2012 Kohana Team
  * @copyright  (c) 2016-2018 Koseven Team
  * @license    https://koseven.ga/LICENSE.md
  */
 class KO7_Unittest_Helpers {
+
 	/**
-	 * Static variable used to work out whether we have an internet
-	 * connection
-	 * @see has_internet
+	 * Static variable used to work out whether we have an internet connection
 	 * @var boolean
 	 */
-	static protected $_has_internet = NULL;
+	protected static $_has_internet;
+
+	/**
+	 * Backup of the environment variables
+	 * @var array
+	 */
+	protected $_environment_backup = [];
 
 	/**
 	 * Check for internet connectivity
 	 *
-	 * @return boolean Whether an internet connection is available
+	 * @return boolean  Whether an internet connection is available
 	 */
-	public static function has_internet()
+	public static function has_internet() : bool
 	{
 		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);
+			$sock = @fsockopen('www.google.com', 80, $errno, $errstr, 1);
 
 			self::$_has_internet = (bool) $sock ? TRUE : FALSE;
 		}
@@ -37,72 +44,54 @@ class KO7_Unittest_Helpers {
 	/**
 	 * Helper function which replaces the "/" to OS-specific delimiter
 	 *
-	 * @param string $path
+	 * @param  string $path
+	 *
 	 * @return string
 	 */
-	static public function dir_separator($path)
+	public static function dir_separator(string $path) : string
 	{
 		return str_replace('/', DIRECTORY_SEPARATOR, $path);
 	}
 
 	/**
-	 * Removes all cache files from the ko7 cache dir
-	 *
-	 * @return void
+	 * Removes all cache files from the kohana cache dir (except .gitignore)
 	 */
-	static public function clean_cache_dir()
+	public static function clean_cache_dir() : void
 	{
-		$cache_dir = opendir(KO7::$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(KO7::$cache_dir.'/'.$dir.'/');
-
-				$cache = opendir($dir);
-
-				while ($file = readdir($cache))
-				{
-					if ($file[0] !== '.')
-					{
-						unlink($dir.$file);
-					}
-				}
-
-				closedir($cache);
-
-				rmdir($dir);
+		$files = new RecursiveIteratorIterator(
+			new RecursiveDirectoryIterator(KO7::$cache_dir, RecursiveDirectoryIterator::SKIP_DOTS),
+			RecursiveIteratorIterator::CHILD_FIRST
+		);
+
+		foreach ($files as $file) {
+			if ($file->getExtension() === 'gitignore') {
+				continue;
 			}
+			$todo = ($file->isDir() ? 'rmdir' : 'unlink');
+			$todo($file->getRealPath());
 		}
-
-		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
+	 * - Server Var
+	 * - Static Variable
+	 * - Config option
+	 *
+	 * @param  array $environment List of environment to set
 	 *
-	 * @codeCoverageIgnore
-	 * @param array $environment List of environment to set
+	 * @return bool
+	 * @throws KO7_Exception
+	 * @throws ReflectionException
 	 */
-	public function set_environment(array $environment)
+	public function set_environment(array $environment) : bool
 	{
-		if ( ! count($environment))
+		if ( ! count($environment)) {
 			return FALSE;
+		}
 
 		foreach ($environment as $option => $value)
 		{
@@ -125,7 +114,7 @@ class KO7_Unittest_Helpers {
 			// If this is a static property i.e. Html::$windowed_urls
 			elseif (strpos($option, '::$') !== FALSE)
 			{
-				list($class, $var) = explode('::$', $option, 2);
+				[$class, $var] = explode('::$', $option, 2);
 
 				$class = new ReflectionClass($class);
 
@@ -137,11 +126,11 @@ class KO7_Unittest_Helpers {
 				$class->setStaticPropertyValue($var, $value);
 			}
 			// If this is an environment variable
-			elseif (preg_match('/^[A-Z_-]+$/', $option) OR isset($_SERVER[$option]))
+			elseif (isset($_SERVER[$option]) || preg_match('/^[A-Z_-]+$/', $option))
 			{
 				if ($backup_needed)
 				{
-					$this->_environment_backup[$option] = isset($_SERVER[$option]) ? $_SERVER[$option] : '';
+					$this->_environment_backup[$option] = $_SERVER[$option] ?? '';
 				}
 
 				$_SERVER[$option] = $value;
@@ -154,21 +143,21 @@ class KO7_Unittest_Helpers {
 					$this->_environment_backup[$option] = KO7::$config->load($option);
 				}
 
-				list($group, $var) = explode('.', $option, 2);
+				[$group, $var] = explode('.', $option, 2);
 
 				KO7::$config->load($group)->set($var, $value);
 			}
 		}
+		return TRUE;
 	}
 
 	/**
 	 * Restores the environment to the original state
 	 *
-	 * @codeCoverageIgnore
-	 * @chainable
-	 * @return KO7_Unittest_Helpers $this
+	 * @throws KO7_Exception
+	 * @throws ReflectionException
 	 */
-	public function restore_environment()
+	public function restore_environment() : void
 	{
 		$this->set_environment($this->_environment_backup);
 	}

+ 48 - 78
modules/unittest/classes/KO7/Unittest/TestCase.php

@@ -7,7 +7,8 @@ use PHPUnit\Framework\TestCase;
  * and default settings
  *
  * @package    KO7/UnitTest
- * @author     Kohana Team
+ *
+ * @author     Koseven Team
  * @copyright  (c) 2007-2012 Kohana Team
  * @copyright  (c) 2016-2018 Koseven Team
  * @license    https://koseven.ga/LICENSE.md
@@ -15,17 +16,16 @@ use PHPUnit\Framework\TestCase;
 abstract class KO7_Unittest_TestCase extends TestCase {
 
 	/**
-	 * Make sure PHPUnit backs up globals
+	 * Make sure PHPUnit does not back up globals
 	 * @var boolean
 	 */
 	protected $backupGlobals = FALSE;
 
 	/**
-	 * A set of unittest helpers that are shared between normal / database
-	 * testcases
-	 * @var KO7_Unittest_Helpers
+	 * A set of Unittest helpers that are shared between normal / database testcases
+	 * @var Kohana_Unittest_Helpers
 	 */
-	protected $_helpers = NULL;
+	protected $_helpers;
 
 	/**
 	 * A default set of environment to be applied before each test
@@ -39,61 +39,72 @@ abstract class KO7_Unittest_TestCase extends TestCase {
 	 * Extending classes that have their own setUp() should call
 	 * parent::setUp()
 	 *
-	 * @codeCoverageIgnore
+	 * @throws KO7_Exception
+	 * @throws ReflectionException
 	 */
 	public function setUp() : void
 	{
 		$this->_helpers = new Unittest_Helpers;
 
 		$this->setEnvironment($this->environmentDefault);
+
+		parent::setUp();
 	}
 
 	/**
-	 * Restores the original environment overriden with setEnvironment()
+	 * Restores the original environment overridden with setEnvironment()
 	 *
 	 * Extending classes that have their own tearDown()
 	 * should call parent::tearDown()
 	 *
-	 * @codeCoverageIgnore
+	 * @throws KO7_Exception
+	 * @throws ReflectionException
 	 */
-	public function tearDown(): void
+	public function tearDown() : void
 	{
 		$this->_helpers->restore_environment();
+
+		parent::tearDown();
 	}
 
 	/**
-	 * Removes all ko7 related cache files in the cache directory
+	 * Removes all koseven related cache files in the cache directory
 	 */
-	public function cleanCacheDir()
+	public function cleanCacheDir() : void
 	{
-		return Unittest_Helpers::clean_cache_dir();
+		Unittest_Helpers::clean_cache_dir();
 	}
 
 	/**
-	 * Helper function that replaces all occurences of '/' with
+	 * Helper function that replaces all occurrences of '/' with
 	 * the OS-specific directory separator
 	 *
-	 * @param string $path The path to act on
+	 * @param  string $path The path to act on
 	 * @return string
+	 *
+	 * @codeCoverageIgnore Gets Tested with Helpers test
 	 */
-	public function dirSeparator($path)
+	public function dirSeparator(string $path) : string
 	{
 		return Unittest_Helpers::dir_separator($path);
 	}
 
 	/**
-	 * Allows easy setting & backing up of enviroment config
+	 * Allows easy setting & backing up of Environment config
 	 *
 	 * Option types are checked in the following order:
 	 *
-	 * * Server Var
-	 * * Static Variable
-	 * * Config option
+	 * - Server Var
+	 * - Static Variable
+	 * - Config option
+	 *
+	 * @param  array $environment List of environment to set
 	 *
-	 * @codeCoverageIgnore
-	 * @param array $environment List of environment to set
+	 * @return bool
+	 * @throws KO7_Exception
+	 * @throws ReflectionException
 	 */
-	public function setEnvironment(array $environment)
+	public function setEnvironment(array $environment) : bool
 	{
 		return $this->_helpers->set_environment($environment);
 	}
@@ -103,7 +114,7 @@ abstract class KO7_Unittest_TestCase extends TestCase {
 	 *
 	 * @return boolean Whether an internet connection is available
 	 */
-	public function hasInternet()
+	public function hasInternet() : bool
 	{
 		return Unittest_Helpers::has_internet();
 	}
@@ -111,74 +122,33 @@ abstract class KO7_Unittest_TestCase extends TestCase {
 	/**
 	 * Evaluate an HTML or XML string and assert its structure and/or contents.
 	 *
-	 * NOTE:
-	 * Overriding this method to remove the deprecation error
-	 * when tested with PHPUnit 4.2.0+
-	 *
-	 * TODO:
-	 * this should be removed when phpunit-dom-assertions gets released
-	 * https://github.com/phpunit/phpunit-dom-assertions
-	 *
-	 * @param array $matcher
+	 * @param array  $matcher
 	 * @param string $actual
 	 * @param string $message
-	 * @param bool $isHtml
-	 * @uses Unittest_TestCase::tag_match
-	 */
-	public static function assertTag($matcher, $actual, $message = '', $isHtml = true)
-	{
-		//trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
-
-		$matched = static::tag_match($matcher, $actual, $message, $isHtml);
-		static::assertTrue($matched, $message);
-	}
-
-	/**
-	 * This assertion is the exact opposite of assertTag
-	 *
-	 * Rather than asserting that $matcher results in a match, it asserts that
-	 * $matcher does not match
-	 *
-	 * NOTE:
-	 * Overriding this method to remove the deprecation error
-	 * when tested with PHPUnit 4.2.0+
+	 * @param bool   $isHtml
 	 *
-	 * TODO:
-	 * this should be removed when phpunit-dom-assertions gets released
-	 * https://github.com/phpunit/phpunit-dom-assertions
-	 *
-	 * @param array $matcher
-	 * @param string $actual
-	 * @param string $message
-	 * @param bool $isHtml
-	 * @uses Unittest_TestCase::tag_match
+	 * @deprecated since 4.0
 	 */
-	public static function assertNotTag($matcher, $actual, $message = '', $isHtml = true)
+	public static function assertTag(array $matcher, string $actual, $message = NULL, $isHtml = NULL) : void
 	{
-
-		//trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
-
-		$matched = static::tag_match($matcher, $actual, $message, $isHtml);
-		static::assertTrue($matched, $message);
+		$matched = static::tag_match($matcher, $actual, $isHtml ?? TRUE);
+		static::assertTrue($matched, $message ?? '');
 	}
 
 	/**
 	 * Helper function to match HTML string tags against certain criteria
 	 *
-	 * TODO:
-	 * this should be removed when phpunit-dom-assertions gets released
-	 * https://github.com/phpunit/phpunit-dom-assertions
-	 *
-	 * @param array $matcher
+	 * @param array  $matcher
 	 * @param string $actual
-	 * @param string $message
-	 * @param bool $isHtml
+	 * @param bool 	 $isHtml
+	 *
 	 * @return bool TRUE if there is a match FALSE otherwise
+	 *
+	 * @deprecated since 4.0
 	 */
-	protected static function tag_match($matcher, $actual, $message = '', $isHtml = true)
+	protected static function tag_match(array $matcher, string $actual, $isHtml = NULL) : bool
 	{
-		$dom = PHPUnit\Util\Xml::load($actual, $isHtml);
-        $tags = $dom->getElementsByTagName($matcher['tag']);
+		$tags = PHPUnit\Util\Xml::load($actual, $isHtml ?? TRUE)->getElementsByTagName($matcher['tag']);
 
 		return count($tags) > 0 && $tags[0] instanceof DOMNode;
 	}

+ 38 - 27
modules/unittest/classes/KO7/Unittest/TestSuite.php

@@ -4,35 +4,45 @@ use PHPUnit\Framework\TestSuite;
 use PHPUnit\Framework\TestResult;
 
 /**
- * A version of the stock PHPUnit testsuite that supports whitelisting
+ * A version of the stock PHPUnit Test Suite that supports whitelisting
  * for code coverage filter.
  *
  * @package    KO7/UnitTest
- * @author     Kohana Team
+ *
+ * @author     Koseven Team
  * @copyright  (c) 2007-2012 Kohana Team
  * @copyright  (c) 2016-2018 Koseven Team
  * @license    https://koseven.ga/LICENSE.md
+ *
+ * @codeCoverageIgnore Basic PhpUnit Test Suite. Unit Tests for this File can be ignored safely. If you change
+ *                     this class, removing this may be necessary
  */
 abstract class KO7_Unittest_TestSuite extends TestSuite
 {
-    /**
-     * Holds the details of files that should be whitelisted for
-     * code coverage
-     * @var array
-     */
-    protected $_filter_calls = [
-        'addFileToWhitelist' => []
-    ];
-
-    /**
-     * Runs the tests and collects their result in a TestResult.
-     * @param TestResult|NULL $result
-     * @return TestResult
-     */
-    public function run(TestResult $result = NULL): TestResult
-    {
+
+	/**
+	 * Holds the details of files that should be whitelisted for code coverage
+	 * @var array
+	 */
+	protected $_filter_calls = [
+		'addFileToWhitelist' => []
+	];
+
+	/**
+	 * Runs the tests and collects their result in a TestResult.
+	 *
+	 * @param  TestResult|NULL $result
+	 *
+	 * @return TestResult
+	 * @throws ReflectionException
+	 */
+	public function run(TestResult $result = NULL): TestResult
+	{
 		// Get the code coverage filter from the suite's result object
-		$coverage = $result->getCodeCoverage();
+		$coverage = FALSE;
+		if ($result !== NULL) {
+			$coverage = $result->getCodeCoverage();
+		}
 
 		if ($coverage)
 		{
@@ -51,12 +61,13 @@ abstract class KO7_Unittest_TestSuite extends TestSuite
 		return parent::run($result);
 	}
 
-    /**
-     * 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 whitelist when the suite runs
+	 *
+	 * @param string $file
+	 */
+	public function addFileToWhitelist(string $file) : void
+	{
+		$this->_filter_calls['addFileToWhitelist'][] = $file;
+	}
 }

+ 59 - 97
modules/unittest/classes/KO7/Unittest/Tests.php

@@ -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);
 			}
 		}
 	}

+ 2 - 2
modules/unittest/config/userguide.php

@@ -6,7 +6,7 @@ return [
 			'enabled' => TRUE,
 			'name' => 'Unittest',
 			'description' => 'Unit testing module',
-			'copyright' => '&copy; 2009-2017 Kohana Team',
+			'copyright' => '&copy; 2008–2012 Kohana Team ; &copy; 2018 Koseven Team'
 		]
 	]
-];
+];

+ 0 - 16
modules/unittest/example.phpunit.xml

@@ -1,16 +0,0 @@
-<!--
-	This is an example phpunit.xml file to get you started
-	Copy it to a directory, update the relative paths and rename to phpunit.xml
-	Then to run tests cd into it's directory and just run
-		phpunit
-	(it'll automatically use any phpunit.xml file in the current directory)
-
-	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="KO7 Tests">
-			<file>rel/path/to/unittest/tests.php</file>
-		</testsuite>
-	</testsuites>
-</phpunit>

+ 1 - 2
modules/unittest/tests.php

@@ -11,8 +11,7 @@ if ($file = KO7::find_file('classes', 'Unittest/Tests'))
 
 	// PHPUnit requires a test suite class to be in this file,
 	// so we create a faux one that uses the ko7 base
-	class TestSuite extends Unittest_Tests
-	{}
+	class TestSuite extends Unittest_Tests {}
 }
 else
 {

+ 76 - 0
modules/unittest/tests/HelpersTest.php

@@ -0,0 +1,76 @@
+<?php
+/**
+ * Testing Unittest Module Helper Class
+ * Very Important as other Unittests depend on that
+ *
+ * @package    KO7/Unittest
+ * @group      koseven
+ * @group      koseven.unittest
+ * @category   Test
+ *
+ * @author	   Koseven Team
+ * @copyright  (c) since 2018 Koseven Team
+ * @license    https://koseven.ga/LICENSE.md
+ */
+class HelpersTest extends Unittest_TestCase {
+
+	/**
+	 * Replaces all / in string with directory Separator
+	 */
+	public function test_dir_separator() : void
+	{
+		$expected = 'test'.DIRECTORY_SEPARATOR.'dir';
+		self::assertSame($expected, KO7_Unittest_Helpers::dir_separator('test/dir'));
+	}
+	/**
+	 * Tests cleaning cache dir which should happen recursivley.
+	 * Following structure will be created to test that:
+	 *
+	 *   - Folder
+	 *     .hidden-file
+	 *     - Folder2
+	 *       - file2
+	 *   - file
+	 */
+	public function test_clean_cache_dir() : void
+	{
+		// Kohana Cache directory
+		$cacheDir = KO7::$cache_dir;
+
+		// Create Test Directories and Files
+		$folder = $cacheDir.DIRECTORY_SEPARATOR.'Folder';
+		$folder2 = $folder.DIRECTORY_SEPARATOR.'Folder2';
+		$file = $folder.DIRECTORY_SEPARATOR.'.hidden-file';
+		$file2 = $cacheDir.DIRECTORY_SEPARATOR.'file';
+		$file3 = $folder2.DIRECTORY_SEPARATOR.'file2';
+		if ( ! mkdir($folder) || ! mkdir($folder2) || ! touch($file) || ! touch($file2) || ! touch($file3)) {
+			self::fail('Could not create Test Files. Please make sure your cache dir is writable');
+		}
+		KO7_Unittest_Helpers::clean_cache_dir();
+		$files = scandir($cacheDir, 0);
+
+		// Only files left should now be: '.','..','.gitignore'
+		self::assertCount(3, $files);
+	}
+	/**
+	 * Test setting configuration options via set environment
+	 * @throws KO7_Exception
+	 * @throws ReflectionException
+	 */
+	public function test_set_environment_config_options()
+	{
+		// Init
+		$config = [
+			'cache.default' => 'test'
+		];
+		$helpers = new KO7_Unittest_Helpers();
+
+		// Set new environment and check if set correctly
+		$helpers->set_environment($config);
+		self::assertSame('test', KO7::$config->load('cache')->get('default'));
+
+		// Restore old environment and check if restored
+		$helpers->restore_environment();
+		self::assertNotSame('test', KO7::$config->load('cache')->get('default'));
+	}
+}

+ 9 - 5
phpunit.xml

@@ -1,21 +1,25 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+<phpunit
+		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 		xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.5/phpunit.xsd"
 		bootstrap="modules/unittest/bootstrap.php"
-		forceCoversAnnotation="false"
-		beStrictAboutCoversAnnotation="false"
+		cacheTokens="false"
 		beStrictAboutOutputDuringTests="true"
-		beStrictAboutTodoAnnotatedTests="true"
-		verbose="false">
+		beStrictAboutTodoAnnotatedTests="true">
+
+	<!-- Koseven Test Suite -->
 	<testsuite name="default">
 		<directory suffix="tests.php">modules/unittest/</directory>
+		<directory suffix="tests.php">./modules/unittest</directory>
 	</testsuite>
 
+	<!-- Path to store coverage reports -->
 	<logging>
 		<log type="coverage-clover" target="build/logs/clover.xml"/>
 		<log type="coverage-html" target="build/logs/"/>
 	</logging>
 
+	<!-- Whitelist for Coverage Support -->
 	<filter>
 		<whitelist processUncoveredFilesFromWhitelist="true">
 			<directory suffix=".php">./application/classes</directory>

Some files were not shown because too many files changed in this diff