MyCakeTestCase.php 4.6 KB

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