PageRenderTime 45ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/sgl/includes/qcubed/_core/tests/simpletest/simpletest.php

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