PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/simpletest/simpletest.php

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