MyCakeTestCase.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. $pieces = explode('/', $_SERVER['SERVER_PROTOCOL'], 2);
  70. $protocol = array_shift($pieces);
  71. $url = strtolower($protocol) . '://' . $_SERVER['SERVER_NAME'] . $url;
  72. }
  73. return $url;
  74. }
  75. protected function _header($title) {
  76. if (strpos($title, 'test') === 0) {
  77. $title = substr($title, 4);
  78. $title = Inflector::humanize(Inflector::underscore($title));
  79. }
  80. return '<h3>' . $title . '</h3>';
  81. }
  82. /**
  83. * Without trailing slash!?
  84. * //TODO: test
  85. */
  86. protected function _baseurl() {
  87. return current(split("webroot", $_SERVER['PHP_SELF']));
  88. }
  89. protected static $_startTime = null;
  90. protected function _microtime($precision = 8) {
  91. return round(microtime(true), $precision);
  92. }
  93. protected function _startClock($precision = 8) {
  94. self::$_startTime = self::_microtime();
  95. }
  96. protected function _elapsedTime($precision = 8, $restart = false) {
  97. $elapsed = self::_microtime() - self::$_startTime;
  98. if ($restart) {
  99. self::_startClock();
  100. }
  101. return round($elapsed, $precision);
  102. }
  103. /**
  104. * @param float $time
  105. * @param integer precision
  106. * @param boolean $secs: usually in milliseconds (for long times set it to 'true')
  107. */
  108. protected function _printElapsedTime($time = null, $precision = 8, $secs = false) {
  109. if ($time === null) {
  110. $time = self::_elapsedTime($precision);
  111. }
  112. if ($secs) {
  113. $unit = 's';
  114. $prec = 7;
  115. } else {
  116. $time = $time * 1000;
  117. $unit = 'ms';
  118. $prec = 4;
  119. }
  120. $precision = ($precision !== null) ? $precision : $prec;
  121. pr('elapsedTime: ' . number_format($time, $precision, ',', '.') . ' ' . $unit);
  122. }
  123. protected function _title($expectation, $title = null) {
  124. $eTitle = '{expects: ' . $expectation . '}';
  125. if (!empty($title)) {
  126. $eTitle = $title . ' ' . $eTitle;
  127. }
  128. return BR . BR . '<b>' . $eTitle . '</b>' . BR;
  129. }
  130. protected function _printTitle($expectation, $title = null) {
  131. if (empty($_SERVER['HTTP_HOST']) || !isset($_GET['show_passes']) || !$_GET['show_passes']) {
  132. return false;
  133. }
  134. echo self::_title($expectation, $title);
  135. }
  136. protected function _printResults($expected, $is, $pre = null, $status = false) {
  137. if (empty($_SERVER['HTTP_HOST']) || !isset($_GET['show_passes']) || !$_GET['show_passes']) {
  138. return false;
  139. }
  140. if ($pre !== null) {
  141. echo 'value:';
  142. pr($pre);
  143. }
  144. echo 'result is:';
  145. pr($is);
  146. if (!$status) {
  147. echo 'result expected:';
  148. pr($expected);
  149. }
  150. }
  151. protected function _printResult($is, $pre = null, $status = false) {
  152. if (empty($_SERVER['HTTP_HOST']) || !isset($_GET['show_passes']) || !$_GET['show_passes']) {
  153. return false;
  154. }
  155. if ($pre !== null) {
  156. echo 'value:';
  157. pr($pre);
  158. }
  159. echo 'result is:';
  160. pr($is);
  161. }
  162. /**
  163. * OsFix method
  164. *
  165. * @param string $string
  166. * @return string
  167. */
  168. protected function _osFix($string) {
  169. return str_replace(array("\r\n", "\r"), "\n", $string);
  170. }
  171. }