Base for a static organization website

CakeTestSuiteDispatcher.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * CakeTestSuiteDispatcher controls dispatching TestSuite web based requests.
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.TestSuite
  15. * @since CakePHP(tm) v 1.3
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. define('CORE_TEST_CASES', CAKE . 'Test' . DS . 'Case');
  19. define('APP_TEST_CASES', TESTS . 'Case');
  20. App::uses('CakeTestSuiteCommand', 'TestSuite');
  21. /**
  22. * CakeTestSuiteDispatcher handles web requests to the test suite and runs the correct action.
  23. *
  24. * @package Cake.TestSuite
  25. */
  26. class CakeTestSuiteDispatcher {
  27. /**
  28. * 'Request' parameters
  29. *
  30. * @var array
  31. */
  32. public $params = array(
  33. 'codeCoverage' => false,
  34. 'case' => null,
  35. 'core' => false,
  36. 'app' => false,
  37. 'plugin' => null,
  38. 'output' => 'html',
  39. 'show' => 'groups',
  40. 'show_passes' => false,
  41. 'filter' => false,
  42. 'fixture' => null
  43. );
  44. /**
  45. * Baseurl for the request
  46. *
  47. * @var string
  48. */
  49. protected $_baseUrl;
  50. /**
  51. * Base dir of the request. Used for accessing assets.
  52. *
  53. * @var string
  54. */
  55. protected $_baseDir;
  56. /**
  57. * boolean to set auto parsing of params.
  58. *
  59. * @var bool
  60. */
  61. protected $_paramsParsed = false;
  62. /**
  63. * reporter instance used for the request
  64. *
  65. * @var CakeBaseReporter
  66. */
  67. protected static $_Reporter = null;
  68. /**
  69. * Constructor
  70. */
  71. public function __construct() {
  72. $this->_baseUrl = $_SERVER['PHP_SELF'];
  73. $dir = rtrim(dirname($this->_baseUrl), '\\');
  74. $this->_baseDir = ($dir === '/') ? $dir : $dir . '/';
  75. }
  76. /**
  77. * Runs the actions required by the URL parameters.
  78. *
  79. * @return void
  80. */
  81. public function dispatch() {
  82. $this->_checkPHPUnit();
  83. $this->_parseParams();
  84. if ($this->params['case']) {
  85. $value = $this->_runTestCase();
  86. } else {
  87. $value = $this->_testCaseList();
  88. }
  89. $output = ob_get_clean();
  90. echo $output;
  91. return $value;
  92. }
  93. /**
  94. * Static method to initialize the test runner, keeps global space clean
  95. *
  96. * @return void
  97. */
  98. public static function run() {
  99. $dispatcher = new CakeTestSuiteDispatcher();
  100. $dispatcher->dispatch();
  101. }
  102. /**
  103. * Checks that PHPUnit is installed. Will exit if it doesn't
  104. *
  105. * @return void
  106. */
  107. protected function _checkPHPUnit() {
  108. $found = $this->loadTestFramework();
  109. if (!$found) {
  110. $baseDir = $this->_baseDir;
  111. include CAKE . 'TestSuite' . DS . 'templates' . DS . 'phpunit.php';
  112. exit();
  113. }
  114. }
  115. /**
  116. * Checks for the existence of the test framework files
  117. *
  118. * @return bool true if found, false otherwise
  119. */
  120. public function loadTestFramework() {
  121. if (class_exists('PHPUnit_Framework_TestCase')) {
  122. return true;
  123. }
  124. $phpunitPath = 'phpunit' . DS . 'phpunit';
  125. if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
  126. $composerGlobalDir[] = env('APPDATA') . DS . 'Composer' . DS . 'vendor' . DS;
  127. } else {
  128. $composerGlobalDir[] = env('HOME') . DS . '.composer' . DS . 'vendor' . DS;
  129. }
  130. $vendors = array_merge(App::path('vendors'), $composerGlobalDir);
  131. foreach ($vendors as $vendor) {
  132. $vendor = rtrim($vendor, DS);
  133. if (is_dir($vendor . DS . $phpunitPath)) {
  134. ini_set('include_path', $vendor . DS . $phpunitPath . PATH_SEPARATOR . ini_get('include_path'));
  135. break;
  136. } elseif (is_dir($vendor . DS . 'PHPUnit')) {
  137. ini_set('include_path', $vendor . PATH_SEPARATOR . ini_get('include_path'));
  138. break;
  139. } elseif (is_file($vendor . DS . 'phpunit.phar')) {
  140. $backup = $GLOBALS['_SERVER']['SCRIPT_NAME'];
  141. $GLOBALS['_SERVER']['SCRIPT_NAME'] = '-';
  142. $included = include_once $vendor . DS . 'phpunit.phar';
  143. $GLOBALS['_SERVER']['SCRIPT_NAME'] = $backup;
  144. return $included;
  145. }
  146. }
  147. include 'PHPUnit' . DS . 'Autoload.php';
  148. return class_exists('PHPUnit_Framework_TestCase');
  149. }
  150. /**
  151. * Checks for the xdebug extension required to do code coverage. Displays an error
  152. * if xdebug isn't installed.
  153. *
  154. * @return void
  155. */
  156. protected function _checkXdebug() {
  157. if (!extension_loaded('xdebug')) {
  158. $baseDir = $this->_baseDir;
  159. include CAKE . 'TestSuite' . DS . 'templates' . DS . 'xdebug.php';
  160. exit();
  161. }
  162. }
  163. /**
  164. * Generates a page containing the a list of test cases that could be run.
  165. *
  166. * @return void
  167. */
  168. protected function _testCaseList() {
  169. $command = new CakeTestSuiteCommand('', $this->params);
  170. $Reporter = $command->handleReporter($this->params['output']);
  171. $Reporter->paintDocumentStart();
  172. $Reporter->paintTestMenu();
  173. $Reporter->testCaseList();
  174. $Reporter->paintDocumentEnd();
  175. }
  176. /**
  177. * Sets the params, calling this will bypass the auto parameter parsing.
  178. *
  179. * @param array $params Array of parameters for the dispatcher
  180. * @return void
  181. */
  182. public function setParams($params) {
  183. $this->params = $params;
  184. $this->_paramsParsed = true;
  185. }
  186. /**
  187. * Parse URL params into a 'request'
  188. *
  189. * @return void
  190. */
  191. protected function _parseParams() {
  192. if (!$this->_paramsParsed) {
  193. if (!isset($_SERVER['SERVER_NAME'])) {
  194. $_SERVER['SERVER_NAME'] = '';
  195. }
  196. foreach ($this->params as $key => $value) {
  197. if (isset($_GET[$key])) {
  198. $this->params[$key] = $_GET[$key];
  199. }
  200. }
  201. if (isset($_GET['code_coverage'])) {
  202. $this->params['codeCoverage'] = true;
  203. $this->_checkXdebug();
  204. }
  205. }
  206. if (empty($this->params['plugin']) && empty($this->params['core'])) {
  207. $this->params['app'] = true;
  208. }
  209. $this->params['baseUrl'] = $this->_baseUrl;
  210. $this->params['baseDir'] = $this->_baseDir;
  211. }
  212. /**
  213. * Runs a test case file.
  214. *
  215. * @return void
  216. */
  217. protected function _runTestCase() {
  218. $commandArgs = array(
  219. 'case' => $this->params['case'],
  220. 'core' => $this->params['core'],
  221. 'app' => $this->params['app'],
  222. 'plugin' => $this->params['plugin'],
  223. 'codeCoverage' => $this->params['codeCoverage'],
  224. 'showPasses' => !empty($this->params['show_passes']),
  225. 'baseUrl' => $this->_baseUrl,
  226. 'baseDir' => $this->_baseDir,
  227. );
  228. $options = array(
  229. '--filter', $this->params['filter'],
  230. '--output', $this->params['output'],
  231. '--fixture', $this->params['fixture']
  232. );
  233. restore_error_handler();
  234. try {
  235. static::time();
  236. $command = new CakeTestSuiteCommand('CakeTestLoader', $commandArgs);
  237. $command->run($options);
  238. } catch (MissingConnectionException $exception) {
  239. ob_end_clean();
  240. $baseDir = $this->_baseDir;
  241. include CAKE . 'TestSuite' . DS . 'templates' . DS . 'missing_connection.php';
  242. exit();
  243. }
  244. }
  245. /**
  246. * Sets a static timestamp
  247. *
  248. * @param bool $reset to set new static timestamp.
  249. * @return int timestamp
  250. */
  251. public static function time($reset = false) {
  252. static $now;
  253. if ($reset || !$now) {
  254. $now = time();
  255. }
  256. return $now;
  257. }
  258. /**
  259. * Returns formatted date string using static time
  260. * This method is being used as formatter for created, modified and updated fields in Model::save()
  261. *
  262. * @param string $format format to be used.
  263. * @return string formatted date
  264. */
  265. public static function date($format) {
  266. return date($format, static::time());
  267. }
  268. }