MyCakeTestCase.php 4.6 KB

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