MyCakeTestCase.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. abstract class MyCakeTestCase extends CakeTestCase {
  3. /**
  4. * Opposite wrapper method of assertWithinMargin.
  5. *
  6. * @param float $result
  7. * @param float $expected
  8. * @param float $margin
  9. * @param string $message
  10. * @return void
  11. */
  12. protected static function assertNotWithinMargin($result, $expected, $margin, $message = '') {
  13. $upper = $result + $margin;
  14. $lower = $result - $margin;
  15. return static::assertFalse((($expected <= $upper) && ($expected >= $lower)), $message);
  16. }
  17. /*** Helper Functions **/
  18. /**
  19. * Outputs debug information during a web tester (browser) test case
  20. * since PHPUnit>=3.6 swallowes all output by default
  21. * this is a convenience output handler since debug() or pr() have no effect
  22. *
  23. * @param mixed $data
  24. * @param bool $force Should the output be flushed (forced)
  25. * @param bool $showHtml
  26. * @return void
  27. */
  28. public static function debug($data, $force = false, $showHtml = null) {
  29. if (!empty($_GET['debug']) || !empty($_SERVER['argv']) && (in_array('-v', $_SERVER['argv'], true) || in_array('-vv', $_SERVER['argv'], true))) {
  30. if ($showHtml === null && php_sapi_name() === 'cli') {
  31. $showHtml = true;
  32. }
  33. debug($data, $showHtml);
  34. } else {
  35. return;
  36. }
  37. if (!$force) {
  38. return;
  39. }
  40. ob_flush();
  41. }
  42. /**
  43. * Outputs debug information during a web tester (browser) test case
  44. * since PHPUnit>=3.6 swallowes all output by default
  45. * this is a convenience output handler
  46. *
  47. * This method will not be part of 3.x! Please switch to debug().
  48. *
  49. * @param mixed $data
  50. * @param bool $force Should the output be flushed (forced)
  51. * @return void
  52. */
  53. public static function out($data, $plain = false, $force = false) {
  54. if (php_sapi_name() === 'cli') {
  55. return;
  56. }
  57. if (!$plain|| is_array($data)) {
  58. pr($data);
  59. } else {
  60. echo '<div>' . $data . '</div>';
  61. }
  62. if (!$force) {
  63. return;
  64. }
  65. ob_flush();
  66. }
  67. protected function _basePath($full = false) {
  68. $phpSelf = $_SERVER['PHP_SELF'];
  69. if (strpos($phpSelf, 'webroot/test.php') !== false) {
  70. $pieces = explode('webroot/test.php', $phpSelf, 2);
  71. } else {
  72. $pieces = explode('test.php', $phpSelf, 2);
  73. }
  74. $url = array_shift($pieces);
  75. if ($full) {
  76. $pieces = explode('/', $_SERVER['SERVER_PROTOCOL'], 2);
  77. $protocol = array_shift($pieces);
  78. $url = strtolower($protocol) . '://' . $_SERVER['SERVER_NAME'] . $url;
  79. }
  80. return $url;
  81. }
  82. protected function _header($title) {
  83. if (strpos($title, 'test') === 0) {
  84. $title = substr($title, 4);
  85. $title = Inflector::humanize(Inflector::underscore($title));
  86. }
  87. return '<h3>' . $title . '</h3>';
  88. }
  89. /**
  90. * Without trailing slash!?
  91. * //TODO: test
  92. */
  93. protected function _baseurl() {
  94. return current(split("webroot", $_SERVER['PHP_SELF']));
  95. }
  96. protected static $_startTime = null;
  97. protected function _microtime($precision = 8) {
  98. return round(microtime(true), $precision);
  99. }
  100. protected function _startClock($precision = 8) {
  101. static::$_startTime = static::_microtime();
  102. }
  103. protected function _elapsedTime($precision = 8, $restart = false) {
  104. $elapsed = static::_microtime() - static::$_startTime;
  105. if ($restart) {
  106. static::_startClock();
  107. }
  108. return round($elapsed, $precision);
  109. }
  110. /**
  111. * @param float $time
  112. * @param int precision
  113. * @param bool $secs: usually in milliseconds (for long times set it to 'true')
  114. */
  115. protected function _printElapsedTime($time = null, $precision = 8, $secs = false) {
  116. if ($time === null) {
  117. $time = static::_elapsedTime($precision);
  118. }
  119. if ($secs) {
  120. $unit = 's';
  121. $prec = 7;
  122. } else {
  123. $time = $time * 1000;
  124. $unit = 'ms';
  125. $prec = 4;
  126. }
  127. $precision = ($precision !== null) ? $precision : $prec;
  128. pr('elapsedTime: ' . number_format($time, $precision, ',', '.') . ' ' . $unit);
  129. }
  130. protected function _title($expectation, $title = null) {
  131. $eTitle = '{expects: ' . $expectation . '}';
  132. if (!empty($title)) {
  133. $eTitle = $title . ' ' . $eTitle;
  134. }
  135. return BR . BR . '<b>' . $eTitle . '</b>' . BR;
  136. }
  137. protected function _printTitle($expectation, $title = null) {
  138. if (empty($_SERVER['HTTP_HOST']) || !isset($_GET['show_passes']) || !$_GET['show_passes']) {
  139. return false;
  140. }
  141. echo static::_title($expectation, $title);
  142. }
  143. protected function _printResults($expected, $is, $pre = null, $status = false) {
  144. if (empty($_SERVER['HTTP_HOST']) || !isset($_GET['show_passes']) || !$_GET['show_passes']) {
  145. return false;
  146. }
  147. if ($pre !== null) {
  148. echo 'value:';
  149. pr($pre);
  150. }
  151. echo 'result is:';
  152. pr($is);
  153. if (!$status) {
  154. echo 'result expected:';
  155. pr($expected);
  156. }
  157. }
  158. protected function _printResult($is, $pre = null, $status = false) {
  159. if (empty($_SERVER['HTTP_HOST']) || !isset($_GET['show_passes']) || !$_GET['show_passes']) {
  160. return false;
  161. }
  162. if ($pre !== null) {
  163. echo 'value:';
  164. pr($pre);
  165. }
  166. echo 'result is:';
  167. pr($is);
  168. }
  169. /**
  170. * OsFix method
  171. *
  172. * @param string $string
  173. * @return string
  174. */
  175. protected function _osFix($string) {
  176. return str_replace(array("\r\n", "\r"), "\n", $string);
  177. }
  178. }