PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/TestSuite/CakeTestSuiteDispatcher.php

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