PageRenderTime 46ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/unit-test/bpm-testlib/bpm-utils.php

http://buddypress-media.googlecode.com/
PHP | 356 lines | 248 code | 68 blank | 40 comment | 28 complexity | 2408647b0dd3c0639715e70a183e11f5 MD5 | raw file
Possible License(s): AGPL-1.0, Apache-2.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * BP-MEDIA UNIT TEST UTILITIES
  4. * Helper functions and utilities
  5. *
  6. * @version 0.1.9
  7. * @since 0.1.9
  8. * @author original version from http://svn.automattic.com/wordpress-tests/
  9. * @package BP-Media
  10. * @subpackage Unit Test
  11. * @link http://code.google.com/p/buddypress-media/
  12. *
  13. * ========================================================================================================
  14. */
  15. function rand_str($len=32) {
  16. return substr(md5(uniqid(rand())), 0, $len);
  17. }
  18. // Strip leading and trailing whitespace from each line in the string
  19. function strip_ws($txt) {
  20. $lines = explode("\n", $txt);
  21. $result = array();
  22. foreach ($lines as $line)
  23. if (trim($line))
  24. $result[] = trim($line);
  25. return trim(join("\n", $result));
  26. }
  27. /**
  28. * Helper class for testing code that involves actions and filters
  29. *
  30. *
  31. * Typical use:
  32. * $ma = new MockAction();
  33. * add_action('foo', array(&$ma, 'action'));
  34. *
  35. */
  36. class MockAction {
  37. var $events;
  38. var $debug;
  39. function MockAction($debug=0) {
  40. $this->reset();
  41. $this->debug = $debug;
  42. }
  43. function reset() {
  44. $this->events = array();
  45. }
  46. function current_filter() {
  47. if (is_callable('current_filter'))
  48. return current_filter();
  49. global $wp_actions;
  50. return end($wp_actions);
  51. }
  52. function action($arg) {
  53. if($this->debug){
  54. dmp(__FUNCTION__, $this->current_filter());
  55. }
  56. $args = func_get_args();
  57. $this->events[] = array('action' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args);
  58. return $arg;
  59. }
  60. function action2($arg) {
  61. if($this->debug){
  62. dmp(__FUNCTION__, $this->current_filter());
  63. }
  64. $args = func_get_args();
  65. $this->events[] = array('action' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args);
  66. return $arg;
  67. }
  68. function filter($arg) {
  69. if ($this->debug) dmp(__FUNCTION__, $this->current_filter());
  70. $args = func_get_args();
  71. $this->events[] = array('filter' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args);
  72. return $arg;
  73. }
  74. function filter2($arg) {
  75. if ($this->debug) dmp(__FUNCTION__, $this->current_filter());
  76. $args = func_get_args();
  77. $this->events[] = array('filter' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args);
  78. return $arg;
  79. }
  80. function filter_append($arg) {
  81. if ($this->debug) dmp(__FUNCTION__, $this->current_filter());
  82. $args = func_get_args();
  83. $this->events[] = array('filter' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args);
  84. return $arg . '_append';
  85. }
  86. function filterall($tag, $arg=NULL) {
  87. // This one doesn't return the result, so it's safe to use with the new 'all' filter
  88. if ($this->debug) dmp(__FUNCTION__, $this->current_filter());
  89. $args = func_get_args();
  90. $this->events[] = array('filter' => __FUNCTION__, 'tag'=>$tag, 'args'=>array_slice($args, 1));
  91. }
  92. // Return a list of all the actions, tags and args
  93. function get_events() {
  94. return $this->events;
  95. }
  96. // Return a count of the number of times the action was called since the last reset
  97. function get_call_count($tag='') {
  98. if ($tag) {
  99. $count = 0;
  100. foreach ($this->events as $e)
  101. if ($e['action'] == $tag)
  102. ++$count;
  103. return $count;
  104. }
  105. return count($this->events);
  106. }
  107. // Return an array of the tags that triggered calls to this action
  108. function get_tags() {
  109. $out = array();
  110. foreach ($this->events as $e) {
  111. $out[] = $e['tag'];
  112. }
  113. return $out;
  114. }
  115. // Return an array of args passed in calls to this action
  116. function get_args() {
  117. $out = array();
  118. foreach ($this->events as $e)
  119. $out[] = $e['args'];
  120. return $out;
  121. }
  122. }
  123. // Convert valid xml to an array tree structure
  124. // kinda lame but it works with a default php 4 install
  125. class testXMLParser {
  126. var $xml;
  127. var $stack = array();
  128. var $data = array();
  129. function testXMLParser($in) {
  130. $this->xml = xml_parser_create();
  131. xml_set_object($this->xml, $this);
  132. xml_parser_set_option($this->xml,XML_OPTION_CASE_FOLDING, 0);
  133. xml_set_element_handler($this->xml, array(&$this, 'startHandler'), array(&$this, 'endHandler'));
  134. xml_set_character_data_handler($this->xml, array(&$this, 'dataHandler'));
  135. $this->parse($in);
  136. }
  137. function parse($in) {
  138. $parse = xml_parse($this->xml, $in, sizeof($in));
  139. if (!$parse) {
  140. trigger_error(sprintf("XML error: %s at line %d",
  141. xml_error_string(xml_get_error_code($this->xml)),
  142. xml_get_current_line_number($this->xml)), E_USER_ERROR);
  143. xml_parser_free($this->xml);
  144. }
  145. return true;
  146. }
  147. function startHandler($parser, $name, $attributes) {
  148. $data['name'] = $name;
  149. if ($attributes) { $data['attributes'] = $attributes; }
  150. $this->data[] = $data;
  151. }
  152. function dataHandler($parser, $data) {
  153. $index = count($this->data) - 1;
  154. @$this->data[$index]['content'] .= $data;
  155. }
  156. function endHandler($parser, $name) {
  157. if (count($this->data) > 1) {
  158. $data = array_pop($this->data);
  159. $index = count($this->data) - 1;
  160. $this->data[$index]['child'][] = $data;
  161. }
  162. }
  163. }
  164. function xml_to_array($in) {
  165. $p = new testXMLParser($in);
  166. return $p->data;
  167. }
  168. function &xml_find(&$tree /*, $el1, $el2, $el3, .. */) {
  169. $a = func_get_args();
  170. $a = array_slice($a, 1);
  171. $n = count($a);
  172. # var_dump(__FUNCTION__, $a, $n);
  173. $out = array();
  174. if ($n < 1)
  175. return $out;
  176. for ($i=0; $i<count($tree); $i++) {
  177. # echo "checking '{$tree[$i][name]}' == '{$a[0]}'\n";
  178. # var_dump($tree[$i]['name'], $a[0]);
  179. if ($tree[$i]['name'] == $a[0]) {
  180. # echo "n == {$n}\n";
  181. if ($n == 1)
  182. $out[] = $tree[$i];
  183. else {
  184. $subtree =& $tree[$i]['child'];
  185. $call_args = array($subtree);
  186. $call_args = array_merge($call_args, array_slice($a, 1));
  187. $out = array_merge($out, call_user_func_array('xml_find', $call_args));
  188. }
  189. }
  190. }
  191. return $out;
  192. }
  193. function xml_join_atts($atts) {
  194. $a = array();
  195. foreach ($atts as $k=>$v)
  196. $a[] = $k.'="'.$v.'"';
  197. return join(' ', $a);
  198. }
  199. function xml_array_dumbdown(&$data) {
  200. $out = array();
  201. foreach (array_keys($data) as $i) {
  202. $name = $data[$i]['name'];
  203. if (!empty($data[$i]['attributes']))
  204. $name .= ' '.xml_join_atts($data[$i]['attributes']);
  205. if (!empty($data[$i]['child'])) {
  206. $out[$name][] = xml_array_dumbdown($data[$i]['child']);
  207. }
  208. else
  209. $out[$name] = $data[$i]['content'];
  210. }
  211. return $out;
  212. }
  213. function dmp() {
  214. $args = func_get_args();
  215. foreach ($args as $thing)
  216. echo (is_scalar($thing) ? strval($thing) : var_export($thing, true)), "\n";
  217. }
  218. function dmp_filter($a) {
  219. dmp($a);
  220. return $a;
  221. }
  222. function get_echo($callable, $args = array()) {
  223. ob_start();
  224. call_user_func_array($callable, $args);
  225. return ob_get_clean();
  226. }
  227. // Recursively generate some quick assertEquals tests based on an array
  228. function gen_tests_array($name, $array) {
  229. $out = array();
  230. foreach ($array as $k=>$v) {
  231. if (is_numeric($k))
  232. $index = strval($k);
  233. else
  234. $index = "'".addcslashes($k, "\n\r\t'\\")."'";
  235. if (is_string($v)) {
  236. $out[] = '$this->assertEquals( \'' . addcslashes($v, "\n\r\t'\\") . '\', $'.$name.'['.$index.'] );';
  237. }
  238. elseif (is_numeric($v)) {
  239. $out[] = '$this->assertEquals( ' . $v . ', $'.$name.'['.$index.'] );';
  240. }
  241. elseif (is_array($v)) {
  242. $out[] = gen_tests_array("{$name}[{$index}]", $v);
  243. }
  244. }
  245. return join("\n", $out)."\n";
  246. }
  247. /**
  248. * Use to create objects by yourself
  249. */
  250. class MockClass {};
  251. function print_backtrace() {
  252. $bt = debug_backtrace();
  253. echo "Backtrace:\n";
  254. $i = 0;
  255. foreach ($bt as $stack) {
  256. echo ++$i, ": ";
  257. if ( isset($stack['class']) )
  258. echo $stack['class'].'::';
  259. if ( isset($stack['function']) )
  260. echo $stack['function'].'() ';
  261. echo "line {$stack[line]} in {$stack[file]}\n";
  262. }
  263. echo "\n";
  264. }
  265. // Mask out any input fields matching the given name
  266. function mask_input_value($in, $name='_wpnonce') {
  267. return preg_replace('@<input([^>]*) name="'.preg_quote($name).'"([^>]*) value="[^>]*" />@', '<input$1 name="'.preg_quote($name).'"$2 value="***" />', $in);
  268. }
  269. $GLOBALS['_wp_die_disabled'] = false;
  270. function _wp_die_handler( $message, $title = '', $args = array() ) {
  271. if ( !$GLOBALS['_wp_die_disabled'] ) {
  272. _default_wp_die_handler( $message, $title, $args );
  273. }
  274. else {
  275. // Ignore at our peril
  276. }
  277. }
  278. function _disable_wp_die() {
  279. $GLOBALS['_wp_die_disabled'] = true;
  280. }
  281. function _enable_wp_die() {
  282. $GLOBALS['_wp_die_disabled'] = false;
  283. }
  284. function _wp_die_handler_filter() {
  285. return '_wp_die_handler';
  286. }
  287. ?>