MyCakeTestCase.php 5.2 KB

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