PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/swiftmailer/swiftmailer/test-suite/lib/simpletest/simpletest.php

https://bitbucket.org/laborautonomo/laborautonomo-site
PHP | 396 lines | 172 code | 30 blank | 194 comment | 19 complexity | 1dd550b6c680944081b4b583ca3f7ee0 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 1786 2008-04-26 17:32:20Z pp11 $
  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 global registry of options.
  166. * @return hash All stored values.
  167. * @access private
  168. */
  169. protected static function &getRegistry() {
  170. static $registry = false;
  171. if (! $registry) {
  172. $registry = SimpleTest::getDefaults();
  173. }
  174. return $registry;
  175. }
  176. /**
  177. * Accessor for the context of the current
  178. * test run.
  179. * @return SimpleTestContext Current test run.
  180. * @access public
  181. */
  182. static function getContext() {
  183. static $context = false;
  184. if (! $context) {
  185. $context = new SimpleTestContext();
  186. }
  187. return $context;
  188. }
  189. /**
  190. * Constant default values.
  191. * @return hash All registry defaults.
  192. * @access private
  193. */
  194. protected static function getDefaults() {
  195. return array(
  196. 'MockBaseClass' => 'SimpleMock',
  197. 'IgnoreList' => array(),
  198. 'DefaultProxy' => false,
  199. 'DefaultProxyUsername' => false,
  200. 'DefaultProxyPassword' => false,
  201. 'Preferred' => array(new HtmlReporter(), new TextReporter(), new XmlReporter()));
  202. }
  203. /**
  204. * @deprecated
  205. */
  206. static function setMockBaseClass($mock_base) {
  207. $registry = &SimpleTest::getRegistry();
  208. $registry['MockBaseClass'] = $mock_base;
  209. }
  210. /**
  211. * @deprecated
  212. */
  213. static function getMockBaseClass() {
  214. $registry = &SimpleTest::getRegistry();
  215. return $registry['MockBaseClass'];
  216. }
  217. }
  218. /**
  219. * Container for all components for a specific
  220. * test run. Makes things like error queues
  221. * available to PHP event handlers, and also
  222. * gets around some nasty reference issues in
  223. * the mocks.
  224. * @package SimpleTest
  225. */
  226. class SimpleTestContext {
  227. private $test;
  228. private $reporter;
  229. private $resources;
  230. /**
  231. * Clears down the current context.
  232. * @access public
  233. */
  234. function clear() {
  235. $this->resources = array();
  236. }
  237. /**
  238. * Sets the current test case instance. This
  239. * global instance can be used by the mock objects
  240. * to send message to the test cases.
  241. * @param SimpleTestCase $test Test case to register.
  242. * @access public
  243. */
  244. function setTest($test) {
  245. $this->clear();
  246. $this->test = $test;
  247. }
  248. /**
  249. * Accessor for currently running test case.
  250. * @return SimpleTestCase Current test.
  251. * @access public
  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. * @access public
  262. */
  263. function setReporter($reporter) {
  264. $this->clear();
  265. $this->reporter = $reporter;
  266. }
  267. /**
  268. * Accessor for current reporter.
  269. * @return SimpleReporter Current reporter.
  270. * @access public
  271. */
  272. function getReporter() {
  273. return $this->reporter;
  274. }
  275. /**
  276. * Accessor for the Singleton resource.
  277. * @return object Global resource.
  278. * @access public
  279. */
  280. function get($resource) {
  281. if (! isset($this->resources[$resource])) {
  282. $this->resources[$resource] = new $resource();
  283. }
  284. return $this->resources[$resource];
  285. }
  286. }
  287. /**
  288. * Interrogates the stack trace to recover the
  289. * failure point.
  290. * @package SimpleTest
  291. * @subpackage UnitTester
  292. */
  293. class SimpleStackTrace {
  294. private $prefixes;
  295. /**
  296. * Stashes the list of target prefixes.
  297. * @param array $prefixes List of method prefixes
  298. * to search for.
  299. */
  300. function __construct($prefixes) {
  301. $this->prefixes = $prefixes;
  302. }
  303. /**
  304. * Extracts the last method name that was not within
  305. * Simpletest itself. Captures a stack trace if none given.
  306. * @param array $stack List of stack frames.
  307. * @return string Snippet of test report with line
  308. * number and file.
  309. * @access public
  310. */
  311. function traceMethod($stack = false) {
  312. $stack = $stack ? $stack : $this->captureTrace();
  313. foreach ($stack as $frame) {
  314. if ($this->frameLiesWithinSimpleTestFolder($frame)) {
  315. continue;
  316. }
  317. if ($this->frameMatchesPrefix($frame)) {
  318. return ' at [' . $frame['file'] . ' line ' . $frame['line'] . ']';
  319. }
  320. }
  321. return '';
  322. }
  323. /**
  324. * Test to see if error is generated by SimpleTest itself.
  325. * @param array $frame PHP stack frame.
  326. * @return boolean True if a SimpleTest file.
  327. * @access private
  328. */
  329. protected function frameLiesWithinSimpleTestFolder($frame) {
  330. if (isset($frame['file'])) {
  331. $path = substr(SIMPLE_TEST, 0, -1);
  332. if (strpos($frame['file'], $path) === 0) {
  333. if (dirname($frame['file']) == $path) {
  334. return true;
  335. }
  336. }
  337. }
  338. return false;
  339. }
  340. /**
  341. * Tries to determine if the method call is an assert, etc.
  342. * @param array $frame PHP stack frame.
  343. * @return boolean True if matches a target.
  344. * @access private
  345. */
  346. protected function frameMatchesPrefix($frame) {
  347. foreach ($this->prefixes as $prefix) {
  348. if (strncmp($frame['function'], $prefix, strlen($prefix)) == 0) {
  349. return true;
  350. }
  351. }
  352. return false;
  353. }
  354. /**
  355. * Grabs a current stack trace.
  356. * @return array Fulle trace.
  357. * @access private
  358. */
  359. protected function captureTrace() {
  360. if (function_exists('debug_backtrace')) {
  361. return array_reverse(debug_backtrace());
  362. }
  363. return array();
  364. }
  365. }
  366. ?>