MyCakeTestCase.php 5.0 KB

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