PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/simpletest/simpletest.php

https://github.com/orchestra-io/sample-openx
PHP | 433 lines | 181 code | 38 blank | 214 comment | 16 complexity | 9a390498ab8bf761e3c5efeec6f50af1 MD5 | raw file
  1. <?php
  2. /**
  3. * Global state for SimpleTest and kicker script in future versions.
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id: simpletest.php 7321 2007-06-05 09:08:01Z andrew.hill@openads.org $
  7. */
  8. /**#@+
  9. * include SimpleTest files
  10. */
  11. if (version_compare(phpversion(), '5') >= 0) {
  12. require_once(dirname(__FILE__) . '/reflection_php5.php');
  13. } else {
  14. require_once(dirname(__FILE__) . '/reflection_php4.php');
  15. }
  16. /**#@-*/
  17. /**
  18. * Registry and test context. Includes a few
  19. * global options that I'm slowly getting rid of.
  20. * @package SimpleTest
  21. */
  22. class SimpleTest {
  23. /**
  24. * Reads the SimpleTest version from the release file.
  25. * @return string Version string.
  26. * @static
  27. * @access public
  28. */
  29. function getVersion() {
  30. $content = file(dirname(__FILE__) . '/VERSION');
  31. return trim($content[0]);
  32. }
  33. /**
  34. * Sets the name of a test case to ignore, usually
  35. * because the class is an abstract case that should
  36. * not be run. Once PHP4 is dropped this will disappear
  37. * as a public method and "abstract" will rule.
  38. * @param string $class Add a class to ignore.
  39. * @static
  40. * @access public
  41. */
  42. function ignore($class) {
  43. $registry = &SimpleTest::_getRegistry();
  44. $registry['IgnoreList'][strtolower($class)] = true;
  45. }
  46. /**
  47. * Scans the now complete ignore list, and adds
  48. * all parent classes to the list. If a class
  49. * is not a runnable test case, then it's parents
  50. * wouldn't be either. This is syntactic sugar
  51. * to cut down on ommissions of ignore()'s or
  52. * missing abstract declarations. This cannot
  53. * be done whilst loading classes wiithout forcing
  54. * a particular order on the class declarations and
  55. * the ignore() calls. It's just nice to have the ignore()
  56. * calls at the top of the file before the actual declarations.
  57. * @param array $classes Class names of interest.
  58. * @static
  59. * @access public
  60. */
  61. function ignoreParentsIfIgnored($classes) {
  62. $registry = &SimpleTest::_getRegistry();
  63. foreach ($classes as $class) {
  64. if (SimpleTest::isIgnored($class)) {
  65. $reflection = new SimpleReflection($class);
  66. if ($parent = $reflection->getParent()) {
  67. SimpleTest::ignore($parent);
  68. }
  69. }
  70. }
  71. }
  72. /**
  73. * Test to see if a test case is in the ignore
  74. * list. Quite obviously the ignore list should
  75. * be a separate object and will be one day.
  76. * This method is internal to SimpleTest. Don't
  77. * use it.
  78. * @param string $class Class name to test.
  79. * @return boolean True if should not be run.
  80. * @access public
  81. * @static
  82. */
  83. function isIgnored($class) {
  84. $registry = &SimpleTest::_getRegistry();
  85. return isset($registry['IgnoreList'][strtolower($class)]);
  86. }
  87. /**
  88. * @deprecated
  89. */
  90. function setMockBaseClass($mock_base) {
  91. $registry = &SimpleTest::_getRegistry();
  92. $registry['MockBaseClass'] = $mock_base;
  93. }
  94. /**
  95. * @deprecated
  96. */
  97. function getMockBaseClass() {
  98. $registry = &SimpleTest::_getRegistry();
  99. return $registry['MockBaseClass'];
  100. }
  101. /**
  102. * Sets proxy to use on all requests for when
  103. * testing from behind a firewall. Set host
  104. * to false to disable. This will take effect
  105. * if there are no other proxy settings.
  106. * @param string $proxy Proxy host as URL.
  107. * @param string $username Proxy username for authentication.
  108. * @param string $password Proxy password for authentication.
  109. * @access public
  110. */
  111. function useProxy($proxy, $username = false, $password = false) {
  112. $registry = &SimpleTest::_getRegistry();
  113. $registry['DefaultProxy'] = $proxy;
  114. $registry['DefaultProxyUsername'] = $username;
  115. $registry['DefaultProxyPassword'] = $password;
  116. }
  117. /**
  118. * Accessor for default proxy host.
  119. * @return string Proxy URL.
  120. * @access public
  121. */
  122. function getDefaultProxy() {
  123. $registry = &SimpleTest::_getRegistry();
  124. return $registry['DefaultProxy'];
  125. }
  126. /**
  127. * Accessor for default proxy username.
  128. * @return string Proxy username for authentication.
  129. * @access public
  130. */
  131. function getDefaultProxyUsername() {
  132. $registry = &SimpleTest::_getRegistry();
  133. return $registry['DefaultProxyUsername'];
  134. }
  135. /**
  136. * Accessor for default proxy password.
  137. * @return string Proxy password for authentication.
  138. * @access public
  139. */
  140. function getDefaultProxyPassword() {
  141. $registry = &SimpleTest::_getRegistry();
  142. return $registry['DefaultProxyPassword'];
  143. }
  144. /**
  145. * Accessor for global registry of options.
  146. * @return hash All stored values.
  147. * @access private
  148. * @static
  149. */
  150. function &_getRegistry() {
  151. static $registry = false;
  152. if (! $registry) {
  153. $registry = SimpleTest::_getDefaults();
  154. }
  155. return $registry;
  156. }
  157. /**
  158. * Accessor for the context of the current
  159. * test run.
  160. * @return SimpleTestContext Current test run.
  161. * @access public
  162. * @static
  163. */
  164. function &getContext() {
  165. static $context = false;
  166. if (! $context) {
  167. $context = new SimpleTestContext();
  168. }
  169. return $context;
  170. }
  171. /**
  172. * Constant default values.
  173. * @return hash All registry defaults.
  174. * @access private
  175. * @static
  176. */
  177. function _getDefaults() {
  178. return array(
  179. 'StubBaseClass' => 'SimpleStub',
  180. 'MockBaseClass' => 'SimpleMock',
  181. 'IgnoreList' => array(),
  182. 'DefaultProxy' => false,
  183. 'DefaultProxyUsername' => false,
  184. 'DefaultProxyPassword' => false);
  185. }
  186. }
  187. /**
  188. * Container for all components for a specific
  189. * test run. Makes things like error queues
  190. * available to PHP event handlers, and also
  191. * gets around some nasty reference issues in
  192. * the mocks.
  193. * @package SimpleTest
  194. */
  195. class SimpleTestContext {
  196. var $_test;
  197. var $_reporter;
  198. var $_resources;
  199. /**
  200. * Clears down the current context.
  201. * @access public
  202. */
  203. function clear() {
  204. $this->_resources = array();
  205. }
  206. /**
  207. * Sets the current test case instance. This
  208. * global instance can be used by the mock objects
  209. * to send message to the test cases.
  210. * @param SimpleTestCase $test Test case to register.
  211. * @access public
  212. */
  213. function setTest(&$test) {
  214. $this->clear();
  215. $this->_test = &$test;
  216. }
  217. /**
  218. * Accessor for currently running test case.
  219. * @return SimpleTestCase Current test.
  220. * @acess pubic
  221. */
  222. function &getTest() {
  223. return $this->_test;
  224. }
  225. /**
  226. * Sets the current reporter. This
  227. * global instance can be used by the mock objects
  228. * to send messages.
  229. * @param SimpleReporter $reporter Reporter to register.
  230. * @access public
  231. */
  232. function setReporter(&$reporter) {
  233. $this->clear();
  234. $this->_reporter = &$reporter;
  235. }
  236. /**
  237. * Accessor for current reporter.
  238. * @return SimpleReporter Current reporter.
  239. * @acess pubic
  240. */
  241. function &getReporter() {
  242. return $this->_reporter;
  243. }
  244. /**
  245. * Accessor for the Singleton resource.
  246. * @return object Global resource.
  247. * @access public
  248. * @static
  249. */
  250. function &get($resource) {
  251. if (! isset($this->_resources[$resource])) {
  252. $this->_resources[$resource] = &new $resource();
  253. }
  254. return $this->_resources[$resource];
  255. }
  256. }
  257. /**
  258. * Interrogates the stack trace to recover the
  259. * failure point.
  260. * @package SimpleTest
  261. * @subpackage UnitTester
  262. */
  263. class SimpleStackTrace {
  264. var $_prefixes;
  265. /**
  266. * Stashes the list of target prefixes.
  267. * @param array $prefixes List of method prefixes
  268. * to search for.
  269. */
  270. function SimpleStackTrace($prefixes) {
  271. $this->_prefixes = $prefixes;
  272. }
  273. /**
  274. * Extracts the last method name that was not within
  275. * Simpletest itself. Captures a stack trace if none given.
  276. * @param array $stack List of stack frames.
  277. * @return string Snippet of test report with line
  278. * number and file.
  279. * @access public
  280. */
  281. function traceMethod($stack = false) {
  282. $stack = $stack ? $stack : $this->_captureTrace();
  283. foreach ($stack as $frame) {
  284. if ($this->_frameLiesWithinSimpleTestFolder($frame)) {
  285. continue;
  286. }
  287. if ($this->_frameMatchesPrefix($frame)) {
  288. return ' at [' . $frame['file'] . ' line ' . $frame['line'] . ']';
  289. }
  290. }
  291. return '';
  292. }
  293. /**
  294. * Test to see if error is generated by SimpleTest itself.
  295. * @param array $frame PHP stack frame.
  296. * @return boolean True if a SimpleTest file.
  297. * @access private
  298. */
  299. function _frameLiesWithinSimpleTestFolder($frame) {
  300. if (isset($frame['file'])) {
  301. $path = substr(SIMPLE_TEST, 0, -1);
  302. if (strpos($frame['file'], $path) === 0) {
  303. if (dirname($frame['file']) == $path) {
  304. return true;
  305. }
  306. }
  307. }
  308. return false;
  309. }
  310. /**
  311. * Tries to determine if the method call is an assert, etc.
  312. * @param array $frame PHP stack frame.
  313. * @return boolean True if matches a target.
  314. * @access private
  315. */
  316. function _frameMatchesPrefix($frame) {
  317. foreach ($this->_prefixes as $prefix) {
  318. if (strncmp($frame['function'], $prefix, strlen($prefix)) == 0) {
  319. return true;
  320. }
  321. }
  322. return false;
  323. }
  324. /**
  325. * Grabs a current stack trace.
  326. * @return array Fulle trace.
  327. * @access private
  328. */
  329. function _captureTrace() {
  330. if (function_exists('debug_backtrace')) {
  331. return array_reverse(debug_backtrace());
  332. }
  333. return array();
  334. }
  335. }
  336. /**
  337. * @deprecated
  338. */
  339. class SimpleTestOptions extends SimpleTest {
  340. /**
  341. * @deprecated
  342. */
  343. function getVersion() {
  344. return Simpletest::getVersion();
  345. }
  346. /**
  347. * @deprecated
  348. */
  349. function ignore($class) {
  350. return Simpletest::ignore($class);
  351. }
  352. /**
  353. * @deprecated
  354. */
  355. function isIgnored($class) {
  356. return Simpletest::isIgnored($class);
  357. }
  358. /**
  359. * @deprecated
  360. */
  361. function setMockBaseClass($mock_base) {
  362. return Simpletest::setMockBaseClass($mock_base);
  363. }
  364. /**
  365. * @deprecated
  366. */
  367. function getMockBaseClass() {
  368. return Simpletest::getMockBaseClass();
  369. }
  370. /**
  371. * @deprecated
  372. */
  373. function useProxy($proxy, $username = false, $password = false) {
  374. return Simpletest::useProxy($proxy, $username, $password);
  375. }
  376. /**
  377. * @deprecated
  378. */
  379. function getDefaultProxy() {
  380. return Simpletest::getDefaultProxy();
  381. }
  382. /**
  383. * @deprecated
  384. */
  385. function getDefaultProxyUsername() {
  386. return Simpletest::getDefaultProxyUsername();
  387. }
  388. /**
  389. * @deprecated
  390. */
  391. function getDefaultProxyPassword() {
  392. return Simpletest::getDefaultProxyPassword();
  393. }
  394. }
  395. ?>