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

/tests/test_tools/simpletest/options.php

http://prado3.googlecode.com/
PHP | 366 lines | 164 code | 25 blank | 177 comment | 25 complexity | 02212e1e1ef3ae85a265672447782663 MD5 | raw file
Possible License(s): Apache-2.0, IPL-1.0, LGPL-3.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @version $Id: options.php 1532 2006-12-01 12:28:55Z xue $
  6. */
  7. /**
  8. * Static global directives and options.
  9. * @package SimpleTest
  10. */
  11. class SimpleTestOptions {
  12. /**
  13. * Reads the SimpleTest version from the release file.
  14. * @return string Version string.
  15. * @static
  16. * @access public
  17. */
  18. static function getVersion() {
  19. $content = file(dirname(__FILE__) . '/VERSION');
  20. return trim($content[0]);
  21. }
  22. /**
  23. * Sets the name of a test case to ignore, usually
  24. * because the class is an abstract case that should
  25. * not be run.
  26. * @param string $class Add a class to ignore.
  27. * @static
  28. * @access public
  29. */
  30. static function ignore($class) {
  31. $registry =SimpleTestOptions::_getRegistry();
  32. $registry['IgnoreList'][] = strtolower($class);
  33. }
  34. /**
  35. * Test to see if a test case is in the ignore
  36. * list.
  37. * @param string $class Class name to test.
  38. * @return boolean True if should not be run.
  39. * @access public
  40. * @static
  41. */
  42. static function isIgnored($class) {
  43. $registry =SimpleTestOptions::_getRegistry();
  44. return in_array(strtolower($class), $registry['IgnoreList']);
  45. }
  46. /**
  47. * The base class name is settable here. This is the
  48. * class that a new stub will inherited from.
  49. * To modify the generated stubs simply extend the
  50. * SimpleStub class and set it's name
  51. * with this method before any stubs are generated.
  52. * @param string $stub_base Server stub class to use.
  53. * @static
  54. * @access public
  55. */
  56. static function setStubBaseClass($stub_base) {
  57. $registry =SimpleTestOptions::_getRegistry();
  58. $registry['StubBaseClass'] = $stub_base;
  59. }
  60. /**
  61. * Accessor for the currently set stub base class.
  62. * @return string Class name to inherit from.
  63. * @static
  64. * @access public
  65. */
  66. static function getStubBaseClass() {
  67. $registry =SimpleTestOptions::_getRegistry();
  68. return $registry['StubBaseClass'];
  69. }
  70. /**
  71. * The base class name is settable here. This is the
  72. * class that a new mock will inherited from.
  73. * To modify the generated mocks simply extend the
  74. * SimpleMock class and set it's name
  75. * with this method before any mocks are generated.
  76. * @param string $mock_base Mock base class to use.
  77. * @static
  78. * @access public
  79. */
  80. static function setMockBaseClass($mock_base) {
  81. $registry =SimpleTestOptions::_getRegistry();
  82. $registry['MockBaseClass'] = $mock_base;
  83. }
  84. /**
  85. * Accessor for the currently set mock base class.
  86. * @return string Class name to inherit from.
  87. * @static
  88. * @access public
  89. */
  90. static function getMockBaseClass() {
  91. $registry =SimpleTestOptions::_getRegistry();
  92. return $registry['MockBaseClass'];
  93. }
  94. /**
  95. * Adds additional mock code.
  96. * @param string $code Extra code that can be added
  97. * to the partial mocks for
  98. * extra functionality. Useful
  99. * when a test tool has overridden
  100. * the mock base classes.
  101. * @access public
  102. */
  103. static function addPartialMockCode($code = '') {
  104. $registry =SimpleTestOptions::_getRegistry();
  105. $registry['AdditionalPartialMockCode'] = $code;
  106. }
  107. /**
  108. * Accessor for additional partial mock code.
  109. * @return string Extra code.
  110. * @access public
  111. */
  112. function getPartialMockCode() {
  113. $registry =SimpleTestOptions::_getRegistry();
  114. return $registry['AdditionalPartialMockCode'];
  115. }
  116. /**
  117. * Sets proxy to use on all requests for when
  118. * testing from behind a firewall. Set host
  119. * to false to disable. This will take effect
  120. * if there are no other proxy settings.
  121. * @param string $proxy Proxy host as URL.
  122. * @param string $username Proxy username for authentication.
  123. * @param string $password Proxy password for authentication.
  124. * @access public
  125. */
  126. static function useProxy($proxy, $username = false, $password = false) {
  127. $registry =SimpleTestOptions::_getRegistry();
  128. $registry['DefaultProxy'] = $proxy;
  129. $registry['DefaultProxyUsername'] = $username;
  130. $registry['DefaultProxyPassword'] = $password;
  131. }
  132. /**
  133. * Accessor for default proxy host.
  134. * @return string Proxy URL.
  135. * @access public
  136. */
  137. function getDefaultProxy() {
  138. $registry =SimpleTestOptions::_getRegistry();
  139. return $registry['DefaultProxy'];
  140. }
  141. /**
  142. * Accessor for default proxy username.
  143. * @return string Proxy username for authentication.
  144. * @access public
  145. */
  146. function getDefaultProxyUsername() {
  147. $registry =SimpleTestOptions::_getRegistry();
  148. return $registry['DefaultProxyUsername'];
  149. }
  150. /**
  151. * Accessor for default proxy password.
  152. * @return string Proxy password for authentication.
  153. * @access public
  154. */
  155. function getDefaultProxyPassword() {
  156. $registry =SimpleTestOptions::_getRegistry();
  157. return $registry['DefaultProxyPassword'];
  158. }
  159. /**
  160. * Accessor for global registry of options.
  161. * @return hash All stored values.
  162. * @access private
  163. * @static
  164. */
  165. static function _getRegistry() {
  166. static $registry = false;
  167. if (! $registry) {
  168. $registry = SimpleTestOptions::_getDefaults();
  169. }
  170. return $registry;
  171. }
  172. /**
  173. * Constant default values.
  174. * @return hash All registry defaults.
  175. * @access private
  176. * @static
  177. */
  178. static function _getDefaults() {
  179. return array(
  180. 'StubBaseClass' => 'SimpleStub',
  181. 'MockBaseClass' => 'SimpleMock',
  182. 'IgnoreList' => array(),
  183. 'AdditionalPartialMockCode' => '',
  184. 'DefaultProxy' => false,
  185. 'DefaultProxyUsername' => false,
  186. 'DefaultProxyPassword' => false);
  187. }
  188. }
  189. /**
  190. * Static methods for compatibility between different
  191. * PHP versions.
  192. * @package SimpleTest
  193. */
  194. class SimpleTestCompatibility {
  195. /**
  196. * Identity test. Drops back to equality + types for PHP5
  197. * objects as the === operator counts as the
  198. * stronger reference constraint.
  199. * @param mixed $first Test subject.
  200. * @param mixed $second Comparison object.
  201. * @access public
  202. * @static
  203. */
  204. static function isIdentical($first, $second) {
  205. if ($first != $second) {
  206. return false;
  207. }
  208. if (version_compare(phpversion(), '5') >= 0) {
  209. return SimpleTestCompatibility::_isIdenticalType($first, $second);
  210. }
  211. return ($first === $second);
  212. }
  213. /**
  214. * Recursive type test.
  215. * @param mixed $first Test subject.
  216. * @param mixed $second Comparison object.
  217. * @access private
  218. * @static
  219. */
  220. static function _isIdenticalType($first, $second) {
  221. if (gettype($first) != gettype($second)) {
  222. return false;
  223. }
  224. if (is_object($first) && is_object($second)) {
  225. if (get_class($first) != get_class($second)) {
  226. return false;
  227. }
  228. return SimpleTestCompatibility::_isArrayOfIdenticalTypes(
  229. get_object_vars($first),
  230. get_object_vars($second));
  231. }
  232. if (is_array($first) && is_array($second)) {
  233. return SimpleTestCompatibility::_isArrayOfIdenticalTypes($first, $second);
  234. }
  235. return true;
  236. }
  237. /**
  238. * Recursive type test for each element of an array.
  239. * @param mixed $first Test subject.
  240. * @param mixed $second Comparison object.
  241. * @access private
  242. * @static
  243. */
  244. static function _isArrayOfIdenticalTypes($first, $second) {
  245. if (array_keys($first) != array_keys($second)) {
  246. return false;
  247. }
  248. foreach (array_keys($first) as $key) {
  249. $is_identical = SimpleTestCompatibility::_isIdenticalType(
  250. $first[$key],
  251. $second[$key]);
  252. if (! $is_identical) {
  253. return false;
  254. }
  255. }
  256. return true;
  257. }
  258. /**
  259. * Test for two variables being aliases.
  260. * @param mixed $first Test subject.
  261. * @param mixed $second Comparison object.
  262. * @access public
  263. * @static
  264. */
  265. static function isReference($first, $second) {
  266. if (version_compare(phpversion(), '5', '>=')
  267. && is_object($first)) {
  268. return ($first === $second);
  269. }
  270. $temp = $first;
  271. $first = uniqid("test");
  272. $is_ref = ($first === $second);
  273. $first = $temp;
  274. return $is_ref;
  275. }
  276. /**
  277. * Test to see if an object is a member of a
  278. * class hiearchy.
  279. * @param object $object Object to test.
  280. * @param string $class Root name of hiearchy.
  281. * @access public
  282. * @static
  283. */
  284. static function isA($object, $class) {
  285. if (version_compare(phpversion(), '5') >= 0) {
  286. if (! class_exists($class, false)) {
  287. return false;
  288. }
  289. eval("\$is_a = \$object instanceof $class;");
  290. return $is_a;
  291. }
  292. if (function_exists('is_a')) {
  293. return is_a($object, $class);
  294. }
  295. return ((strtolower($class) == get_class($object))
  296. or (is_subclass_of($object, $class)));
  297. }
  298. /**
  299. * Autoload safe version of class_exists().
  300. * @param string $class Name of class to look for.
  301. * @return boolean True if class is defined.
  302. * @access public
  303. * @static
  304. */
  305. static function classExists($class) {
  306. if (version_compare(phpversion(), '5') >= 0) {
  307. return class_exists($class, false);
  308. } else {
  309. return class_exists($class);
  310. }
  311. }
  312. /**
  313. * Sets a socket timeout for each chunk.
  314. * @param resource $handle Socket handle.
  315. * @param integer $timeout Limit in seconds.
  316. * @access public
  317. * @static
  318. */
  319. static function setTimeout($handle, $timeout) {
  320. if (function_exists('stream_set_timeout')) {
  321. stream_set_timeout($handle, $timeout, 0);
  322. } elseif (function_exists('socket_set_timeout')) {
  323. socket_set_timeout($handle, $timeout, 0);
  324. } elseif (function_exists('set_socket_timeout')) {
  325. set_socket_timeout($handle, $timeout, 0);
  326. }
  327. }
  328. /**
  329. * Gets the current stack trace topmost first.
  330. * @return array List of stack frames.
  331. * @access public
  332. * @static
  333. */
  334. static function getStackTrace() {
  335. if (function_exists('debug_backtrace')) {
  336. return array_reverse(debug_backtrace());
  337. }
  338. return array();
  339. }
  340. }
  341. ?>