Tests.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * PHPUnit testsuite for kohana application
  4. *
  5. * @package Kohana/UnitTest
  6. * @author Kohana Team
  7. * @author BRMatt <matthew@sigswitch.com>
  8. * @author Paul Banks
  9. * @copyright (c) Kohana Team
  10. * @license https://koseven.ga/LICENSE.md
  11. */
  12. class Kohana_Unittest_Tests {
  13. static protected $cache = [];
  14. /**
  15. * Loads test files if they cannot be found by kohana
  16. * @param <type> $class
  17. */
  18. static function autoload($class)
  19. {
  20. $file = str_replace('_', '/', $class);
  21. if ($file = Kohana::find_file('tests', $file))
  22. {
  23. require_once $file;
  24. }
  25. }
  26. /**
  27. * Configures the environment for testing
  28. *
  29. * Does the following:
  30. *
  31. * * Loads the phpunit framework (for the web ui)
  32. * * Restores exception phpunit error handlers (for cli)
  33. * * registeres an autoloader to load test files
  34. */
  35. static public function configure_environment()
  36. {
  37. restore_exception_handler();
  38. restore_error_handler();
  39. spl_autoload_register(['Unittest_tests', 'autoload']);
  40. Unittest_tests::$cache = (($cache = Kohana::cache('unittest_whitelist_cache')) === NULL) ? [] : $cache;
  41. }
  42. /**
  43. * Creates the test suite for kohana
  44. *
  45. * @return Unittest_TestSuite
  46. */
  47. static function suite()
  48. {
  49. static $suite = NULL;
  50. if ($suite instanceof PHPUnit_Framework_TestSuite)
  51. {
  52. return $suite;
  53. }
  54. Unittest_Tests::configure_environment();
  55. $suite = new Unittest_TestSuite;
  56. // Load the whitelist and blacklist for code coverage
  57. $config = Kohana::$config->load('unittest');
  58. if ($config->use_whitelist)
  59. {
  60. Unittest_Tests::whitelist(NULL, $suite);
  61. }
  62. // Add tests
  63. $files = Kohana::list_files('tests');
  64. self::addTests($suite, $files);
  65. return $suite;
  66. }
  67. /**
  68. * Add files to test suite $suite
  69. *
  70. * Uses recursion to scan subdirectories
  71. *
  72. * @param Unittest_TestSuite $suite The test suite to add to
  73. * @param array $files Array of files to test
  74. */
  75. static function addTests(Unittest_TestSuite $suite, array $files)
  76. {
  77. foreach ($files as $path => $file)
  78. {
  79. if (is_array($file))
  80. {
  81. if ($path != 'tests'.DIRECTORY_SEPARATOR.'test_data')
  82. {
  83. self::addTests($suite, $file);
  84. }
  85. }
  86. else
  87. {
  88. // Make sure we only include php files
  89. if (is_file($file) AND substr($file, -strlen(EXT)) === EXT)
  90. {
  91. // The default PHPUnit TestCase extension
  92. if ( ! strpos($file, 'TestCase'.EXT))
  93. {
  94. $suite->addTestFile($file);
  95. }
  96. else
  97. {
  98. require_once($file);
  99. }
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * Sets the whitelist
  106. *
  107. * If no directories are provided then the function'll load the whitelist
  108. * set in the config file
  109. *
  110. * @param array $directories Optional directories to whitelist
  111. * @param Unittest_Testsuite $suite Suite to load the whitelist into
  112. */
  113. static public function whitelist(array $directories = NULL, Unittest_TestSuite $suite = NULL)
  114. {
  115. if (empty($directories))
  116. {
  117. $directories = self::get_config_whitelist();
  118. }
  119. if (count($directories))
  120. {
  121. foreach ($directories as & $directory)
  122. {
  123. $directory = realpath($directory).'/';
  124. }
  125. // Only whitelist the "top" files in the cascading filesystem
  126. self::set_whitelist(Kohana::list_files('classes', $directories), $suite);
  127. }
  128. }
  129. /**
  130. * Works out the whitelist from the config
  131. * Used only on the CLI
  132. *
  133. * @returns array Array of directories to whitelist
  134. */
  135. static protected function get_config_whitelist()
  136. {
  137. $config = Kohana::$config->load('unittest');
  138. $directories = [];
  139. if ($config->whitelist['app'])
  140. {
  141. $directories['k_app'] = APPPATH;
  142. }
  143. if ($modules = $config->whitelist['modules'])
  144. {
  145. $k_modules = Kohana::modules();
  146. // Have to do this because kohana merges config...
  147. // If you want to include all modules & override defaults then TRUE must be the first
  148. // value in the modules array of your app/config/unittest file
  149. if (array_search(TRUE, $modules, TRUE) === (count($modules) - 1))
  150. {
  151. $modules = $k_modules;
  152. }
  153. elseif (array_search(FALSE, $modules, TRUE) === FALSE)
  154. {
  155. $modules = array_intersect_key($k_modules, array_combine($modules, $modules));
  156. }
  157. else
  158. {
  159. // modules are disabled
  160. $modules = [];
  161. }
  162. $directories += $modules;
  163. }
  164. if ($config->whitelist['system'])
  165. {
  166. $directories['k_sys'] = SYSPATH;
  167. }
  168. return $directories;
  169. }
  170. /**
  171. * Recursively whitelists an array of files
  172. *
  173. * @param array $files Array of files to whitelist
  174. * @param Unittest_TestSuite $suite Suite to load the whitelist into
  175. */
  176. static protected function set_whitelist($files, Unittest_TestSuite $suite = NULL)
  177. {
  178. foreach ($files as $file)
  179. {
  180. if (is_array($file))
  181. {
  182. self::set_whitelist($file, $suite);
  183. }
  184. else
  185. {
  186. if ( ! isset(Unittest_tests::$cache[$file]))
  187. {
  188. $relative_path = substr($file, strrpos($file, 'classes'.DIRECTORY_SEPARATOR) + 8, -strlen(EXT));
  189. $cascading_file = Kohana::find_file('classes', $relative_path);
  190. // The theory is that if this file is the highest one in the cascading filesystem
  191. // then it's safe to whitelist
  192. Unittest_tests::$cache[$file] = ($cascading_file === $file);
  193. }
  194. if (Unittest_tests::$cache[$file])
  195. {
  196. if (isset($suite))
  197. {
  198. $suite->addFileToWhitelist($file);
  199. }
  200. else
  201. {
  202. PHPUnit_Util_Filter::addFileToWhitelist($file);
  203. }
  204. }
  205. }
  206. }
  207. }
  208. }