simpletest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. ?>